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.
 
 
 
 
 
 

36 lines
1.6 KiB

# -*- coding: utf-8 -*-
from django import forms
from models import Settings
from settings import LANGUAGES
from functions.translate import fill_trans_fields, populate, ZERO_LANGUAGE, populate_all, fill_trans_fields_all
class SettingsForm(forms.Form):
"""
Create Settings form for creating settings
__init__ uses for dynamic creates fields
save function saves data in Settings object. If it doesnt exist create new object
"""
def __init__(self, *args, **kwargs):
super(SettingsForm, self).__init__(*args, **kwargs)
# creates translated form fields, example: name_ru, name_en
# len(10) is a hack for detect if settings.LANGUAGES is not configured it return all langs
obj = Settings.objects.get(key='hall_template')
if len(LANGUAGES) in range(10):
for lid, (code, name) in enumerate(LANGUAGES):
# using enumerate for detect iteration number
# first iteration is a default lang so it required fields
required = True if lid == 0 else False
self.fields['hall_capacity_tmpl_%s' % code] = forms.CharField(label=getattr(obj, 'name'), initial=obj.get_value(code),
required=required,widget=forms.TextInput(attrs={'style':'width: 550px'}))
def save(self):
"""
changes Settings model
"""
data = self.cleaned_data
capacity_tmpl = Settings.objects.get(key='hall_template')
for code, name in LANGUAGES:
capacity_tmpl.set_value(data['hall_capacity_tmpl_%s'%code], code)