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.
 
 
 
 
 
 

353 lines
12 KiB

# -*- coding: utf-8 -*-
import json
from city.models import City
from ckeditor.widgets import CKEditorWidget
from conference.models import Conference
from country.models import Country
from django import forms
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 theme.models import Tag, Theme
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'Создать банер'
text = forms.CharField(label=u'Текст', required=False, widget=CKEditorWidget)
class Meta:
model = Banner
exclude = ['created_at', 'updated_at', 'often', 'paid', 'stat_pswd', 'cookie', 'link']
class BannerLinkCreateForm(forms.ModelForm):
verbose = u'Отслеживаемую ссылку'
class Meta:
model = Banner
fields = ['public', 'alt', 'url']
exclude = ['created_at', 'updated_at', 'often', 'paid', 'stat_pswd', 'cookie', 'link']
def save(self, commit=True):
banner = super(BannerLinkCreateForm, self).save(commit=False)
if commit:
banner.link =True
banner.save()
return banner
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 MainCreateForm(forms.ModelForm):
verbose = u'Добавить выставку на главную'
exposition = forms.CharField(label=u'Выставка', widget=forms.HiddenInput())
class Meta:
model = MainPage
fields = ['position', 'public']
def save(self, commit=True):
main = super(MainCreateForm, 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, 'main_page_link')
main.link = link_b
main.save()
expo.main = main
expo.save()
return main
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 MainConfCreateForm(forms.ModelForm):
verbose = u'Добавить конференцию на главную'
conf = forms.CharField(label=u'Конференция', widget=forms.HiddenInput())
class Meta:
model = MainPage
fields = ['position', 'public']
def save(self, commit=True):
main = super(MainConfCreateForm, self).save(commit=False)
if commit:
conf = self.cleaned_data['conf']
link = conf.get_permanent_url()
link_b = Banner.objects.create_for_paid(conf, link, 'main_page_link')
main.link = link_b
main.save()
conf.main = main
conf.save()
return main
def clean_conf(self):
conf_id = self.cleaned_data['conf']
try:
conf = Conference.objects.get(id=conf_id)
except Conference.DoesNotExist:
raise forms.ValidationError(u'Такой конференции не существует')
return conf
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 MainUpdateForm(forms.ModelForm):
class Meta:
model = MainPage
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):
cities = forms.CharField(label=u'Город', widget=forms.HiddenInput() ,required=False)
verbose = u'Изменить выставку'
class Meta:
model = Top
fields = ['catalog', 'position', 'theme', 'country', 'fr', 'to', 'months', 'cities']
fieldsets = [
{'title': '',
'class': '',
'fields': ['catalog', 'position', 'theme', 'country', 'fr', 'to']},
{'title': _(u'Топ города'),
'class': '',
'fields': ['cities', 'months']}
]
def __init__(self, *args, **kwargs):
super(TopUpdateForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance:
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))])
def clean_cities(self):
cities = self.cleaned_data.get('cities')
if cities:
res = []
for id in cities.split(','):
try:
res.append(int(id))
except ValueError:
continue
return City.objects.filter(id__in=res)
else:
return City.objects.none()
def save(self, commit=True):
top = super(TopUpdateForm, 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)
top.cities.clear()
for city in self.cleaned_data['cities']:
top.cities.add(city)
self.save_m2m = save_m2m
if commit:
top.save()
self.save_m2m()
return top