# -*- coding: utf-8 -*- from django import forms from expobanner.models import URL, BannerGroup, Banner, Paid, Top from exposition.models import Exposition from country.models import Country from city.models import City from theme.models import Theme, Tag class UrlCreateForm(forms.ModelForm): verbose = u'Создать урл' class Meta: model = URL exclude = ['created_at', 'updated_at', 'sites'] class BannerCreateGroupForm(forms.ModelForm): verbose = u'Создать групу' class Meta: model = BannerGroup exclude = ['created_at', 'updated_at', 'speed'] class BannerGroupUpdateForm(BannerCreateGroupForm): verbose = u'Изменить групу' class Meta: model = BannerGroup exclude = ['created_at', 'updated_at', 'slug', 'speed'] class BannerCreateForm(forms.ModelForm): verbose = u'Создать банер' #country = forms.ChoiceField(label=u'Страна', choices=[('', ' ')] + [(c.id, c.name) for c in Country.objects.all()], required=False) #theme = forms.ChoiceField(label=u'Тематика', required=False, # choices=[('', ' ')] + [(item.id, item.name) for item in Theme.objects.language().all()]) #city = forms.CharField(label=u'Город', widget=forms.HiddenInput(), required=False) #tag = forms.CharField(label=u'Тег', widget=forms.HiddenInput(), required=False) class Meta: model = Banner exclude = ['created_at', 'updated_at', 'often', 'paid', 'stat_pswd'] class ClientStatForm(forms.Form): stat_pswd = forms.CharField(label=u'Введите пароль:') def check_pass(self, obj): pswd = self.cleaned_data['stat_pswd'] return obj.stat_pswd == pswd class PaidCreateForm(forms.ModelForm): verbose = u'Создать проплаченую выставку' exposition = forms.CharField(label=u'Выставка', widget=forms.HiddenInput()) tickets = forms.URLField(label=u'Линк на билеты') participation = forms.URLField(label=u'Линк на участие') official = forms.URLField(label=u'Линк на официальный сайт') class Meta: model = Paid fields = ['logo', 'organiser', 'public'] def save(self, commit=True): paid = super(PaidCreateForm, self).save(commit=False) if commit: expo = self.cleaned_data['exposition'] tickets = self.cleaned_data['tickets'] tickets_link = Banner.objects.create_for_paid(expo, tickets, 'tickets') participation = self.cleaned_data['participation'] participation_link = Banner.objects.create_for_paid(expo, participation, 'participation') official = self.cleaned_data['official'] official_link = Banner.objects.create_for_paid(expo, official, 'official') catalog = expo.get_permanent_url() catalog_link = Banner.objects.create_for_paid(expo, catalog, 'catalog') paid.tickets = tickets_link paid.participation = participation_link paid.official = official_link paid.catalog = catalog_link paid.save() expo.paid_new = paid expo.save() return paid 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 PaidUpdateForm(forms.ModelForm): tickets = forms.URLField(label=u'Линк на билеты') participation = forms.URLField(label=u'Линк на участие') official = forms.URLField(label=u'Линк на официальный сайт') class Meta: model = Paid fields = ['logo', 'organiser', 'public'] def save(self, commit=True): paid = super(PaidUpdateForm, self).save(commit=False) if commit: tickets = self.cleaned_data['tickets'] b_tickets = paid.tickets if tickets != b_tickets.url: b_tickets.url = tickets b_tickets.save() participation = self.cleaned_data['participation'] b_participation = paid.participation if participation != b_participation.url: b_participation.url = participation b_participation.save() official = self.cleaned_data['official'] b_official = paid.official if official != b_official.url: b_official.url = official b_official.save() paid.save() return paid 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', 'excluded_tags', 'country', 'excluded_cities', '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) self.save_m2m = save_m2m if commit: expo = self.cleaned_data['exposition'] 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