Skip to content

Commit

Permalink
sql_column changed to sql_predicate and now generates full predicate …
Browse files Browse the repository at this point in the history
…of multiple columns and directions
  • Loading branch information
drewyeaton committed Jun 13, 2012
1 parent babe7f3 commit f7baa26
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
30 changes: 22 additions & 8 deletions django_sortable/sortable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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'):
Expand All @@ -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
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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__

Expand All @@ -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 = '''
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit f7baa26

Please sign in to comment.