|
|
|
|
@ -13,6 +13,9 @@ from functions.files import check_tmp_files |
|
|
|
|
from functions.form_check import is_positive_integer, translit_with_separator |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from functions.translate import get_translated_fields |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tz = [ |
|
|
|
|
(99, ''), |
|
|
|
|
(-12.0, '(GMT -12:00) Eniwetok, Kwajalein'), (-11.0,'(GMT -11:00) Midway Island, Samoa'), |
|
|
|
|
@ -127,6 +130,39 @@ class CountryForm(forms.Form): |
|
|
|
|
country.language.clear() |
|
|
|
|
country.currency.clear() |
|
|
|
|
|
|
|
|
|
all_langs = [code for code, lang in settings.LANGUAGES] |
|
|
|
|
# fields in translations model that doesn't need |
|
|
|
|
bad_fields = ['id', 'language_code', 'master_id'] |
|
|
|
|
# translated fields |
|
|
|
|
fields = [field.name for field in Country.translations.related.editable_fields() if field.name not in bad_fields] |
|
|
|
|
|
|
|
|
|
# translate to first language(require) |
|
|
|
|
if not id: |
|
|
|
|
country.translate(all_langs[0]) |
|
|
|
|
# go through all fields and set value |
|
|
|
|
for field in fields: |
|
|
|
|
setattr(country, field, data.get('%s_%s'%(field, all_langs[0]))) |
|
|
|
|
|
|
|
|
|
# first save method call signal that fill require language to all translated fields |
|
|
|
|
country.save() |
|
|
|
|
else: |
|
|
|
|
# require translation. uses for substitution unfilled form fields |
|
|
|
|
require_transl = Country._meta.translations_model.objects.get(language_code = all_langs[0],master__id=getattr(country, 'id')) |
|
|
|
|
|
|
|
|
|
for code in all_langs[1:]: |
|
|
|
|
# fill translation objects |
|
|
|
|
# start from second language |
|
|
|
|
obj = Country._meta.translations_model.objects.get(language_code = code,master__id=getattr(country, 'id')) |
|
|
|
|
for field in fields: |
|
|
|
|
val = data.get('%s_%s'%(field, code)) |
|
|
|
|
if val == '': |
|
|
|
|
# get value from require translation |
|
|
|
|
setattr(obj, field, getattr(require_transl, field)) |
|
|
|
|
else: |
|
|
|
|
setattr(obj, field, val) |
|
|
|
|
|
|
|
|
|
obj.save() |
|
|
|
|
|
|
|
|
|
country.url = translit_with_separator(data['name_ru']).lower() |
|
|
|
|
country.population = data['population'] |
|
|
|
|
country.teritory = data['teritory'] |
|
|
|
|
@ -154,15 +190,7 @@ class CountryForm(forms.Form): |
|
|
|
|
# uses because in the next loop data will be overwritten |
|
|
|
|
country.save() |
|
|
|
|
|
|
|
|
|
#populate fields with zero language |
|
|
|
|
zero_fields = {} |
|
|
|
|
|
|
|
|
|
fill_trans_fields_all(Country, country, data, id, zero_fields) |
|
|
|
|
|
|
|
|
|
#autopopulate |
|
|
|
|
#populate empty fields and fields which was already populated |
|
|
|
|
country_id = getattr(country, 'id') |
|
|
|
|
populate_all(Country, data, country_id, zero_fields) |
|
|
|
|
#save files |
|
|
|
|
check_tmp_files(country, data['key']) |
|
|
|
|
|
|
|
|
|
|