diff --git a/article/forms.py b/article/forms.py index 39f9e0ca..70edfb89 100644 --- a/article/forms.py +++ b/article/forms.py @@ -101,20 +101,10 @@ class ArticleForm(forms.Form): cleaned_data = super(ArticleForm, self).clean() main_title_ru = cleaned_data.get('main_title_ru') try: - Article.objects.get(url=translit_with_separator(main_title_ru)) + article = Article.objects.get(url=translit_with_separator(main_title_ru)) + if (article.url == translit_with_separator(main_title_ru)): + return main_title_ru except: return main_title_ru - raise ValidationError('Статья с таким названием уже существует') - -class ArticleChangeForm(ArticleForm): - """ - cancel checking url field - """ - def clean_main_title_ru(self): - """ - check main title which must be unique because it generate slug field - """ - cleaned_data = super(ArticleForm, self).clean() - main_title_ru = cleaned_data.get('main_title_ru') - return main_title_ru \ No newline at end of file + raise ValidationError('Статья с таким названием уже существует') \ No newline at end of file diff --git a/article/views.py b/article/views.py index f06d6069..2c748106 100644 --- a/article/views.py +++ b/article/views.py @@ -6,7 +6,7 @@ from django.conf import settings from django.contrib.auth.decorators import login_required from django.contrib.contenttypes.models import ContentType #models and forms -from forms import ArticleForm, Article, ArticleChangeForm +from forms import ArticleForm, Article from theme.models import Tag from file.models import FileModel, TmpFile from file.forms import FileModelForm @@ -46,7 +46,7 @@ def article_change(request, url): return HttpResponseRedirect('/article/all') if request.POST: - form = ArticleChangeForm(request.POST) + form = ArticleForm(request.POST) #set choices filled by ajax form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.all()] @@ -69,7 +69,7 @@ def article_change(request, url): data['keywords_%s' % code] = obj.keywords data['descriptions_%s' % code] = obj.descriptions #fill form - form = ArticleChangeForm(initial=data) + form = ArticleForm(initial=data) #set choices filled by ajax form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.filter(theme__in=data['theme'])] diff --git a/city/forms.py b/city/forms.py index 12ab1081..3a373f0e 100644 --- a/city/forms.py +++ b/city/forms.py @@ -110,7 +110,9 @@ class CityForm(forms.Form): cleaned_data = super(CityForm, self).clean() name_ru = cleaned_data.get('name_ru') try: - City.objects.get(url=translit_with_separator(name_ru)) + city = City.objects.get(url=translit_with_separator(name_ru)) + if(city.url == translit_with_separator(name_ru)): + return name_ru except: return name_ru @@ -140,12 +142,4 @@ class CityForm(forms.Form): """ cleaned_data = super(CityForm, self).clean() population = cleaned_data.get('population').strip() - return is_positive_integer(population) - -class CityChangeForm(CityForm): - """ - cancel name checking - """ - def clean_name_ru(self): - name_ru = self.cleaned_data.get('name_ru') - return name_ru \ No newline at end of file + return is_positive_integer(population) \ No newline at end of file diff --git a/city/views.py b/city/views.py index cf866de0..4982b53a 100644 --- a/city/views.py +++ b/city/views.py @@ -6,7 +6,7 @@ from django.conf import settings from django.contrib.auth.decorators import login_required from django.contrib.contenttypes.models import ContentType #models and forms -from forms import CityForm, CityChangeForm +from forms import CityForm from models import City from file.models import FileModel from file.forms import FileModelForm @@ -45,7 +45,7 @@ def city_change(request, url): return HttpResponseRedirect('/city/all') if request.POST: - form = CityChangeForm(request.POST) + form = CityForm(request.POST) if form.is_valid(): form.save(city_id) return HttpResponseRedirect('/city/all') @@ -72,7 +72,7 @@ def city_change(request, url): data['keywords_%s' % code] = obj.keywords data['descriptions_%s' % code] = obj.descriptions #fill form - form = CityChangeForm(initial=data) + form = CityForm(initial=data) args = {} diff --git a/conference/forms.py b/conference/forms.py index 0e9c8c4a..f64f3e07 100644 --- a/conference/forms.py +++ b/conference/forms.py @@ -160,7 +160,9 @@ class ConferenceCreateForm(forms.Form): cleaned_data = super(ConferenceCreateForm, self).clean() name_ru = cleaned_data.get('name_ru') try: - Conference.objects.get(url=translit_with_separator(name_ru)) + conference = Conference.objects.get(url=translit_with_separator(name_ru)) + if (conference.url == translit_with_separator(name_ru)): + return name_ru except: return name_ru @@ -225,9 +227,6 @@ class ConferenceCreateForm(forms.Form): return is_positive_integer(max_price) - - - class ConferenceChangeForm(ConferenceCreateForm): """ @@ -239,12 +238,6 @@ class ConferenceChangeForm(ConferenceCreateForm): users = forms.ModelMultipleChoiceField(label='Пользователи', queryset=User.objects.all(), required=False) - def clean_name_ru(self): - name_ru = self.cleaned_data.get('name_ru') - return name_ru - - - class TimeTableForm(forms.Form): """ Create TimeTable form @@ -269,6 +262,4 @@ class TimeTableForm(forms.Form): self.fields['name_%s' % code] = forms.CharField(label='Название', required=required) def save(self, id=None): - pass - - + pass \ No newline at end of file diff --git a/country/forms.py b/country/forms.py index 6735c020..df4590d1 100644 --- a/country/forms.py +++ b/country/forms.py @@ -6,7 +6,6 @@ from django.core.exceptions import ValidationError #models from models import Country, City from directories.models import Language, Currency, Iata -from file.models import FileModel, TmpFile #functions from functions.translate import populate_all, fill_trans_fields_all from functions.files import check_tmp_files @@ -54,6 +53,8 @@ class CountryForm(forms.Form): time_delivery = forms.CharField(label='Срок выдачи', required=False, widget=forms.TextInput(attrs={'placeholder':'Срок выдачи'})) region = forms.ChoiceField(label='Регион', choices=((item, item) for item in Country.REGIONS)) + + #services = forms.MultipleChoiceField(label='Сервисы', required=False, choices=); #field for comparing tmp files key = forms.CharField(required=False, widget=forms.HiddenInput()) @@ -169,7 +170,10 @@ class CountryForm(forms.Form): cleaned_data = super(CountryForm, self).clean() name_ru = cleaned_data.get('name_ru') try: - Country.objects.get(url=translit_with_separator(name_ru)) + country = Country.objects.get(url=translit_with_separator(name_ru)) + if (country.url == translit_with_separator(name_ru)): + return name_ru + except: return name_ru @@ -222,9 +226,3 @@ class CountryForm(forms.Form): cleaned_data = super(CountryForm, self).clean() time_delivery = cleaned_data.get('time_delivery').strip() return is_positive_integer(time_delivery) - - -class CountryChangeForm(CountryForm): - def clean_name_ru(self): - name_ru = self.cleaned_data.get('name_ru') - return name_ru \ No newline at end of file diff --git a/country/models.py b/country/models.py index 69433bc9..131eff3b 100644 --- a/country/models.py +++ b/country/models.py @@ -19,27 +19,25 @@ class Country(TranslatableModel): """ flags = [] - try: - ids = [item.id for item in Service.objects.all() ] - max = sorted(ids)[-1] - for i in range(max): - flags.append(str(i)) - except: pass + ids = [str(item.id) for item in Service.objects.all() ] + #max = sorted(ids)[-1] + #for i in range(max): + # flags.append(str(i)) - services = BitField(flags=flags) + services = BitField(flags=ids) REGIONS =('europa', 'asia', 'america', 'africa') url = models.SlugField(unique=True) - capital = models.ForeignKey(City, null=True, related_name='capital') + capital = models.ForeignKey(City,blank=True, null=True, related_name='capital') population = models.PositiveIntegerField(blank=True, null=True) teritory = models.PositiveIntegerField(blank=True, null=True) timezone = models.FloatField(blank=True, null=True) phone_code = models.PositiveIntegerField(blank=True, null=True) - big_cities = models.ManyToManyField(City, null=True, related_name='cities') - language = models.ManyToManyField(Language, null=True) - currency = models.ManyToManyField(Currency, null=True) + big_cities = models.ManyToManyField(City, blank=True, null=True, related_name='cities') + language = models.ManyToManyField(Language, blank=True, null=True) + currency = models.ManyToManyField(Currency, blank=True, null=True) time_delivery = models.PositiveSmallIntegerField(blank=True, null=True) # region = EnumField(values=REGIONS) @@ -65,5 +63,4 @@ class Country(TranslatableModel): ) def __unicode__(self): - return self.lazy_translation_getter('name', unicode(self.pk)) - + return self.lazy_translation_getter('name', unicode(self.pk)) \ No newline at end of file diff --git a/country/views.py b/country/views.py index 5b15c09b..79e0fc23 100644 --- a/country/views.py +++ b/country/views.py @@ -9,7 +9,7 @@ from django.db.models.loading import get_model from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage #models and forms from models import Country -from forms import CountryForm, CountryChangeForm +from forms import CountryForm from file.models import FileModel, TmpFile from file.forms import FileModelForm #custom views @@ -50,7 +50,7 @@ def country_change(request, url): if request.POST: #country_id sending for saving capital field in __init__ - form = CountryChangeForm(request.POST, country_id=country_id) + form = CountryForm(request.POST, country_id=country_id) if form.is_valid(): form.save(country_id) @@ -81,7 +81,7 @@ def country_change(request, url): data['currency'] = [item.id for item in c.currency.all()] #initial forms #country_id sending for initialing capital field in __init__ - form = CountryChangeForm(initial=data, country_id = c.id) + form = CountryForm(initial=data, country_id = c.id) args = {} diff --git a/exposition/forms.py b/exposition/forms.py index 5059adaf..ec3a65c6 100644 --- a/exposition/forms.py +++ b/exposition/forms.py @@ -172,7 +172,9 @@ class ExpositionCreateForm(forms.Form): cleaned_data = super(ExpositionCreateForm, self).clean() name_ru = cleaned_data.get('name_ru') try: - Exposition.objects.get(url=translit_with_separator(name_ru)) + exposition = Exposition.objects.get(url=translit_with_separator(name_ru)) + if (exposition.url == translit_with_separator(name_ru)): + return name_ru except: return name_ru @@ -279,12 +281,6 @@ class ExpositionChangeForm(ExpositionCreateForm): company = forms.ModelMultipleChoiceField(label='Компании', queryset=Company.objects.all(), required=False) users = forms.ModelMultipleChoiceField(label='Пользователи', queryset=User.objects.all(), required=False) - def clean_name_ru(self): - name_ru = self.cleaned_data.get('name_ru') - return name_ru - - - class TimeTableForm(forms.Form): begin = forms.DateTimeField(label='Время начала', widget=forms.TextInput(attrs={'style':'width: 150px'})) diff --git a/news/forms.py b/news/forms.py index caffaf09..b2437778 100644 --- a/news/forms.py +++ b/news/forms.py @@ -124,7 +124,9 @@ class NewsForm(forms.Form): cleaned_data = super(NewsForm, self).clean() main_title_ru = cleaned_data.get('main_title_ru') try: - News.objects.get(url=translit_with_separator(main_title_ru)) + news = News.objects.get(url=translit_with_separator(main_title_ru)) + if (news.url == translit_with_separator(main_title_ru)): + return main_title_ru except: return main_title_ru @@ -140,13 +142,4 @@ class NewsForm(forms.Form): if not event_id and event: raise ValidationError('Вы должны выбрать тип события') else: - return event_id - -class NewsChangeForm(NewsForm): - """ - cancel checking url - """ - def clean_main_title_ru(self): - cleaned_data = super(NewsForm, self).clean() - main_title_ru = cleaned_data.get('main_title_ru') - return main_title_ru \ No newline at end of file + return event_id \ No newline at end of file diff --git a/news/views.py b/news/views.py index 012b4d04..de2a6246 100644 --- a/news/views.py +++ b/news/views.py @@ -9,7 +9,7 @@ from django.db.models.loading import get_model import json #model and forms from models import News -from news.forms import NewsForm, NewsChangeForm +from news.forms import NewsForm from exposition.models import Exposition from conference.models import Conference from theme.models import Tag @@ -84,7 +84,7 @@ def news_change(request, url): return HttpResponseRedirect('/news/all') if request.POST: - form = NewsChangeForm(request.POST) + form = NewsForm(request.POST) #set choices filled by ajax form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.all()] @@ -125,7 +125,7 @@ def news_change(request, url): data['keywords_%s' % code] = obj.keywords data['descriptions_%s' % code] = obj.descriptions - form = NewsChangeForm(initial=data) + form = NewsForm(initial=data) #set choices filled by ajax form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.filter(theme__in=data['theme'])] if data.get('event'): diff --git a/place_conference/forms.py b/place_conference/forms.py index 78203794..42480c6c 100644 --- a/place_conference/forms.py +++ b/place_conference/forms.py @@ -157,12 +157,14 @@ class ConferenceForm(forms.Form): """ check name which must be unique because it generate slug field """ - #cleaned_data = super(ConferenceForm, self).clean() name_ru = self.cleaned_data.get('name_ru') try: - PlaceConference.objects.get(url=translit_with_separator(name_ru)) - except PlaceConference.DoesNotExist: + place_conference = PlaceConference.objects.get(url=translit_with_separator(name_ru)) + if (place_conference.url == translit_with_separator(name_ru)): + return name_ru + except: return name_ru + raise ValidationError('Место проведения с таким названием уже существует') @@ -259,12 +261,6 @@ class ConferenceForm(forms.Form): return is_positive_integer(exp_hall_area) -class ConferenceChangeForm(ConferenceForm): - - def clean_name_ru(self): - name_ru = self.cleaned_data.get('name_ru') - return name_ru - class HallForm(forms.ModelForm): """ form for Hall model diff --git a/place_conference/views.py b/place_conference/views.py index 3358fe6d..303c401b 100644 --- a/place_conference/views.py +++ b/place_conference/views.py @@ -97,7 +97,7 @@ def conference_change(request, url): if request.POST: #formset of HallForm HallFormSet = formset_factory(HallForm) - form = ConferenceChangeForm(request.POST) + form = ConferenceForm(request.POST) #set choices filled by ajax form.fields['city'].choices = [(item.id, item.name) for item in City.objects.filter(country=request.POST['country'])] formset = HallFormSet(request.POST) @@ -141,7 +141,7 @@ def conference_change(request, url): data['keywords_%s'%code] = obj.keywords data['descriptions_%s'%code] = obj.descriptions - form = ConferenceChangeForm(initial=data) + form = ConferenceForm(initial=data) #set choices filled by ajax form.fields['city'].choices = [(item.id, item.name) for item in City.objects.filter(country=data['country'])] #get existing halls diff --git a/place_exposition/forms.py b/place_exposition/forms.py index e862d4e8..0b2526a6 100644 --- a/place_exposition/forms.py +++ b/place_exposition/forms.py @@ -167,7 +167,9 @@ class ExpositionForm(forms.Form): cleaned_data = super(ExpositionForm, self).clean() name_ru = cleaned_data.get('name_ru') try: - PlaceExposition.objects.get(url=translit_with_separator(name_ru)) + place_exposittion = PlaceExposition.objects.get(url=translit_with_separator(name_ru)) + if(place_exposittion.url == translit_with_separator(name_ru)): + return name_ru except: return name_ru @@ -292,10 +294,6 @@ class ExpositionForm(forms.Form): total_halls = cleaned_data.get('total_halls').strip() return is_positive_integer(total_halls) -class ExpositionChangeForm(ExpositionForm): - def clean_name_ru(self): - name_ru = self.cleaned_data.get('name_ru') - return name_ru class HallForm(forms.ModelForm): """ diff --git a/place_exposition/views.py b/place_exposition/views.py index 0f3c5a8e..07380af8 100644 --- a/place_exposition/views.py +++ b/place_exposition/views.py @@ -98,7 +98,7 @@ def exposition_change(request, url): if request.POST: #formset of HallForm HallFormSet = formset_factory(HallForm) - form = ExpositionChangeForm(request.POST) + form = ExpositionForm(request.POST) #set choices filled by ajax form.fields['city'].choices = [(item.id, item.name) for item in City.objects.filter(country=request.POST['country'])] formset = HallFormSet(request.POST) @@ -145,7 +145,7 @@ def exposition_change(request, url): data['keywords_%s'%code] = obj.keywords data['descriptions_%s'%code] = obj.descriptions - form = ExpositionChangeForm(initial=data) + form = ExpositionForm(initial=data) #set choices filled by ajax form.fields['city'].choices = [(item.id, item.name) for item in City.objects.filter(country=data['country'])] #get existing halls diff --git a/seminar/forms.py b/seminar/forms.py index 83045f4d..3ebb1666 100644 --- a/seminar/forms.py +++ b/seminar/forms.py @@ -150,7 +150,9 @@ class SeminarCreateForm(forms.Form): cleaned_data = super(SeminarCreateForm, self).clean() name_ru = cleaned_data.get('name_ru') try: - Seminar.objects.get(url=translit_with_separator(name_ru)) + seminar = Seminar.objects.get(url=translit_with_separator(name_ru)) + if(seminar.url == translit_with_separator(name_ru)): + return name_ru except: return name_ru @@ -221,8 +223,4 @@ class SeminarChangeForm(SeminarCreateForm): """ organiser = forms.ModelMultipleChoiceField(label='Организаторы', queryset=Organiser.objects.all(), required=False) company = forms.ModelMultipleChoiceField(label='Компании', queryset=Company.objects.all(), required=False) - users = forms.ModelMultipleChoiceField(label='Пользователи', queryset=User.objects.all(), required=False) - - def clean_name_ru(self): - name_ru = self.cleaned_data.get('name_ru') - return name_ru \ No newline at end of file + users = forms.ModelMultipleChoiceField(label='Пользователи', queryset=User.objects.all(), required=False) \ No newline at end of file diff --git a/service/forms.py b/service/forms.py index 4fcea46d..8569f979 100644 --- a/service/forms.py +++ b/service/forms.py @@ -1,24 +1,38 @@ # -*- coding: utf-8 -*- +from django.db.models import F from django import forms from django.conf import settings from ckeditor.widgets import CKEditorWidget from django.core.exceptions import ValidationError -from functions.translate import fill_trans_fields, populate, ZERO_LANGUAGE, populate_all, fill_trans_fields_all +from functions.translate import populate_all, fill_trans_fields_all #models from models import Review, Service from country.models import Country, City from functions.form_check import translit_with_separator class ServiceForm(forms.Form): - countries = [(item.id, item.name) for item in Country.objects.all()] - cities = [(item.id, item.name) for item in City.objects.all()] - country = forms.MultipleChoiceField(choices=countries) - city = forms.MultipleChoiceField(choices=cities) + + europa = forms.ModelMultipleChoiceField(queryset=Country.objects.filter(region='europa'), required=False, + widget=forms.CheckboxSelectMultiple()) + asia = forms.ModelMultipleChoiceField(queryset=Country.objects.filter(region='asia'), required=False, + widget=forms.CheckboxSelectMultiple()) + america = forms.ModelMultipleChoiceField(queryset=Country.objects.filter(region='america'),required=False, + widget=forms.CheckboxSelectMultiple()) + africa = forms.ModelMultipleChoiceField(queryset=Country.objects.filter(region='africa'),required=False, + widget=forms.CheckboxSelectMultiple()) + + +# city = forms.MultipleChoiceField(choices=cities) + + url = forms.CharField(label='url', required=False) price = forms.CharField(label='Цена', required=False, widget=forms.TextInput(attrs={'placeholder':'Цена'})) currency_list = [('USD','USD'), ('EUR','EUR'), ('RUB','RUB')] currency = forms.ChoiceField(choices=currency_list) + + + def __init__(self, *args, **kwargs): super(ServiceForm, self).__init__(*args, **kwargs) # creates translated form fields, example: name_ru, name_en @@ -44,7 +58,7 @@ class ServiceForm(forms.Form): def save(self, id=None): data = self.cleaned_data - #create new Tag object or get exists + #create new Service object or get exists if not id: service = Service() else: @@ -55,13 +69,14 @@ class ServiceForm(forms.Form): # flag = flag | getattr(Theme.types, item) #generates bitfield - countries = reduce(lambda x,y: x|y, (getattr(Service.country, item) for item in data['country'])) - service.country= countries +# countries = reduce(lambda x,y: x|y, (getattr(Service.country, item) for item in data['country'])) +# service.country= countries + +# cities = reduce(lambda x,y: x|y, (getattr(Service.city, item) for item in data['city'])) +# service.city = cities - cities = reduce(lambda x,y: x|y, (getattr(Service.city, item) for item in data['city'])) - service.city = cities - service.url = translit_with_separator(data['name_ru']) + service.url = translit_with_separator(data['url']) if data['url'] else translit_with_separator(data['name_ru']) service.price = data['price']+' '+data['currency']#%s %s'%(data['price'], data['currency']) # uses because in the next loop data will be overwritten service.save() @@ -76,21 +91,31 @@ class ServiceForm(forms.Form): service_id = getattr(service, 'id') populate_all(Service, data, service_id, zero_fields) - def clean_name_ru(self): + countries = [item.id for item in data['europa']] + countries += [item.id for item in data['asia']] + countries += [item.id for item in data['america']] + countries += [item.id for item in data['africa']] + + #add service to checked countries + Country.objects.filter(id__in=countries).\ + update(services = F('services').bitor(getattr(Country.services, str(service.id))) ) + Country.objects.exclude(id__in=countries).\ + update(services = F('services').bitand(~getattr(Country.services, str(service.id)))) + + + def clean_url(self): """ check name which must be unique because it generate slug field """ cleaned_data = super(ServiceForm, self).clean() - name_ru = cleaned_data.get('name_ru') + url = cleaned_data.get('url') try: - Service.objects.get(url=translit_with_separator(name_ru)) + service = Service.objects.get(url=translit_with_separator(url)) + if (url ==service.url): + return url except: - return name_ru - - raise ValidationError('Сервис с таким названием уже существует') - - - + return url + raise ValidationError('Такой урл уже занят') diff --git a/service/templates/service_add.html b/service/templates/service_add.html index fa483473..4d502336 100644 --- a/service/templates/service_add.html +++ b/service/templates/service_add.html @@ -8,6 +8,21 @@ + + {% endblock %} @@ -28,20 +43,12 @@ {% with field='name' form=form languages=languages %} {% include 'admin/forms/multilang.html' %} {% endwith %} - {# country #} -
- + {# url #} +
+
- {{ form.country }} - {{ form.country.errors }} -
-
- {# country #} -
- -
- {{ form.city }} - {{ form.city.errors }} + {{ form.url }} + {{ form.url.errors }}
{# main_title #} @@ -64,6 +71,20 @@ {{ form.price.errors }}
+ {# countries #} +
+ +
+ + {{ form.europa }} + + {{ form.asia }} + + {{ form.america }} + + {{ form.africa }} +
+
{# keywords #} {% with field='keywords' form=form languages=languages %} {% include 'admin/forms/multilang.html' %} diff --git a/service/templates/service_all.html b/service/templates/service_all.html index e52fc4e5..c01636cf 100644 --- a/service/templates/service_all.html +++ b/service/templates/service_all.html @@ -23,7 +23,7 @@ {{ item.name }} {{ item.price }} - + Изменить @@ -31,7 +31,7 @@ {% endfor %} - Добавить услугу + Добавить услугу {# pagination #}