|
|
|
|
@ -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('Такой урл уже занят') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|