+# Get a dictionary with sort() arguments (key and reverse) by parsing the sort
+# specification format:
+# [+-]?<key>?
+# Where "-" is used to specify reverse order, while "+" is regular, ascending,
+# order (reverse = False). The key value is an Article's attribute name (title,
+# author, created, updated and uuid are accepted), and will be used as key for
+# sorting. If a value is omitted, that value is taken from the default, which
+# should be provided using the same format specification, with the difference
+# that all values must be provided for the default.
+def get_sort_args(sort_str, default):
+ def parse(s):
+ d = dict()
+ if not s:
+ return d
+ key = None
+ if len(s) > 0:
+ # accept ' ' as an alias of '+' since '+' is translated
+ # to ' ' in URLs
+ if s[0] in ('+', ' ', '-'):
+ key = s[1:]
+ d['reverse'] = (s[0] == '-')
+ else:
+ key = s
+ if key in ('title', 'author', 'created', 'updated', 'uuid'):
+ d['key'] = lambda a: getattr(a, key)
+ return d
+ args = parse(default)
+ assert args['key'] is not None and args['reverse'] is not None
+ args.update(parse(sort_str))
+ return args
+