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.
76 lines
3.0 KiB
76 lines
3.0 KiB
# -*- coding: utf-8 -*-
|
|
from django import forms
|
|
from django.conf import settings
|
|
from functions.translate import fill_with_signal
|
|
from models import Gallery, Photo
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
class GalleryForm(forms.Form):
|
|
def __init__(self, *args, **kwargs):
|
|
"""
|
|
create dynamical translated fields fields
|
|
"""
|
|
super(GalleryForm, 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'Заголовок'), required=required)
|
|
self.fields['description_%s' % code] = forms.CharField(label=_(u'Описание'), required=False)
|
|
|
|
def save(self, obj=None):
|
|
data = self.cleaned_data
|
|
#create new Gallery object or get exists
|
|
if not obj:
|
|
gallery = Gallery()
|
|
else:
|
|
gallery = obj
|
|
|
|
fill_with_signal(Gallery, gallery, data)
|
|
return gallery
|
|
|
|
|
|
class PhotoForm(forms.Form):
|
|
image = forms.ImageField(label=_(u'Изображение'), required=False)
|
|
sort = forms.IntegerField(label=_(u'Позиция'), initial=10, required=False)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
"""
|
|
create dynamical translated fields fields
|
|
"""
|
|
super(PhotoForm, 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'Описание'), required=False)
|
|
self.fields['caption_%s' % code] = forms.CharField(label=_(u'Заголовок'), required=required)
|
|
|
|
def save(self, obj=None):
|
|
data = self.cleaned_data
|
|
#create new Photo object or get exists
|
|
if not obj:
|
|
photo = Photo()
|
|
photo.image = data['image']
|
|
else:
|
|
photo = obj
|
|
photo.image = obj.image
|
|
if data.get('sort'):
|
|
photo.sort = data['sort']
|
|
|
|
fill_with_signal(Photo, photo, data)
|
|
photo.save()
|
|
return photo
|
|
|
|
|
|
class GalleryPhotoForm(PhotoForm):
|
|
def save(self, obj=None, gallery=None):
|
|
photo = super(GalleryPhotoForm, self).save(obj)
|
|
gallery.add(photo)
|
|
|