|
| 1 | +import itertools |
1 | 2 | from django.contrib.admin.views.main import ( |
2 | 3 | ALL_VAR, ORDER_VAR, PAGE_VAR, SEARCH_VAR, |
3 | 4 | ) |
@@ -35,48 +36,64 @@ def adminlte_paginator_number(cl, i): |
35 | 36 | ) |
36 | 37 |
|
37 | 38 |
|
| 39 | +def get_filter_id(spec): |
| 40 | + try: |
| 41 | + return getattr(spec, 'field_path') |
| 42 | + except AttributeError: |
| 43 | + try: |
| 44 | + return getattr(spec, 'parameter_name') |
| 45 | + except AttributeError: |
| 46 | + pass |
| 47 | + return spec.title |
| 48 | + |
| 49 | + |
| 50 | +@register.simple_tag |
| 51 | +def admin_extra_filters(cl): |
| 52 | + """ Return the dict of used filters which is not included |
| 53 | + in list_filters form """ |
| 54 | + used_parameters = list(itertools.chain(*(s.used_parameters.keys() |
| 55 | + for s in cl.filter_specs))) |
| 56 | + return dict( |
| 57 | + (k, v) for k, v in cl.params.items() if k not in used_parameters) |
| 58 | + |
| 59 | + |
38 | 60 | @register.simple_tag |
39 | 61 | def adminlte_admin_list_filter(cl, spec): |
40 | 62 | tpl = get_template(spec.template) |
41 | | - new_choice = [] |
| 63 | + choices = list(spec.choices(cl)) |
| 64 | + field_key = get_filter_id(spec) |
| 65 | + matched_key = field_key |
| 66 | + for choice in choices: |
| 67 | + query_string = choice['query_string'][1:] |
| 68 | + query_parts = urllib.parse.parse_qs(query_string) |
42 | 69 |
|
43 | | - # print(spec.lookup_kwarg) |
44 | | - # print(list(spec.choices(cl))) |
| 70 | + value = '' |
| 71 | + matches = {} |
| 72 | + for key in query_parts.keys(): |
| 73 | + if key == field_key: |
| 74 | + value = query_parts[key][0] |
| 75 | + matched_key = key |
| 76 | + elif key.startswith( |
| 77 | + field_key + '__') or '__' + field_key + '__' in key: |
| 78 | + value = query_parts[key][0] |
| 79 | + matched_key = key |
45 | 80 |
|
46 | | - for choice in list(spec.choices(cl)): |
47 | | - qs = (urllib.parse.parse_qs(choice.get('query_string'))) |
48 | | - # print(qs) |
49 | | - for k, v in qs.items(): |
50 | | - if k.strip('?') in cl.params.keys() and k.strip( |
51 | | - '?') != spec.lookup_kwarg: |
52 | | - continue |
53 | | - new_choice.append({'name': k.strip('?'), 'value': v[0], |
54 | | - 'display': choice.get('display'), |
55 | | - 'selected': choice.get('selected')}) |
| 81 | + if value: |
| 82 | + matches[matched_key] = value |
56 | 83 |
|
57 | | - # print(new_choice) |
| 84 | + # Iterate matches, use first as actual values, additional for hidden |
| 85 | + i = 0 |
| 86 | + for key, value in matches.items(): |
| 87 | + if i == 0: |
| 88 | + choice['name'] = key |
| 89 | + choice['value'] = value |
| 90 | + # else: |
| 91 | + # choice['additional'] = '%s=%s' % (key, value) |
| 92 | + i += 1 |
58 | 93 |
|
59 | 94 | return tpl.render({ |
| 95 | + 'field_name': field_key, |
60 | 96 | 'title': spec.title, |
61 | | - 'choices': list(spec.choices(cl)), |
62 | | - 'new_choices': new_choice, |
| 97 | + 'choices': choices, |
63 | 98 | 'spec': spec, |
64 | 99 | }) |
65 | | - |
66 | | -# def adminlte_search_form(cl): |
67 | | -# """ |
68 | | -# Display a search form for searching the list. |
69 | | -# """ |
70 | | -# print(cl) |
71 | | -# return { |
72 | | -# 'cl': cl, |
73 | | -# 'show_result_count': cl.result_count != cl.full_result_count, |
74 | | -# 'search_var': SEARCH_VAR |
75 | | -# } |
76 | | -# |
77 | | -# |
78 | | -# @register.tag(name='adminlte_search_form') |
79 | | -# def adminlte_search_form_tag(parser, token): |
80 | | -# return InclusionAdminNode(parser, token, func=adminlte_search_form, |
81 | | -# template_name='search_form.html', |
82 | | -# takes_context=False) |
|
0 commit comments