diff --git a/conference/views.py b/conference/views.py index 8779bca5..6e4eda6e 100644 --- a/conference/views.py +++ b/conference/views.py @@ -21,7 +21,7 @@ from django.views.generic.edit import FormMixin from functions.cache_mixin import CacheMixin, JitterCacheMixin from functions.custom_views import ListView from functions.views_help import get_side_items - +from functions.utils import CachedSting from accounts.models import User from article.models import Article @@ -42,6 +42,8 @@ from theme.models import Tag, Theme from events.mixin import ConfFilterMixin MONTHES = settings.MONTHES +description_templates = settings.DEFAULT_DESCRIPTION + class ConferenceBy(ConfFilterMixin, ConfSectionMixin, JitterCacheMixin, MetadataMixin, ListView): cache_range = settings.CACHE_RANGE @@ -428,6 +430,10 @@ class ConferenceDetail(ObjectStatMixin, JitterCacheMixin, MetadataMixin, DetailV model = Conference slug_field = 'url' template_name = 'client/conference/conference_detail.html' + descriptions = { + 'ru': CachedSting(settings.DEFAULT_DESCRIPTION.get('c_description_ru')), + 'en': CachedSting(settings.DEFAULT_DESCRIPTION.get('c_description_en')), + } def dispatch(self, request, *args, **kwargs): @@ -446,9 +452,12 @@ class ConferenceDetail(ObjectStatMixin, JitterCacheMixin, MetadataMixin, DetailV raise Http404('NotFound') def get_context_data(self, **kwargs): + lang = translation.get_language() context = super(ConferenceDetail, self).get_context_data(**kwargs) context['advertising_form'] = AdvertiseForm() obj = self.object + if not obj.description: + context['default_description'] = unicode(self.descriptions[lang]).format(**obj.default_description_context()) context['city'] = str(obj.city_id) context['country'] = str(obj.country_id) context['themes'] = [str(item.id) for item in obj.theme.all()] diff --git a/exposition/views.py b/exposition/views.py index c280d0e2..b1df396a 100644 --- a/exposition/views.py +++ b/exposition/views.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- - import datetime import json @@ -18,12 +17,15 @@ from django.http import ( ) from django.shortcuts import get_object_or_404 from django.utils.translation import ugettext as _ +from django.utils import translation from django.views.generic import DetailView from django.views.generic.edit import FormMixin + from functions.cache_mixin import JitterCacheMixin from functions.custom_views import ExpoSearchView, ListView from functions.search_forms import ExpositionSearchForm from functions.views_help import get_side_items +from functions.utils import CachedSting from meta.views import MetadataMixin from models import Exposition @@ -40,6 +42,8 @@ from stats_collector.mixin import ( from theme.models import Tag, Theme from events.mixin import ExpoFilterMixin, SearchFilterMixin +description_templates = settings.DEFAULT_DESCRIPTION + class ExpositionBy(ExpoFilterMixin, ExpoSectionMixin, JitterCacheMixin, MetadataMixin, ListView): template_name = 'exposition/exposition_by.html' @@ -183,11 +187,18 @@ class ExpoDetail(ObjectStatMixin, JitterCacheMixin, MetadataMixin, DetailView): queryset = Exposition.objects.language()\ .select_related('place', 'city', 'country', 'paid_new')\ .prefetch_related('theme', 'tag') + descriptions = { + 'ru': CachedSting(settings.DEFAULT_DESCRIPTION.get('e_description_ru')), + 'en': CachedSting(settings.DEFAULT_DESCRIPTION.get('e_description_en')), + } def get_context_data(self, **kwargs): + lang = translation.get_language() context = super(ExpoDetail, self).get_context_data(**kwargs) context['advertising_form'] = AdvertiseForm() obj = self.object + if not obj.description: + context['default_description'] = unicode(self.descriptions[lang]).format(**obj.default_description_context()) context['city'] = str(obj.city_id) context['country'] = str(obj.country_id) context['themes'] = [str(item.id) for item in obj.theme.all()] diff --git a/functions/model_mixin.py b/functions/model_mixin.py index 7cf31639..112132af 100644 --- a/functions/model_mixin.py +++ b/functions/model_mixin.py @@ -7,6 +7,8 @@ from django.conf import settings from django.db.models import Q from django.utils.translation import ugettext as _ from django.utils.translation import get_language +from django.http import QueryDict + from hvad.utils import get_translation, set_cached_translation, get_cached_translation from functions.translate import fill_with_signal @@ -16,14 +18,12 @@ from photologue.models import Gallery class ExpoMixin(object): def get_logo(self): - logo = self.files.filter(purpose='logo') if logo: return logo[0] return self.logo def get_preview(self): - preview = self.files.filter(purpose='preview') if preview: return preview[0] @@ -68,6 +68,39 @@ def get_dates(data_begin, data_end): class EventMixin(object): + def default_description_context(self): + lang = get_language() + booking_url_params = QueryDict('', mutable=True) + booking_url_params.update({ + 'aid': 333667, + 'city': self.city_id, + 'do_availability_check': 'on', + 'label': '{}_search'.format(self.event_type), + 'lang': lang, + 'checkin_monthday': self.data_begin.strftime('%d'), + 'checkin_year_month': self.data_begin.strftime('%Y-%m'), + 'checkout_monthday': self.data_end.strftime('%d'), + 'checkout_year_month': self.data_end.strftime('%Y-%m'), + }) + booking_url = 'http://www.booking.com/searchresults.html?' + booking_url_params.urlencode() + try: + theme_name = self.theme.language().all()[0] + except IndexError: + theme_name = '' + + ctx = { + 'name': self.name, + 'short_descr': self.main_title, + 'dates': self.get_dates, + 'country': self.country.name if self.country else '', + 'city': self.city.name if self.city else '', + 'place': self.get_event_place_name(), + 'theme': theme_name, + 'tags': ', '.join([t.name for t in self.tag.language().all()]), + 'booking_url': booking_url, + } + return ctx + @property def get_dates(self): return get_dates(self.data_begin, self.data_end) diff --git a/functions/utils.py b/functions/utils.py index 6339456f..66b4aaf4 100644 --- a/functions/utils.py +++ b/functions/utils.py @@ -21,6 +21,7 @@ def strfdelta(tdelta, fmt): class CachedSting(object): def __init__(self, path, timeout=None): super(CachedSting, self).__init__() + print('initiated', path) self.path = path self.timeout = timeout or timedelta(days=1) self.get_object() @@ -29,12 +30,22 @@ class CachedSting(object): return self.__str__() def __str__(self): - if self.timeto > datetime.now(): - return self.object - self.get_object() return self.object + # .encode('utf-8') + + def __unicode__(self): + return self.object.decode('utf-8') def get_object(self): self.timeto = datetime.now() + self.timeout - with open(self.path, 'r') as f: - self.object = f.read() + try: + with open(self.path, 'r') as f: + self._object = f.read() + except: + self._object = '' + + @property + def object(self): + if self.timeto < datetime.now(): + self.get_object() + return self._object diff --git a/settings/forms.py b/settings/forms.py index 7b6b91a9..e2638138 100644 --- a/settings/forms.py +++ b/settings/forms.py @@ -174,12 +174,6 @@ class EventSectionSettingsForm(forms.Form): class EventDefaultDescriptionEditForm(forms.Form): - data_mapping = { - 'e_description_ru': 'templates/client/includes/conference/default_description_ru.html', - 'e_description_en': 'templates/client/includes/conference/default_description_en.html', - 'c_description_ru': 'templates/client/includes/exposition/default_description_ru.html', - 'c_description_en': 'templates/client/includes/exposition/default_description_en.html', - } e_description_ru = forms.CharField(label=_(u'Описание для выставки'), required=False, widget=CKEditorWidget) e_description_en = forms.CharField(label=_(u'Описание для выставки'), required=False, widget=CKEditorWidget) c_description_ru = forms.CharField(label=_(u'Описание для конференции'), required=False, widget=CKEditorWidget) @@ -187,17 +181,24 @@ class EventDefaultDescriptionEditForm(forms.Form): def __init__(self, *args, **kwargs): super(EventDefaultDescriptionEditForm, self).__init__(*args, **kwargs) - for field_name, path in self.data_mapping.iteritems(): + templates = settings.DEFAULT_DESCRIPTION + for field_name, path in templates.iteritems(): try: with open(os.path.join(settings.SITE_ROOT, path), 'r') as f: self.initial[field_name] = f.read() - except: - pass + except Exception as e: + print(e) + + # except: + # pass def save(self): - for field_name, path in self.data_mapping.iteritems(): + templates = settings.DEFAULT_DESCRIPTION + for field_name, path in templates.iteritems(): try: with open(os.path.join(settings.SITE_ROOT, path), 'w+') as f: - f.write(self.cleaned_data.get(field_name)) - except: - pass + print(self.cleaned_data.get(field_name)) + f.write(self.cleaned_data.get(field_name).encode('utf-8')) + except Exception as e: + print(e) + # pass diff --git a/templates/c_admin/settings/default_description.html b/templates/c_admin/settings/default_description.html index 786c3660..8059a874 100644 --- a/templates/c_admin/settings/default_description.html +++ b/templates/c_admin/settings/default_description.html @@ -10,6 +10,20 @@ {% block body %}
{% csrf_token %} +
+ Доступные параметры: + +
{name} - название события +
{short_descr} - краткое описание события +
{dates} - даты проведения события +
{country} - страна +
{city} - город +
{place} - место проведения +
{theme} - тема +
{tags} - теги +
{booking_url} - ссылка на букинг +
+

{{ form.verbose }}

diff --git a/templates/client/includes/conference/conference_object.html b/templates/client/includes/conference/conference_object.html index 30372119..972bbef1 100644 --- a/templates/client/includes/conference/conference_object.html +++ b/templates/client/includes/conference/conference_object.html @@ -148,7 +148,10 @@ {% if event.description %}
{{ event.description|safe }}
{% else %} - {% include "client/includes/conference/default_description.html" with conf=event %} +
+ {{ default_description|safe }} +
+ {# {% include "client/includes/conference/default_description.html" with conf=event %} #} {% endif %}

diff --git a/templates/client/includes/conference/conference_paid.html b/templates/client/includes/conference/conference_paid.html index a89629c6..c44a5650 100644 --- a/templates/client/includes/conference/conference_paid.html +++ b/templates/client/includes/conference/conference_paid.html @@ -189,7 +189,10 @@ {% if event.description %}
{{ event.description|safe }}
{% else %} - {% include "client/includes/conference/default_description.html" with conf=event %} +
+ {{ default_description|safe }} +
+ {# {% include "client/includes/conference/default_description.html" with conf=event %} #} {% endif %}

diff --git a/templates/client/includes/conference/default_description_en.html b/templates/client/includes/conference/default_description_en.html deleted file mode 100644 index e69de29b..00000000 diff --git a/templates/client/includes/conference/default_description_ru.html b/templates/client/includes/conference/default_description_ru.html deleted file mode 100644 index dae180f6..00000000 --- a/templates/client/includes/conference/default_description_ru.html +++ /dev/null @@ -1 +0,0 @@ -lkjlkj tty ty \ No newline at end of file diff --git a/templates/client/includes/exposition/default_description_en.html b/templates/client/includes/exposition/default_description_en.html deleted file mode 100644 index 29405b63..00000000 --- a/templates/client/includes/exposition/default_description_en.html +++ /dev/null @@ -1 +0,0 @@ -dsds \ No newline at end of file diff --git a/templates/client/includes/exposition/default_description_ru.html b/templates/client/includes/exposition/default_description_ru.html deleted file mode 100644 index e69de29b..00000000 diff --git a/templates/client/includes/exposition/exposition_object.html b/templates/client/includes/exposition/exposition_object.html index 3cfbb282..9cd9971a 100644 --- a/templates/client/includes/exposition/exposition_object.html +++ b/templates/client/includes/exposition/exposition_object.html @@ -150,9 +150,12 @@

{% trans 'О выставке' %} {{ exposition.name|safe }}

{% if exposition.description %} -
{{ exposition.description|safe }}
+
{{ exposition.description|safe }}
{%else %} - {% include "client/includes/exposition/default_description.html" with expo=exposition %} +
+ {{ default_description|safe }} +
+ {# {% include "client/includes/exposition/default_description.html" with expo=exposition %} #} {% endif %}