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.
148 lines
4.0 KiB
148 lines
4.0 KiB
# -*- coding: utf-8 -*-
|
|
import copy
|
|
import calendar as python_calendar
|
|
from service.models import Service
|
|
|
|
|
|
class ExpoMixin(object):
|
|
|
|
# def get_index_text(self):
|
|
# names = [tr.name for tr in self.translations.all()]
|
|
# return names
|
|
|
|
def get_logo(self):
|
|
|
|
logo = self.files.filter(purpose='logo')
|
|
if logo:
|
|
return logo[0]
|
|
return logo
|
|
|
|
def get_preview(self):
|
|
|
|
preview = self.files.filter(purpose='preview')
|
|
if preview:
|
|
return preview[0]
|
|
return preview
|
|
|
|
def get_photos(self):
|
|
photos = self.files.filter(purpose='photo')
|
|
return photos
|
|
|
|
|
|
class EventMixin(object):
|
|
def get_permanent_url(self):
|
|
url = '%s%s/'%(self.get_catalog_url(), self.url)
|
|
return url
|
|
|
|
def get_paid_catalog_url(self):
|
|
return self.paid_new.catalog.get_click_link()
|
|
|
|
def org_split(self):
|
|
if self.org:
|
|
return self.org.split(';')
|
|
else:
|
|
return []
|
|
|
|
def get_logo(self):
|
|
return self.logo
|
|
"""
|
|
logo = self.files.filter(purpose='logo')
|
|
if logo:
|
|
return logo[0]
|
|
return logo
|
|
"""
|
|
|
|
def get_preview(self):
|
|
|
|
preview = self.files.filter(purpose='preview')
|
|
if preview:
|
|
return preview[0]
|
|
return preview
|
|
|
|
def get_photos(self):
|
|
if self.photogallery:
|
|
return self.photogallery.photos.language().all()
|
|
else:
|
|
return None
|
|
|
|
"""
|
|
photos = self.files.filter(purpose='photo')
|
|
return photos
|
|
"""
|
|
|
|
def on(self):
|
|
self.is_published = True
|
|
self.canceled_by_administrator = False
|
|
self.save()
|
|
|
|
def off(self):
|
|
self.is_published = False
|
|
self.canceled_by_administrator = True
|
|
self.save()
|
|
|
|
def cancel(self):
|
|
self.canceled_by_administrator = True
|
|
|
|
def get_services(self):
|
|
|
|
country_ids = [item for item, bool in self.country.services if bool==True]
|
|
ids = [item for item, bool in self.services if bool==True and item in country_ids]
|
|
|
|
return list(Service.objects.language().filter(url__in=ids).order_by('sort'))
|
|
|
|
def duration_days(self, month=None):
|
|
if not month:
|
|
d = self.data_end - self.data_begin
|
|
return d.days+1
|
|
else:
|
|
if self.data_begin.month == month and self.data_end.month ==month:
|
|
d = self.data_end - self.data_begin
|
|
return d.days + 1
|
|
if self.data_begin.month == month:
|
|
last_day = python_calendar.monthrange(self.data_begin.year, month)[1]
|
|
return last_day - self.data_begin.day + 1
|
|
|
|
|
|
if self.data_end.month == month:
|
|
return self.data_end.day
|
|
|
|
return 0
|
|
|
|
def copy(self, url):
|
|
"""
|
|
Copy event with new url
|
|
:param url: new url for event
|
|
:return: event object
|
|
"""
|
|
# check url
|
|
Model = type(self)
|
|
try:
|
|
Model.objects.get(url=url)
|
|
return u'Событие с таким урлом уже существует'
|
|
except Model.DoesNotExist:
|
|
pass
|
|
|
|
duplicate = copy.copy(self)
|
|
duplicate.url = url
|
|
# Setting pk to None. for Django its a new object.
|
|
duplicate.pk = None
|
|
# copy translations
|
|
ignore_fields = ['id', 'master', 'language_code']
|
|
duplicate.translate('ru')
|
|
tr = self._meta.translations_model.objects.get(language_code='ru', master__id=self.pk)
|
|
for field in duplicate._translated_field_names:
|
|
if field in ignore_fields:
|
|
continue
|
|
|
|
setattr(duplicate, field, getattr(tr, field))
|
|
duplicate.is_published = False
|
|
duplicate.save() # save but lost all ManyToMany relations
|
|
|
|
# copy relations
|
|
for field in self._meta.many_to_many:
|
|
source = getattr(self, field.attname)
|
|
destination = getattr(duplicate, field.attname)
|
|
for item in source.all():
|
|
destination.add(item)
|
|
|
|
return duplicate |