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.
328 lines
15 KiB
328 lines
15 KiB
# -*- coding: utf-8 -*-
|
|
from django import forms
|
|
from django.conf import settings
|
|
from ckeditor.widgets import CKEditorWidget
|
|
from django.core.exceptions import ValidationError
|
|
from django.core.validators import validate_email, URLValidator
|
|
from django.forms.util import ErrorList
|
|
#models
|
|
from models import PlaceConference, Hall, CONFERENCE_TYPE
|
|
from country.models import Country
|
|
from city.models import City
|
|
#functions
|
|
from functions.translate import fill_with_signal
|
|
from functions.files import check_tmp_files
|
|
from functions.form_check import is_positive_integer, translit_with_separator
|
|
from functions.custom_fields import LocationWidget
|
|
from functions.admin_forms import AdminFilterForm
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
class ConferenceForm(forms.Form):
|
|
"""
|
|
Create Conference form for creating place_conference
|
|
|
|
__init__ uses for dynamic creates fields
|
|
|
|
save function saves data in PlaceConference object. If it doesnt exist create new object
|
|
"""
|
|
types = [(item1, item2) for item1, item2 in CONFERENCE_TYPE]
|
|
type = forms.ChoiceField(label=_(u'Краткое описание'), required=False, choices=types)
|
|
logo = forms.ImageField(label=_(u'Logo'), required=False, max_length=500)
|
|
country = forms.ModelChoiceField(label=_(u'Страна'), queryset=Country.objects.all(), empty_label=None)
|
|
#creates select input with empty choices cause it will be filled with ajax
|
|
city = forms.CharField(label=_(u'Город'), widget=forms.HiddenInput())
|
|
|
|
address = forms.CharField(label=_(u'Адресс'), widget=LocationWidget, required=False)
|
|
foundation_year = forms.CharField(label=_(u'Год основания'), required=False,
|
|
widget=forms.TextInput(attrs={'placeholder': _(u'Год основания')}))
|
|
phone = forms.CharField(label=_(u'Телефон'), required=False,
|
|
widget=forms.TextInput(attrs={'placeholder': _(u'Введите телефон')}))
|
|
fax = forms.CharField(label=_(u'Факс'), required=False,
|
|
widget=forms.TextInput(attrs={'placeholder': _(u'Введите факс')}))
|
|
web_page = forms.CharField(label=_(u'Веб-сайт'), required=False,
|
|
widget=forms.TextInput(attrs={'placeholder': _(u'Введите адрес сайта')}))
|
|
email = forms.CharField(label=_(u'Email'), required=False,
|
|
widget=forms.TextInput(attrs={'placeholder': _(u'Введите email')}))
|
|
total_capacity = forms.CharField(label=_(u'Общая вместимость'), required=False,
|
|
widget=forms.TextInput(attrs={'placeholder': _(u'Общая вместимость')}))
|
|
amount_halls = forms.CharField(label=_(u'Количество залов'), required=False,
|
|
widget=forms.TextInput(attrs={'placeholder': _(u'Количество залов')}))
|
|
video_link = forms.CharField(label=_(u'Видео'), required=False,
|
|
widget=forms.TextInput(attrs={'placeholder': _(u'Введите ссылку на видео')}))
|
|
#halls information
|
|
hall_name = forms.CharField(label=_(u'Название зала'), required=False)
|
|
hall_number = forms.IntegerField(label=_(u'Номер зала'), min_value=1, initial='1', required=False)
|
|
hall_capacity = forms.IntegerField(label=_(u'Вместимость зала'), min_value='1', required=False)
|
|
#
|
|
exposition_hall = forms.BooleanField(label=_(u'Выставочный зал'), required=False)#14
|
|
exp_hall_area = forms.CharField(label=_(u'Площадь выст. зала'), required=False,
|
|
widget=forms.TextInput(attrs={'placeholder': _(u'Площадь выст. зала')}))#15
|
|
#
|
|
wifi = forms.BooleanField(label='Wi-fi', required=False)
|
|
multimedia_equipment = forms.BooleanField(label=_(u'Мультимедийное оборудование'), required=False)
|
|
conference_call = forms.BooleanField(label=_(u'Конференц-связь'), required=False)
|
|
translate_equipment = forms.BooleanField(label=_(u'Оборудование для синхронного перевода'), required=False)
|
|
banquet_hall = forms.BooleanField(label=_(u'Банкетный зал'), required=False)
|
|
catering = forms.BooleanField(label=_(u'Кейтеринг'), required=False)
|
|
hotel = forms.BooleanField(label=_(u'Гостиница'), required=False)
|
|
#
|
|
key = forms.CharField(required=False, widget=forms.HiddenInput())
|
|
#
|
|
place_conference_id = forms.CharField(required=False, widget=forms.HiddenInput())
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
"""
|
|
create dynamical translated fields fields
|
|
"""
|
|
super(ConferenceForm, self).__init__(*args, **kwargs)
|
|
# creates translated form fields, 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):
|
|
# using enumerate for detect iteration number
|
|
# first iteration is a default lang so it required fields
|
|
required = True if lid == 0 else False
|
|
self.fields['name_%s' % code] = forms.CharField(label=_(u'Название'), required=required) #
|
|
self.fields['main_title_%s' % code] = forms.CharField(label=_(u'Краткое описание'), required=False, widget=CKEditorWidget)
|
|
self.fields['description_%s' % code] = forms.CharField(label=_(u'Полное описание'), required=False, widget=CKEditorWidget)
|
|
self.fields['adress_%s' % code] = forms.CharField(label=_(u'Дополнительная инф по адресу'), required=False, max_length=255,
|
|
widget=forms.TextInput(attrs={'style':'width: 550px'}))
|
|
self.fields['total_year_action_%s' % code] = forms.CharField(label=_(u'Количество мероприятий в год'), required=False, widget=CKEditorWidget)
|
|
#meta data
|
|
self.fields['title_%s' % code] = forms.CharField(label=_(u'Тайтл'), required=False, max_length=255,
|
|
widget=forms.TextInput(attrs={'style':'width: 550px'}))
|
|
self.fields['keywords_%s' % code] = forms.CharField(label=_(u'Дескрипшен'), required=False, max_length=255,
|
|
widget=forms.TextInput(attrs={'style':'width: 550px'}))
|
|
self.fields['descriptions_%s' % code] = forms.CharField(label=_(u'Кейвордс'), required=False, max_length=255,
|
|
widget=forms.TextInput(attrs={'style':'width: 550px'}))
|
|
self.fields['hall_capacity_%s' % code] = forms.CharField(label=_(u'Шаблон вместимости'),
|
|
widget=forms.TextInput(attrs={'style':'width: 550px'}), required=False)
|
|
"""
|
|
try:
|
|
capacity_tmpl = Settings.objects.get(key='hall_template')
|
|
self.fields['hall_capacity_%s' % code] = forms.CharField(label='Шаблон вместимости', initial= capacity_tmpl.get_value(code),
|
|
widget=forms.TextInput(attrs={'style':'width: 550px'}), required=False)
|
|
except:
|
|
pass
|
|
"""
|
|
|
|
|
|
def save(self, obj=None):
|
|
"""
|
|
change PlaceConference model object with id = id
|
|
N/A add new PlaceConference model object
|
|
usage: form.save(obj) - if change place_conference
|
|
form.save() - if add place_conference
|
|
"""
|
|
data = self.cleaned_data
|
|
#create new place_conference object or get exists
|
|
if not obj:
|
|
place_conference = PlaceConference()
|
|
else:
|
|
place_conference = obj
|
|
|
|
if not getattr(place_conference, 'url'):
|
|
if data.get('name_en'):
|
|
place_conference.url = translit_with_separator(data['name_en'].strip()).lower()
|
|
else:
|
|
place_conference.url = translit_with_separator(data['name_ru'].strip()).lower()
|
|
|
|
if data.get('logo'):
|
|
place_conference.logo = data['logo']
|
|
#simple fields
|
|
place_conference.type = data['type']
|
|
place_conference.address = data['address']
|
|
place_conference.phone = data['phone']
|
|
place_conference.fax = data['fax']
|
|
place_conference.web_page = data['web_page']
|
|
place_conference.video_link = data['video_link']
|
|
place_conference.email = data['email']
|
|
place_conference.foundation_year = data['foundation_year']
|
|
place_conference.total_capacity = data['total_capacity']
|
|
place_conference.amount_halls = data['amount_halls']
|
|
place_conference.exposition_hall = data['exposition_hall']
|
|
place_conference.exp_hall_area = data['exp_hall_area']
|
|
place_conference.wifi = data['wifi']
|
|
place_conference.multimedia_equipment = data['multimedia_equipment']
|
|
place_conference.conference_call = data['conference_call']
|
|
place_conference.translate_equipment = data['translate_equipment']
|
|
place_conference.banquet_hall = data['banquet_hall']
|
|
place_conference.catering = data['catering']
|
|
place_conference.hotel = data['hotel']
|
|
|
|
if data.get('country'):
|
|
place_conference.country = Country.objects.get(id=data['country'].id)#.id cause select uses queryset
|
|
if data.get('city'):
|
|
place_conference.city = City.objects.get(id=data['city'])
|
|
|
|
fill_with_signal(PlaceConference, place_conference, data)
|
|
|
|
#save files
|
|
place_conference.save()
|
|
|
|
return place_conference
|
|
|
|
def clean_web_page(self):
|
|
"""
|
|
web_page checking
|
|
"""
|
|
cleaned_data = super(ConferenceForm, self).clean()
|
|
web_page = cleaned_data.get('web_page')
|
|
if not web_page:
|
|
return ''
|
|
|
|
validate = URLValidator()
|
|
try:
|
|
validate(web_page)
|
|
except(ValidationError),e:
|
|
raise ValidationError(e.messages[0])
|
|
return web_page
|
|
|
|
def clean_video_link(self):
|
|
"""
|
|
video_link checking
|
|
"""
|
|
cleaned_data = super(ConferenceForm, self).clean()
|
|
video_link = cleaned_data.get('video_link')
|
|
if not video_link:
|
|
return ''
|
|
|
|
validate = URLValidator()
|
|
try:
|
|
validate(video_link)
|
|
except(ValidationError),e:
|
|
raise ValidationError(e.messages[0])
|
|
return video_link
|
|
|
|
def clean_email(self):
|
|
"""
|
|
email checking
|
|
"""
|
|
cleaned_data = super(ConferenceForm, self).clean()
|
|
email = cleaned_data.get('email')
|
|
if not email:
|
|
return ''
|
|
try:
|
|
validate_email(email)
|
|
except(ValidationError),e:
|
|
raise ValidationError(e.messages[0])
|
|
return email
|
|
|
|
def clean_phone(self):
|
|
"""
|
|
phone checking
|
|
"""
|
|
cleaned_data = super(ConferenceForm, self).clean()
|
|
phone = cleaned_data.get('phone')
|
|
if not phone:
|
|
return
|
|
|
|
deduct = ('-','(',')','.','+',' ')
|
|
|
|
for elem in deduct:
|
|
phone = phone.replace(elem, '')
|
|
|
|
if phone.isdigit():
|
|
return phone
|
|
else:
|
|
raise ValidationError(_(u'Введите правильный телефон'))
|
|
|
|
def clean_fax(self):
|
|
"""
|
|
fax checking
|
|
"""
|
|
cleaned_data = super(ConferenceForm, self).clean()
|
|
fax = cleaned_data.get('fax')
|
|
if not fax:
|
|
return
|
|
|
|
deduct = ('-','(',')','.',' ')
|
|
|
|
for elem in deduct:
|
|
fax = fax.replace(elem, '')
|
|
|
|
if fax.isdigit():
|
|
return fax
|
|
else:
|
|
raise ValidationError(_(u'Введите правильный факс'))
|
|
|
|
def clean_total_capacity(self):
|
|
"""
|
|
checking total_capacity
|
|
"""
|
|
cleaned_data = super(ConferenceForm, self).clean()
|
|
total_capacity = cleaned_data.get('total_capacity').strip()
|
|
return is_positive_integer(total_capacity)
|
|
|
|
def clean_foundation_year(self):
|
|
"""
|
|
checking foundation_year
|
|
"""
|
|
cleaned_data = super(ConferenceForm, self).clean()
|
|
foundation_year = cleaned_data.get('foundation_year').strip()
|
|
return is_positive_integer(foundation_year)
|
|
|
|
def clean_amount_halls(self):
|
|
"""
|
|
checking amount_halls
|
|
"""
|
|
cleaned_data = super(ConferenceForm, self).clean()
|
|
amount_halls = cleaned_data.get('amount_halls').strip()
|
|
return is_positive_integer(amount_halls)
|
|
|
|
def clean_exp_hall_area(self):
|
|
"""
|
|
checking exp_hall_area
|
|
"""
|
|
cleaned_data = super(ConferenceForm, self).clean()
|
|
exp_hall_area = cleaned_data.get('exp_hall_area').strip()
|
|
return is_positive_integer(exp_hall_area)
|
|
|
|
|
|
class PlaceConferenceFormDelete(forms.ModelForm):
|
|
url = forms.CharField(widget=forms.HiddenInput())
|
|
|
|
class Meta:
|
|
model = PlaceConference
|
|
fields = ('url',)
|
|
|
|
|
|
class HallForm(forms.ModelForm):
|
|
"""
|
|
form for Hall model
|
|
uses ModelForm cause Hall doesnt have translated fields
|
|
"""
|
|
number = forms.CharField(widget=forms.TextInput(attrs={'style': 'width:30px'}),required=False)
|
|
capacity = forms.CharField(widget=forms.TextInput(attrs={'style': 'width:60px'}), required=False)
|
|
name = forms.CharField(required=False)
|
|
class Meta:
|
|
model = Hall
|
|
exclude = ('place_conference',)
|
|
|
|
def clean_number(self):
|
|
cleaned_data = super(HallForm, self).clean()
|
|
number = cleaned_data.get('number').strip()
|
|
return is_positive_integer(number)
|
|
|
|
def clean_capacity(self):
|
|
cleaned_data = super(HallForm, self).clean()
|
|
capacity = cleaned_data.get('capacity').strip()
|
|
return is_positive_integer(capacity)
|
|
|
|
|
|
class PlaceConferenceFilterForm(AdminFilterForm):
|
|
model = PlaceConference
|
|
|
|
country = forms.MultipleChoiceField(choices=[(item.id, item.name) for item in list(Country.objects.all())],
|
|
required=False, widget=forms.SelectMultiple())
|
|
|
|
def filter(self):
|
|
qs = super(PlaceConferenceFilterForm, self).filter()
|
|
data = self.cleaned_data
|
|
country = data['country']
|
|
|
|
if country:
|
|
qs = qs.filter(country_id__in=country)
|
|
|
|
return qs
|
|
|