|
|
|
|
@ -10,6 +10,7 @@ from django.utils import translation |
|
|
|
|
from django.utils.translation import ugettext_lazy as _ |
|
|
|
|
from expobanner.models import URL, Banner, BannerGroup, MainPage, Paid, Top |
|
|
|
|
from exposition.models import Exposition |
|
|
|
|
from functions.forms import FieldsetMixin |
|
|
|
|
from theme.models import Tag, Theme |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -215,85 +216,9 @@ class MainUpdateForm(forms.ModelForm): |
|
|
|
|
fields = ['position', 'public'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TopCreateForm(forms.ModelForm): |
|
|
|
|
verbose = u'Создать выставку в топе' |
|
|
|
|
exposition = forms.CharField(label=u'Выставка', widget=forms.HiddenInput()) |
|
|
|
|
country = forms.MultipleChoiceField(label=u'Страна', choices=[('', ' ')] + [(c.id, c.name) for c in Country.objects.all()], required=False) |
|
|
|
|
theme = forms.MultipleChoiceField(label=u'Тематика', required=False, |
|
|
|
|
choices=[('', ' ')] + [(item.id, item.name) for item in Theme.objects.language().all()]) |
|
|
|
|
#excluded_cities = forms.CharField(label=u'Город', widget=forms.HiddenInput(), required=False) |
|
|
|
|
#excluded_tags = forms.CharField(label=u'Тег', widget=forms.HiddenInput(), required=False) |
|
|
|
|
class Meta: |
|
|
|
|
model = Top |
|
|
|
|
fields = ['catalog', 'position', 'theme', 'country', 'fr', 'to'] |
|
|
|
|
|
|
|
|
|
def save(self, commit=True): |
|
|
|
|
top = super(TopCreateForm, self).save(commit=False) |
|
|
|
|
# Prepare a 'save_m2m' method for the form, |
|
|
|
|
old_save_m2m = self.save_m2m |
|
|
|
|
|
|
|
|
|
def save_m2m(): |
|
|
|
|
old_save_m2m() |
|
|
|
|
# This is where we actually link the pizza with toppings |
|
|
|
|
top.theme.clear() |
|
|
|
|
for theme in self.cleaned_data['theme']: |
|
|
|
|
top.theme.add(theme) |
|
|
|
|
top.country.clear() |
|
|
|
|
for country in self.cleaned_data['country']: |
|
|
|
|
top.country.add(country) |
|
|
|
|
|
|
|
|
|
self.save_m2m = save_m2m |
|
|
|
|
|
|
|
|
|
if commit: |
|
|
|
|
expo = self.cleaned_data['exposition'] |
|
|
|
|
link = expo.get_permanent_url() |
|
|
|
|
link_b = Banner.objects.create_for_paid(expo, link, 'top_link') |
|
|
|
|
top.link = link_b |
|
|
|
|
top.save() |
|
|
|
|
self.save_m2m() |
|
|
|
|
expo.top = top |
|
|
|
|
expo.save() |
|
|
|
|
|
|
|
|
|
return top |
|
|
|
|
|
|
|
|
|
def clean_theme(self): |
|
|
|
|
theme_ids = self.cleaned_data['theme'] |
|
|
|
|
if theme_ids: |
|
|
|
|
return Theme.objects.filter(id__in=theme_ids) |
|
|
|
|
return Theme.objects.none() |
|
|
|
|
|
|
|
|
|
def clean_country(self): |
|
|
|
|
country_ids = self.cleaned_data['country'] |
|
|
|
|
if country_ids: |
|
|
|
|
return Country.objects.filter(id__in=country_ids) |
|
|
|
|
return Country.objects.none() |
|
|
|
|
|
|
|
|
|
def clean_exposition(self): |
|
|
|
|
expo_id = self.cleaned_data['exposition'] |
|
|
|
|
try: |
|
|
|
|
expo = Exposition.objects.get(id=expo_id) |
|
|
|
|
except Exposition.DoesNotExist: |
|
|
|
|
raise forms.ValidationError(u'Такой выставки не существует') |
|
|
|
|
return expo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FieldsetMixin(object): |
|
|
|
|
def fieldsets(self, fieldsets=None): |
|
|
|
|
meta = getattr(self, 'Meta', None) or getattr(self, '_meta', None) |
|
|
|
|
_fieldsets = fieldsets or meta.fieldsets |
|
|
|
|
if _fieldsets: |
|
|
|
|
for fs in _fieldsets: |
|
|
|
|
yield { |
|
|
|
|
'title': fs.get('title'), |
|
|
|
|
'class': fs.get('class'), |
|
|
|
|
'fields': (self[field] for field in fs.get('fields')) if fs.get('fields') else None, |
|
|
|
|
'fieldsets': (fieldset for fieldset in self.fieldsets(fieldsets=fs.get('fieldsets'))) if fs.get('fieldsets') else None, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TopUpdateForm(forms.ModelForm, FieldsetMixin): |
|
|
|
|
class TopMixinForm(forms.ModelForm, FieldsetMixin): |
|
|
|
|
cities = forms.CharField(label=_(u'Город'), widget=forms.HiddenInput() ,required=False) |
|
|
|
|
verbose = u'Изменить выставку' |
|
|
|
|
|
|
|
|
|
class Meta: |
|
|
|
|
model = Top |
|
|
|
|
fields = [ |
|
|
|
|
@ -307,10 +232,11 @@ class TopUpdateForm(forms.ModelForm, FieldsetMixin): |
|
|
|
|
'class': '', |
|
|
|
|
'fields': ['cities', 'months', 'years']} |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
|
super(TopUpdateForm, self).__init__(*args, **kwargs) |
|
|
|
|
super(TopMixinForm, self).__init__(*args, **kwargs) |
|
|
|
|
instance = getattr(self, 'instance', None) |
|
|
|
|
if instance: |
|
|
|
|
if instance.pk: |
|
|
|
|
lang = translation.get_language() |
|
|
|
|
self.fields['cities'].widget.attrs['data-init-text'] =\ |
|
|
|
|
json.dumps([{'id': item.id, 'text': item.name} for item in list(instance.cities.filter(translations__language_code=lang))]) |
|
|
|
|
@ -329,7 +255,7 @@ class TopUpdateForm(forms.ModelForm, FieldsetMixin): |
|
|
|
|
return City.objects.none() |
|
|
|
|
|
|
|
|
|
def save(self, commit=True): |
|
|
|
|
top = super(TopUpdateForm, self).save(commit=False) |
|
|
|
|
top = super(TopMixinForm, self).save(commit=False) |
|
|
|
|
# Prepare a 'save_m2m' method for the form, |
|
|
|
|
old_save_m2m = self.save_m2m |
|
|
|
|
|
|
|
|
|
@ -348,7 +274,63 @@ class TopUpdateForm(forms.ModelForm, FieldsetMixin): |
|
|
|
|
self.save_m2m = save_m2m |
|
|
|
|
|
|
|
|
|
if commit: |
|
|
|
|
top.save() |
|
|
|
|
self.save_m2m() |
|
|
|
|
|
|
|
|
|
return top |
|
|
|
|
|
|
|
|
|
class TopCreateForm(TopMixinForm): |
|
|
|
|
verbose = u'Создать выставку в топе' |
|
|
|
|
exposition = forms.CharField(label=u'Выставка', widget=forms.HiddenInput()) |
|
|
|
|
country = forms.MultipleChoiceField(label=u'Страна', choices=[('', ' ')] + [(c.id, c.name) for c in Country.objects.all()], required=False) |
|
|
|
|
theme = forms.MultipleChoiceField(label=u'Тематика', required=False, |
|
|
|
|
choices=[('', ' ')] + [(item.id, item.name) for item in Theme.objects.language().all()]) |
|
|
|
|
#excluded_cities = forms.CharField(label=u'Город', widget=forms.HiddenInput(), required=False) |
|
|
|
|
#excluded_tags = forms.CharField(label=u'Тег', widget=forms.HiddenInput(), required=False) |
|
|
|
|
class Meta(TopMixinForm.Meta): |
|
|
|
|
fieldsets = [ |
|
|
|
|
{'title': '', |
|
|
|
|
'class': '', |
|
|
|
|
'fields': ['catalog', 'position', 'theme', 'country', 'fr', 'to', 'exposition']}, |
|
|
|
|
{'title': _(u'Топ города'), |
|
|
|
|
'class': '', |
|
|
|
|
'fields': ['cities', 'months', 'years']} |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
def save(self, commit=True): |
|
|
|
|
top = super(TopCreateForm, self).save(commit=False) |
|
|
|
|
if commit: |
|
|
|
|
expo = self.cleaned_data['exposition'] |
|
|
|
|
link = expo.get_permanent_url() |
|
|
|
|
link_b = Banner.objects.create_for_paid(expo, link, 'top_link') |
|
|
|
|
top.link = link_b |
|
|
|
|
top.save() |
|
|
|
|
self.save_m2m() |
|
|
|
|
expo.top = top |
|
|
|
|
expo.save() |
|
|
|
|
|
|
|
|
|
return top |
|
|
|
|
|
|
|
|
|
def clean_theme(self): |
|
|
|
|
theme_ids = self.cleaned_data['theme'] |
|
|
|
|
if theme_ids: |
|
|
|
|
return Theme.objects.filter(id__in=theme_ids) |
|
|
|
|
return Theme.objects.none() |
|
|
|
|
|
|
|
|
|
def clean_country(self): |
|
|
|
|
country_ids = self.cleaned_data['country'] |
|
|
|
|
if country_ids: |
|
|
|
|
return Country.objects.filter(id__in=country_ids) |
|
|
|
|
return Country.objects.none() |
|
|
|
|
|
|
|
|
|
def clean_exposition(self): |
|
|
|
|
expo_id = self.cleaned_data['exposition'] |
|
|
|
|
try: |
|
|
|
|
expo = Exposition.objects.get(id=expo_id) |
|
|
|
|
except Exposition.DoesNotExist: |
|
|
|
|
raise forms.ValidationError(u'Такой выставки не существует') |
|
|
|
|
return expo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TopUpdateForm(TopMixinForm): |
|
|
|
|
verbose = u'Изменить выставку' |
|
|
|
|
|