filters.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from django.contrib.admin import RelatedFieldListFilter
  2. from django.utils.encoding import smart_text
  3. from django.utils.html import format_html
  4. try:
  5. from django.core.urlresolvers import reverse
  6. except ImportError: # Django 1.11
  7. from django.urls import reverse
  8. try:
  9. from django.contrib.admin.utils import get_model_from_relation
  10. except ImportError: # Django 1.6
  11. from django.contrib.admin.util import get_model_from_relation
  12. try:
  13. from django.forms.utils import flatatt
  14. except ImportError: # Django 1.6
  15. from django.forms.util import flatatt
  16. class RelatedFieldAjaxListFilter(RelatedFieldListFilter):
  17. template = 'jet/related_field_ajax_list_filter.html'
  18. ajax_attrs = None
  19. def has_output(self):
  20. return True
  21. def field_choices(self, field, request, model_admin):
  22. model = field.remote_field.model if hasattr(field, 'remote_field') else field.related_field.model
  23. app_label = model._meta.app_label
  24. model_name = model._meta.object_name
  25. self.ajax_attrs = format_html('{0}', flatatt({
  26. 'data-app-label': app_label,
  27. 'data-model': model_name,
  28. 'data-ajax--url': reverse('jet:model_lookup'),
  29. 'data-queryset--lookup': self.lookup_kwarg
  30. }))
  31. if self.lookup_val is None:
  32. return []
  33. other_model = get_model_from_relation(field)
  34. if hasattr(field, 'rel'):
  35. rel_name = field.rel.get_related_field().name
  36. else:
  37. rel_name = other_model._meta.pk.name
  38. queryset = model._default_manager.filter(**{rel_name: self.lookup_val}).all()
  39. return [(x._get_pk_val(), smart_text(x)) for x in queryset]
  40. try:
  41. from collections import OrderedDict
  42. from django import forms
  43. from django.contrib.admin.widgets import AdminDateWidget
  44. from rangefilter.filter import DateRangeFilter as OriginalDateRangeFilter
  45. from django.utils.translation import ugettext as _
  46. class DateRangeFilter(OriginalDateRangeFilter):
  47. def get_template(self):
  48. return 'rangefilter/date_filter.html'
  49. def _get_form_fields(self):
  50. # this is here, because in parent DateRangeFilter AdminDateWidget
  51. # could be imported from django-suit
  52. return OrderedDict((
  53. (self.lookup_kwarg_gte, forms.DateField(
  54. label='',
  55. widget=AdminDateWidget(attrs={'placeholder': _('From date')}),
  56. localize=True,
  57. required=False
  58. )),
  59. (self.lookup_kwarg_lte, forms.DateField(
  60. label='',
  61. widget=AdminDateWidget(attrs={'placeholder': _('To date')}),
  62. localize=True,
  63. required=False
  64. )),
  65. ))
  66. @staticmethod
  67. def _get_media():
  68. css = [
  69. 'style.css',
  70. ]
  71. return forms.Media(
  72. css={'all': ['range_filter/css/%s' % path for path in css]}
  73. )
  74. except ImportError:
  75. pass