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.
83 lines
3.3 KiB
83 lines
3.3 KiB
# -*- coding: utf-8 -*-
|
|
from django import forms
|
|
from django.conf import settings
|
|
from models import MetaSetting, SeoText
|
|
from functions.translate import fill_with_signal
|
|
from functions.admin_forms import AdminFilterForm
|
|
from ckeditor.widgets import CKEditorWidget
|
|
from hvad.forms import TranslatableModelForm
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
class MetaForm(forms.Form):
|
|
name = forms.CharField(label=_(u'Название страницы'))
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
"""
|
|
create dynamical translated fields fields
|
|
"""
|
|
super(MetaForm, self).__init__(*args, **kwargs)
|
|
# creates translated forms example: name_ru, name_en
|
|
# len(10) is a hack for detect if settings.LANGUAGES is not configured it return all langs
|
|
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
|
|
self.fields['title_%s' % code] = forms.CharField(label=_(u'Title'), required=required,
|
|
widget=forms.TextInput(attrs={'style':'width: 550px'}))
|
|
self.fields['description_%s' % code] = forms.CharField(label=_(u'Description'), required=required,
|
|
widget=forms.TextInput(attrs={'style':'width: 550px'}))
|
|
self.fields['keywords_%s' % code] = forms.CharField(label=_(u'Keywords'), required=False,
|
|
widget=forms.TextInput(attrs={'style':'width: 550px'}))
|
|
self.fields['h1_%s' % code] = forms.CharField(label=_(u'H1'), required=False,
|
|
widget=forms.TextInput(attrs={'style':'width: 550px'}))
|
|
|
|
def save(self, obj=None):
|
|
data = self.cleaned_data
|
|
if not obj:
|
|
meta = MetaSetting()
|
|
else:
|
|
meta = obj
|
|
|
|
meta.name = data['name']
|
|
|
|
|
|
# fill translated fields and save object
|
|
fill_with_signal(MetaSetting, meta, data)
|
|
meta.save()
|
|
|
|
|
|
class MetaFilterForm(forms.Form):
|
|
model = MetaSetting
|
|
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__title=name).distinct()
|
|
return qs
|
|
|
|
qs = model.objects.all()
|
|
if name:
|
|
qs = qs.filter(translations__title__icontains=name).distinct()
|
|
|
|
return qs
|
|
|
|
|
|
class SeoTextForm(TranslatableModelForm):
|
|
# lang = forms.ChoiceField(choices=settings.LANGUAGES)
|
|
class Meta:
|
|
model = SeoText
|
|
fields = ['url', 'title', 'page_title', 'description', 'body']
|
|
widgets = {'body': CKEditorWidget}
|
|
|