You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.4 KiB
49 lines
1.4 KiB
# -*- coding: utf-8 -*-
|
|
from django import forms
|
|
from django.conf import settings
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
class AdminForm(forms.Form):
|
|
model = None
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
"""
|
|
create dynamical translated fields fields
|
|
"""
|
|
if len(settings.LANGUAGES) in range(10):
|
|
for lid, (code, name) in enumerate(settings.LANGUAGES):
|
|
# uses enumerate for detect iteration number
|
|
# first iteration is a default lang so it required fields
|
|
required = True if lid == 0 else False
|
|
|
|
|
|
class AdminFilterForm(forms.Form):
|
|
"""
|
|
class for filtering lists in admin panel
|
|
"""
|
|
model = None # which models work with
|
|
exact_name = forms.CharField(label=_(u'Название'), required=False)
|
|
name = forms.CharField(label=_(u'Часть названия'), required=False)
|
|
|
|
|
|
def filter(self):
|
|
"""
|
|
|
|
return filtered queryset
|
|
form must be cleaned before calling this method
|
|
|
|
"""
|
|
model = self.model
|
|
data = self.cleaned_data
|
|
name = data['name']
|
|
exact_name = data['exact_name']
|
|
if exact_name:
|
|
qs = model.objects.filter(translations__name=name).distinct()
|
|
return qs
|
|
|
|
qs = model.objects.all()
|
|
if name:
|
|
qs = qs.filter(translations__name__contains=name).distinct()
|
|
|
|
return qs |