diff --git a/templates/admin/theme/theme_list.html b/templates/admin/theme/theme_list.html index 1e5cdb1e..198ac351 100644 --- a/templates/admin/theme/theme_list.html +++ b/templates/admin/theme/theme_list.html @@ -7,8 +7,37 @@

Фильтры

-
- {{ form }} + + +
+ {# exact_name #} +
+ +
+ {{ form.exact_name }} + {{ form.exact_name.errors }} +
+
+ {# name #} +
+ +
+ {{ form.name }} + {{ form.name.errors }} +
+
+
+ +
+ {# types #} +
+ +
+ {{ form.types }} + {{ form.types.errors }} +
+
+
@@ -63,4 +92,4 @@ {# pagination #} {% include 'admin/includes/admin_pagination.html' with page_obj=object_list %}
-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/theme/forms.py b/theme/forms.py index a344c5ed..4fe9732d 100644 --- a/theme/forms.py +++ b/theme/forms.py @@ -1,10 +1,11 @@ # -*- coding: utf-8 -*- from bitfield import BitField -from bitfield.forms import BitFieldCheckboxSelectMultiple, BitFormField +from bitfield.forms import BitFormField as _BitFormField from bitfield.types import BitHandler from ckeditor.widgets import CKEditorWidget from django import forms from django.conf import settings +from django.utils.translation import ugettext_lazy as _ from functions.admin_forms import AdminFilterForm from functions.form_check import translit_with_separator from functions.translate import fill_with_signal @@ -137,8 +138,43 @@ class TagDeleteForm(forms.ModelForm): fields = ('id',) + +class BitFormField(_BitFormField): + """ + instead int bit returns list witch we can query FOO__in + """ + def clean(self, value): + if not value: + return 0 + # Assume an iterable which contains an item per flag that's enabled + result = [] + l = [k for k, v in self.choices] + for k in value: + b = BitHandler(0, l) + try: + setattr(b, str(k), True) + except AttributeError: + raise ValidationError('Unknown choice: %r' % (k,)) + result.append(int(b)) + return result + + class ThemeFilterForm(AdminFilterForm): model = Theme + types = BitFormField(label=_(u'Тип'), required=False, choices=Theme.FLAGS) + + def filter(self): + model = self.model + data = self.cleaned_data + qs = model.objects.all() + if data['types']: + qs = qs.filter(types__in=data['types']) + if data['exact_name']: + qs = qs.filter(translations__name=data['exact_name']).distinct() + return qs + if data['name']: + qs = qs.filter(translations__name__icontains=data['name']).distinct() + return qs class TagFilterForm(AdminFilterForm):