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.
116 lines
4.2 KiB
116 lines
4.2 KiB
# -*- coding: utf-8 -*-
|
|
from django.db import models
|
|
from django.utils import translation
|
|
from django.db.models.signals import post_save
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
|
|
from pymorphy.django_conf import default_morph as morph
|
|
from functions.signal_handlers import post_save_handler
|
|
|
|
# additional funcs
|
|
MONTHES = {'jan': _(u'январе'), 'feb': _(u'феврале'), 'mar': _(u'марте'), 'apr': _(u'апреле'),
|
|
'may': _(u'мае'), 'jun': _(u'июне'), u'jul': _(u'июле'), 'aug': _(u'августе'),
|
|
'sep': _(u'сентябре'), 'oct': _(u'октябре'), 'nov': _(u'ноябре'), 'dec': _(u'декабре'),}
|
|
def get_month_inflect(value, key):
|
|
return {key: MONTHES.get(value, '') }
|
|
|
|
def get_obj_inflect(obj, key):
|
|
if translation.get_language() == 'ru':
|
|
result = {key: getattr(obj, 'inflect', '')}
|
|
else:
|
|
result = {key: getattr(obj, 'name', '')}
|
|
return result
|
|
|
|
def get_theme_inflect(obj, key):
|
|
if translation.get_language() == 'ru':
|
|
result = {'theme_inflect': getattr(obj, 'inflect', ''),
|
|
'theme_name': getattr(obj, 'name', '')}
|
|
else:
|
|
result = {'theme_inflect': getattr(obj, 'name', ''),
|
|
'theme_name': getattr(obj, 'name', '')}
|
|
return result
|
|
|
|
def get_tag_inflect(obj, key):
|
|
if translation.get_language() == 'ru':
|
|
result = {'tag_inflect': getattr(obj, 'inflect', ''),
|
|
'tag_name': getattr(obj, 'name', '')}
|
|
else:
|
|
result = {'tag_inflect': getattr(obj, 'name', ''),
|
|
'tag_name': getattr(obj, 'name', '')}
|
|
return result
|
|
|
|
|
|
class MetaSetting(TranslatableModel):
|
|
name = models.CharField(max_length=100, unique=True)
|
|
translations = TranslatedFields(
|
|
title = models.CharField(max_length=255, blank=True),
|
|
description = models.CharField(max_length=255, blank=True),
|
|
keywords = models.CharField(max_length=255, blank=True),
|
|
h1 = models.CharField(max_length=255, blank=True),
|
|
)
|
|
|
|
object_params = {'object_name': 'name', 'object_title': 'main_title', 'city': 'city', 'country': 'country'}
|
|
|
|
params = {'month': get_month_inflect, 'country': get_obj_inflect, 'city': get_obj_inflect,
|
|
'theme': get_theme_inflect, 'tag': get_tag_inflect}
|
|
monthes = {'jan': _(u'январе'), 'feb': _(u'феврале'), 'mar': _(u'марте'), 'apr': _(u'апреле'),
|
|
'may': _(u'мае'), 'jun': _(u'июне'), u'jul': _(u'июле'), 'aug': _(u'августе'),
|
|
'sep': _(u'сентябре'), 'oct': _(u'октябре'), 'nov': _(u'ноябре'), 'dec': _(u'декабре'),}
|
|
|
|
|
|
def __unicode__(self):
|
|
return self.name
|
|
|
|
|
|
def generate_meta(self, params, obj=None):
|
|
"""
|
|
obj must be in current language
|
|
"""
|
|
|
|
lang = translation.get_language()
|
|
updates = {}
|
|
if obj:
|
|
for key, value in self.object_params.iteritems():
|
|
updates.update({key: getattr(obj, value, '')})
|
|
|
|
for key, value in params.iteritems():
|
|
if key in self.params:
|
|
updates.update(self.params[key](value, key))
|
|
params.update(updates)
|
|
|
|
tr = self.translations.get(language_code=lang)
|
|
title = tr.title.format(**params)
|
|
description = tr.description.format(**params)
|
|
keywords = []#tr.keywords.format(**params)
|
|
h1 = tr.h1.format(**params)
|
|
|
|
return {'title': title, 'description': description, 'keywords': keywords, 'h1': h1}
|
|
|
|
def get_param(self, obj, field):
|
|
param = self.params.get(field)
|
|
if not param:
|
|
return ''
|
|
s = getattr(obj, param['name'], '')
|
|
if param.get('inflected'):
|
|
s = morph.inflect_ru(s, u'пр')
|
|
return s
|
|
|
|
|
|
def get_title(self):
|
|
title = self.title
|
|
return title
|
|
|
|
def get_h1(self):
|
|
h1 = self.h1
|
|
return h1
|
|
|
|
def get_description(self):
|
|
description = self.description
|
|
return description
|
|
|
|
def get_keywords(self):
|
|
keywords = self.keywords
|
|
return []
|
|
|
|
|
|
post_save.connect(post_save_handler, sender=MetaSetting) |