|
|
|
|
@ -2,18 +2,15 @@ |
|
|
|
|
from django.db.models import F |
|
|
|
|
from django import forms |
|
|
|
|
from django.conf import settings |
|
|
|
|
from django.forms.util import ErrorList |
|
|
|
|
from ckeditor.widgets import CKEditorWidget |
|
|
|
|
from django.core.exceptions import ValidationError |
|
|
|
|
from functions.translate import populate_all, fill_trans_fields_all |
|
|
|
|
#models |
|
|
|
|
from models import Review, Service |
|
|
|
|
from models import Service |
|
|
|
|
from country.models import Country, City |
|
|
|
|
from functions.form_check import translit_with_separator |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from django.forms.util import ErrorList |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceForm(forms.Form): |
|
|
|
|
|
|
|
|
|
europa = forms.ModelMultipleChoiceField(queryset=Country.objects.filter(region='europa'), required=False, |
|
|
|
|
@ -26,7 +23,7 @@ class ServiceForm(forms.Form): |
|
|
|
|
widget=forms.CheckboxSelectMultiple()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# city = forms.MultipleChoiceField(choices=cities) |
|
|
|
|
city = forms.MultipleChoiceField(required=False, choices="") |
|
|
|
|
|
|
|
|
|
url = forms.CharField(label='url', required=False) |
|
|
|
|
|
|
|
|
|
@ -71,7 +68,8 @@ class ServiceForm(forms.Form): |
|
|
|
|
service = Service.objects.get(id=id) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service.url = translit_with_separator(data['url']) if data['url'] else translit_with_separator(data['name_ru']) |
|
|
|
|
service.url = translit_with_separator(data['url']).lower() if data['url']\ |
|
|
|
|
else translit_with_separator(data['name_ru'].lower()) |
|
|
|
|
service.price = data['price']+' '+data['currency']#%s %s'%(data['price'], data['currency']) |
|
|
|
|
# uses because in the next loop data will be overwritten |
|
|
|
|
service.save() |
|
|
|
|
@ -83,6 +81,8 @@ class ServiceForm(forms.Form): |
|
|
|
|
|
|
|
|
|
#autopopulate |
|
|
|
|
#populate empty fields and fields which was already populated |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service_id = getattr(service, 'id') |
|
|
|
|
populate_all(Service, data, service_id, zero_fields) |
|
|
|
|
|
|
|
|
|
@ -91,102 +91,29 @@ class ServiceForm(forms.Form): |
|
|
|
|
countries += [item.id for item in data['america']] |
|
|
|
|
countries += [item.id for item in data['africa']] |
|
|
|
|
|
|
|
|
|
cities = [item for item in data['city']] |
|
|
|
|
|
|
|
|
|
#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)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#add service to checked cities |
|
|
|
|
City.objects.filter(id__in=cities).\ |
|
|
|
|
update(services = F('services').bitor(getattr(City.services, str(service.id))) ) |
|
|
|
|
City.objects.exclude(id__in=cities).\ |
|
|
|
|
update(services = F('services').bitand(~getattr(City.services, str(service.id)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clean(self): |
|
|
|
|
id = self.cleaned_data.get('service_id') |
|
|
|
|
url = self.cleaned_data.get('url') |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
service = Service.objects.get(url=translit_with_separator(url)) |
|
|
|
|
if (str(service.id) != str(id)): |
|
|
|
|
msg = 'Такой урл уже занят' |
|
|
|
|
self._errors['url'] = ErrorList([msg]) |
|
|
|
|
del self.cleaned_data['url'] |
|
|
|
|
except: |
|
|
|
|
pass |
|
|
|
|
service = Service.objects.filter(url=translit_with_separator(url)) |
|
|
|
|
if service and str(service[0].id) != id: |
|
|
|
|
msg = 'Такой урл уже занят' |
|
|
|
|
self._errors['url'] = ErrorList([msg]) |
|
|
|
|
del self.cleaned_data['url'] |
|
|
|
|
|
|
|
|
|
return self.cleaned_data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clean_url(self): |
|
|
|
|
""" |
|
|
|
|
check name which must be unique because it generate slug field |
|
|
|
|
""" |
|
|
|
|
cleaned_data = super(ServiceForm, self).clean() |
|
|
|
|
url = cleaned_data.get('url') |
|
|
|
|
id = cleaned_data.get('service_id') |
|
|
|
|
try: |
|
|
|
|
service = Service.objects.get(url=translit_with_separator(url)) |
|
|
|
|
if (translit_with_separator(url) == service.url): |
|
|
|
|
return url |
|
|
|
|
except: |
|
|
|
|
return url |
|
|
|
|
raise ValidationError('Такой урл уже занят') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReviewForm(forms.ModelForm): |
|
|
|
|
rating = forms.ChoiceField(label='Оценка', choices=[(n, n) for n in range(1,6)], widget=forms.RadioSelect()) |
|
|
|
|
comment = forms.CharField(label='Комментарий', widget=CKEditorWidget) |
|
|
|
|
class Meta: |
|
|
|
|
model = Review |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
|
class ReviewForm(forms.Form): |
|
|
|
|
web_page = forms.CharField(label='Сайт', required=False) |
|
|
|
|
rating = forms.ChoiceField(label=('Оценка'), choices=[(n,n) for n in range(1,6)],widget=forms.RadioSelect()) |
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
|
super(ReviewForm, 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['company_%s' % code] = forms.CharField(label='Компания', required=required) |
|
|
|
|
self.fields['contact_%s' % code] = forms.CharField(label='Контакт', required=required) |
|
|
|
|
self.fields['comment_%s' % code] = forms.CharField(label='Коментарий', required=required, widget=CKEditorWidget) |
|
|
|
|
|
|
|
|
|
services = [(item.id, item.name) for item in Service.objects.all()] |
|
|
|
|
self.fields['service'] = forms.ChoiceField(label='Сервис', choices=services) |
|
|
|
|
|
|
|
|
|
def save(self, id=None): |
|
|
|
|
data = self.cleaned_data |
|
|
|
|
|
|
|
|
|
if not id: |
|
|
|
|
review = Review() |
|
|
|
|
else: |
|
|
|
|
review = Review.objects.get(id=id) |
|
|
|
|
|
|
|
|
|
review.rating = data['rating'] |
|
|
|
|
review.web_page = data['web_page'] |
|
|
|
|
|
|
|
|
|
if data.get('service'): |
|
|
|
|
review.service = Service.objects.get(id=data['service']) |
|
|
|
|
|
|
|
|
|
review.save() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#populate fields with zero language |
|
|
|
|
zero_fields = {} |
|
|
|
|
|
|
|
|
|
fill_trans_fields_all(Review, review, data, id, zero_fields) |
|
|
|
|
|
|
|
|
|
#autopopulate |
|
|
|
|
#populate empty fields and fields which was already populated |
|
|
|
|
review_id = getattr(review, 'id') |
|
|
|
|
populate_all(Review, data, review_id, zero_fields) |
|
|
|
|
|
|
|
|
|
""" |