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.
59 lines
2.5 KiB
59 lines
2.5 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
|
|
|
|
|
|
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(AdminFilterForm):
|
|
model = MetaSetting
|
|
|
|
|
|
class SeoTextForm(TranslatableModelForm):
|
|
# lang = forms.ChoiceField(choices=settings.LANGUAGES)
|
|
class Meta:
|
|
model = SeoText
|
|
fields = ['url', 'title', 'page_title', 'description', 'body']
|
|
widgets = {'body': CKEditorWidget}
|
|
|