Skip to content
Browse files

sql_column changed to sql_predicate and now generates full predicate …

…of multiple columns and directions
  • Loading branch information...
1 parent babe7f3 commit f7baa26575a2f35d41991e6077d80f9990b1ba8e @drewyeaton committed Jun 13, 2012
Showing with 27 additions and 13 deletions.
  1. +22 −8 django_sortable/sortable.py
  2. +3 −3 readme.md
  3. +2 −2 setup.py
View
30 django_sortable/sortable.py
@@ -70,25 +70,26 @@ def comparer(left, right):
return result
- def sql_column(self, field_name, direction='asc', default=None):
- """Returns a column for use in a SQL ORDER BY clause."""
+ def sql_predicate(self, field_name, direction='asc', default=None):
+ """Returns a predicate for use in a SQL ORDER BY clause."""
if self.fields:
try:
- field = self.fields[field_name]
+ fields = self.fields[field_name]
except KeyError:
- return default
+ fields = default
else:
- field = field_name
+ fields = field_name
if direction not in ('asc', 'desc'):
- return default
+ fields = default
- return '%s %s' % (field, direction.upper())
+ fields = Sortable.prepare_fields(fields, direction, sql_predicate=True)
+ return ', '.join(fields)
@staticmethod
- def prepare_fields(fields, direction):
+ def prepare_fields(fields, direction, sql_predicate=False):
"""Given a list or tuple of fields and direction, return a list of fields
with their appropriate order_by direction applied.
@@ -99,6 +100,8 @@ def prepare_fields(fields, direction):
['one', '-two', '-three', '-four', 'five']
>>> Sortable.prepare_fields(fields, 'not_asc_or_desc')
['one', '-two', 'three', 'four', '-five']
+ >>> Sortable.prepare_fields(fields, 'desc', True)
+ ['one ASC', 'two DESC', 'three DESC', 'four DESC', 'five ASC']
"""
if direction not in ('asc', 'desc'):
@@ -115,4 +118,15 @@ def prepare_fields(fields, direction):
else:
field = field[1:] if field.startswith('+') else field
fields[i] = (direction == 'desc' and '-' or '') + field
+
+ if not sql_predicate:
+ return fields
+
+ # determine sql predicates from list
+ fields = list(fields)
+ for i, field in enumerate(fields):
+ if field.startswith('-'):
+ fields[i] = '%s DESC' % (field[1:],)
+ else:
+ fields[i] = '%s ASC' % (field,)
return fields
View
6 readme.md
@@ -111,7 +111,7 @@ __authors.html__
##Raw SQL Usage
-You can use django-sortable with raw SQL queries by asking it for ordering columns. This way you can build the query yourself and append an `ORDER BY` clause. Here's an example:
+You can use django-sortable with raw SQL queries by asking it for ordering columns. This way you can build the query yourself and append a predicate generated by django-sortable in the `ORDER BY` clause. Here's an example:
__views.py__
@@ -125,10 +125,10 @@ __views.py__
direction = request.GET.get('dir', 'asc')
# also, you can pass in a default ordering column(s)
- order_col = sortable.sql_column(
+ order_col = sortable.sql_predicate(
field_name=field_name,
direction=direction,
- default='m.title ASC, p.title ASC, t.condition DESC'
+ default=('m.title', 'p.title', '-t.condition')
)
sql = '''
View
4 setup.py
@@ -16,10 +16,10 @@
'Topic :: Software Development :: Libraries :: Python Modules'
]
-KEYWORDS = 'sorting sortable queryset pagination django'
+KEYWORDS = 'sorting sortable queryset sql pagination django'
setup(name='django-sortable',
- version='0.2.1',
+ version='0.3.0',
description='Flexible sorting for Django applications',
long_description=LONG_DESCRIPTION,
author='Drew Yeaton',

0 comments on commit f7baa26

Please sign in to comment.
Something went wrong with that request. Please try again.