-
Notifications
You must be signed in to change notification settings - Fork 28
2202 multicolumn sort interface #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.0
Are you sure you want to change the base?
Changes from 9 commits
abbc66b
24c9c79
76a4536
a4197ed
cca0164
f7a3a83
1edd4d0
43f4bc7
0675fed
9c142f1
0c4ede7
f9f8b6c
639c26c
61ee487
885103b
4e01f24
58c3159
c1adfbe
529f858
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,13 +99,13 @@ def header(self): | |
| q['s'] = '%s%s' % (d, self.field) | ||
| else: | ||
| q['s'] = self.field | ||
| next_sort = 'descending' if sort == 'Ascending' else 'ascending' | ||
| next_sort = 'sort descending' if sort == 'Ascending' else 'remove from sort' if sort == 'Descending' else 'sort ascending' | ||
| sr_label = (' <span class="sr-only">(%s)</span>' % sort) if sort else '' | ||
| if self.field_definition: | ||
| span = '<span title="{}" class ="fa fa-question-circle"></span>'.format(self.field_definition) | ||
| else: | ||
| span = '' | ||
| html = '<th class="%s"><a href="?%s" title="Click to sort %s" data-sort="%s">%s%s %s</a></th>' % (cls, q.urlencode(), next_sort, q['s'], self.header_html, sr_label, span) | ||
| html = '<th class="%s"><a href="?%s" title="Click to %s" data-sort="%s">%s%s %s</a></th>' % (cls, q.urlencode(), next_sort, q['s'], self.header_html, sr_label, span) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change this to an f-string so it's a little more readable? html = f'<th class="{ cls }"><a href="?{ q.urlencode() }" title="Click to { next_sort }" data-sort="{ q["s"] }">{ self.header_html }{ sr_label } { span }</a></th>'
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The most current version of the code uses |
||
| return mark_safe(html) | ||
|
|
||
| def context(self, result, **kwargs): | ||
|
|
@@ -748,19 +748,17 @@ def apply_sort_descriptor(self, sort): | |
| desc = sort.startswith('-') | ||
| field = sort.lstrip('-') | ||
| missing = self.missing_sort | ||
| sort_querydict = {'order': 'desc' if desc else 'asc'} | ||
| if missing == '_low': | ||
| missing = '_last' if desc else '_first' | ||
| sort_querydict.update({'missing':'_last' if desc else '_first'}) | ||
| elif missing == '_high': | ||
| missing = '_first' if desc else '_last' | ||
| sort_querydict.update({'missing':'_first' if desc else '_last'}) | ||
| return { | ||
| field: { | ||
| 'order': 'desc' if desc else 'asc', | ||
| 'missing': missing, | ||
| } | ||
| sort: sort_querydict | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed, I think we can revert the changes to this function
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're correct, I've reverted them in the most recent commit |
||
| } | ||
|
|
||
| def sort_descriptor(self, sort): | ||
| if self.missing_sort is None or isinstance(sort, dict): | ||
| if isinstance(sort, dict): | ||
| return sort | ||
| if not isinstance(sort, list): | ||
| return self.apply_sort_descriptor(sort) | ||
|
|
@@ -1054,7 +1052,8 @@ def dispatch(self, request, *args, **kwargs): | |
|
|
||
| class AdvancedColumn(Column): | ||
|
|
||
| def header(self, results=None): | ||
| def header(self, results=None, default_sort=None): | ||
|
|
||
| cls = '{}_{}'.format(self.view.document._doc_type.name, self.field.replace('.', '_')) | ||
| cls += ' {}_{}'.format(self.view.document.__name__.lower(), self.field.replace('.', '_')) | ||
| if self.model_lower: | ||
|
|
@@ -1063,18 +1062,29 @@ def header(self, results=None): | |
| return mark_safe('<th class="%s">%s</th>' % (cls, self.header_html)) | ||
| current_sort = self.view.search_object['sort'] | ||
| sort = None | ||
| sort_order = 0 | ||
| cls += ' sort' | ||
| if current_sort.lstrip('-') == self.field: | ||
| # If the current sort field is this field, give it a class a change direction. | ||
| sort = 'Descending' if current_sort.startswith('-') else 'Ascending' | ||
| cls += ' desc' if current_sort.startswith('-') else ' asc' | ||
| d = '' if current_sort.startswith('-') else '-' | ||
| data_sort = '{}{}'.format(d, self.field) | ||
| if not isinstance(current_sort, list): | ||
| current_sort = [current_sort] | ||
| sr_label = '' | ||
| data_sort = self.field | ||
| if not current_sort or current_sort == ['']: | ||
| if default_sort.lstrip('-') == self.field: | ||
| cls += ' desc' if default_sort.startswith('-') else ' asc' | ||
| sort_default = sort = 'Descending' if default_sort.startswith('-') else 'Ascending' | ||
| sr_label = '<span class="sr-only">({})</span>'.format(sort_default) | ||
| else: | ||
| data_sort = self.field | ||
|
|
||
| next_sort = 'descending' if sort == 'Ascending' else 'ascending' | ||
| sr_label = (' <span class="sr-only">(%s)</span>' % sort) if sort else '' | ||
| for sort_field in current_sort: | ||
| if sort_field.lstrip('-') == self.field: | ||
| # If the current sort field is this field, give it a class a change direction. | ||
| sort = 'Descending' if sort_field.startswith('-') else 'Ascending' | ||
| cls += ' desc' if sort_field.startswith('-') else ' asc' | ||
| d = '' if sort_field.startswith('-') else '-' | ||
| data_sort = '{}{}'.format(d, self.field) | ||
| sort_order = current_sort.index(sort_field) + 1 | ||
| sr_label = ('<span class="sr-only">Number {} in sort order ({})</span>'.format(sort_order, sort)) | ||
|
|
||
| next_sort = 'sort descending' if sort == 'Ascending' else 'remove from sort' if sort == 'Descending' else 'sort ascending' | ||
|
|
||
| # If results provided, we check to see if header has space to allow for wordwrapping. If it already wordwrapped | ||
| # (i.e. has <br> in header) we skip it. | ||
|
|
@@ -1085,7 +1095,11 @@ def header(self, results=None): | |
| span = '<span title="{}" class ="fa fa-question-circle"></span>'.format(self.field_definition) | ||
| else: | ||
| span = '' | ||
| html = '<th class="{}"><a href="#" title="Click to sort {}" data-sort="{}">{}{} {}</a></th>'.format(cls, next_sort, data_sort, self.header_html, sr_label, span) | ||
| if sort_order: | ||
| sort_rank = '<span class="sort_rank">{}</span>'.format(sort_order) | ||
| else: | ||
| sort_rank = '' | ||
| html = '<th class="{}">{}<a href="#" title="Click to {}" data-sort={}>{}{} {}</a></th>'.format(cls, sort_rank, next_sort, data_sort, self.header_html, sr_label, span) | ||
| return mark_safe(html) | ||
|
|
||
| def get_data_max_length(self, results): | ||
|
|
@@ -1394,7 +1408,7 @@ def post(self, request, *args, **kwargs): | |
| except ValueError: | ||
| return HttpResponseBadRequest("Improperly formatted 'search_object', json.loads failed.") | ||
|
|
||
| # Sanity check that the search object has all of it's required components | ||
| # Sanity check that the search object has all of its required components | ||
| if not all(k in self.search_object for k in ('query', 'keywords', 'page', 'sort', 'display')): | ||
| return HttpResponseBadRequest("The 'search_object' is not in the proper format.") | ||
|
|
||
|
|
@@ -1525,11 +1539,16 @@ def render_results(self, export): | |
|
|
||
| # Finally, grab the results. | ||
| sort = self.get_sort_field(columns, self.search_object['sort'], display) | ||
|
|
||
| default_sort_field = '' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed, let me know if you figure out what this was used for so we can determine if it's still needed
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| sort_descrip_dict = self.sort_descriptor(sort) | ||
| if sort_descrip_dict: | ||
| default_sort_field = list(sort_descrip_dict.keys())[0].replace('.raw', '') | ||
| if list(sort_descrip_dict.values())[0]['order'] == 'desc': | ||
| default_sort_field = '{}{}'.format('-', default_sort_field) | ||
|
|
||
| if sort: | ||
| if (self.missing_sort is None or isinstance(sort, dict)) and isinstance(sort, list): | ||
| results = search.sort(*self.sort_descriptor(sort))[offset:offset + page_size].params(request_timeout=self.search_timeout).execute() | ||
| else: | ||
| results = search.sort(self.sort_descriptor(sort))[offset:offset + page_size].params(request_timeout=self.search_timeout).execute() | ||
| results = search.sort(*self.sort_descriptor(sort))[offset:offset + page_size].params(request_timeout=self.search_timeout).execute() | ||
| else: | ||
| results = search[offset:offset + page_size].params(request_timeout=self.search_timeout).execute() | ||
|
|
||
|
|
@@ -1558,6 +1577,7 @@ def render_results(self, export): | |
| 'results': results, | ||
| 'show_rank': self.show_rank, | ||
| 'sort': sort, | ||
| 'default_sort': default_sort_field, | ||
| 'export_name': self.export_name, | ||
| 'use_wordwrap_header': self.use_wordwrap_header, | ||
| 'search': search | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.