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.
 
 
 
 
 
 

126 lines
5.4 KiB

# -*- coding: utf-8 -*-
from django import forms
from django.conf import settings
from ckeditor.widgets import CKEditorWidget
from models import Translator
from country.models import Country
from city.models import City
from django.utils.translation import ugettext as _
#
from accounts.models import User, Profile
#functions
from functions.translate import fill_with_signal
from functions.files import check_tmp_files
from functions.form_check import translit_with_separator
from functions.admin_forms import AdminFilterForm
class TranslatorForm(forms.Form):
"""
Create Translator form for creating translator
__init__ uses for dynamic creates fields
save function saves data in Translator object. If it doesnt exist create new object
"""
car = forms.BooleanField(label=_(u'Личный автомобиль'), required=False)
birth = forms.DateField(label=_(u'Дата рождения'))
gender = forms.ChoiceField(label=_(u'Пол'), choices=[('male', _(u'Мужской')),('female', _(u'Женский'))])
#
key = forms.CharField(required=False, widget=forms.HiddenInput())
def __init__(self, *args, **kwargs):
"""
create dynamical translated fields fields
"""
super(TranslatorForm, 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['education_%s' % code] = forms.CharField(label=_(u'Образование'), required=False)
self.fields['specialization_%s' % code] = forms.CharField(label=_(u'Специализация'), required=False)
self.fields['languages_%s' % code] = forms.CharField(label=_(u'Языки'), required=required)
self.fields['native_language_%s' % code] = forms.CharField(label=_(u'Родной язык'), required=required)
self.fields['prices_%s' % code] = forms.CharField(label=_(u'Цены'),
required=False, widget=CKEditorWidget)
self.fields['discounts_%s' % code] = forms.CharField(label=_(u'Скидки'),
required=False, widget=CKEditorWidget)
#meta data
self.fields['title_%s' % code] = forms.CharField(label=_(u'Тайтл'), required=False, max_length=255,
widget=forms.TextInput(attrs={'style':'width: 550px'}))
self.fields['keywords_%s' % code] = forms.CharField(label=_(u'Дескрипшен'), required=False, max_length=255,
widget=forms.TextInput(attrs={'style':'width: 550px'}))
self.fields['descriptions_%s' % code] = forms.CharField(label=_(u'Кейвордс'), required=False, max_length=255,
widget=forms.TextInput(attrs={'style':'width: 550px'}))
def save(self, id=None):
translator = Translator.objects.get(id=id)
data = self.cleaned_data
translator.car = data['car']
translator.birth = data['birth']
translator.gender = data['gender']
fill_with_signal(Translator, translator, data)
def clean_user(self):
user = self.cleaned_data.get('user')
u = User.objects.get(id = user.id)
if u.translator:
raise forms.ValidationError(_(u'У этого пользователя уже есть профиль переводчика'))
else:
return user
class TranslatorDeleteForm(forms.ModelForm):
id = forms.CharField(widget=forms.HiddenInput())
class Meta:
model = Translator
fields = ('id', )
class TranslatorFilterForm(AdminFilterForm):
model = Translator
class TranslatorUserForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name')
class TranslatorUserProfileForm(forms.ModelForm):
city = forms.CharField(label=_(u'Город'), required=False,
widget=forms.HiddenInput(attrs={'class': 'select2'}))
country = forms.ChoiceField(label=_(u'Страна'), choices=[(c.id, c.name) for c in Country.objects.all()], required=False,
widget=forms.Select(attrs={'class': 'select2'}))
def __init__(self, *args, **kwargs):
super(TranslatorUserProfileForm, self).__init__(*args, **kwargs)
if self.instance.city:
self.fields['city'].widget = forms.HiddenInput(attrs={'class': 'select2', 'data-init-text': self.instance.city.name})
def clean_city(self):
try:
return City.objects.get(id=self.cleaned_data['city'])
except City.DoesNotExist:
return None
def clean_country(self):
try:
return Country.objects.get(id=self.cleaned_data['country'])
except City.DoesNotExist:
return None
class Meta:
model = Profile
fields = ('country', 'city', 'avatar')