From 14912f001bd414608515ba01479b931dbc85fe38 Mon Sep 17 00:00:00 2001 From: Alexander Burdeiny Date: Tue, 3 May 2016 15:41:04 +0300 Subject: [PATCH] =?UTF-8?q?1330:=20=D0=AD=D1=82=D0=B0=D0=BF=20=E2=84=963?= =?UTF-8?q?=20-=20=D0=93=D0=9E=D0=A0=D0=98=D0=A2,=20=D0=BF=D0=BB=D0=B0?= =?UTF-8?q?=D1=88=D0=BA=D0=B0=20=D0=A2=D0=9E=D0=9F=20=D1=80=D0=B0=D0=B7?= =?UTF-8?q?=D0=BC=D0=B5=D1=89=D0=B5=D0=BD=D0=B8=D1=8F=20top=20year=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- expobanner/forms.py | 9 +++++---- .../migrations/0002_auto__add_field_top_months.py | 15 ++++++++++++--- expobanner/models.py | 8 +++++++- expobanner/utils.py | 3 ++- functions/custom_fields.py | 15 ++++++++++++--- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/expobanner/forms.py b/expobanner/forms.py index 5c1c9fe1..45f96d7f 100644 --- a/expobanner/forms.py +++ b/expobanner/forms.py @@ -292,19 +292,20 @@ class FieldsetMixin(object): class TopUpdateForm(forms.ModelForm, FieldsetMixin): - cities = forms.CharField(label=u'Город', widget=forms.HiddenInput() ,required=False) - + cities = forms.CharField(label=_(u'Город'), widget=forms.HiddenInput() ,required=False) verbose = u'Изменить выставку' class Meta: model = Top - fields = ['catalog', 'position', 'theme', 'country', 'fr', 'to', 'months', 'cities'] + fields = [ + 'catalog', 'position', 'theme', 'country', 'fr', 'to', 'months', + 'cities', 'years'] fieldsets = [ {'title': '', 'class': '', 'fields': ['catalog', 'position', 'theme', 'country', 'fr', 'to']}, {'title': _(u'Топ города'), 'class': '', - 'fields': ['cities', 'months']} + 'fields': ['cities', 'months', 'years']} ] def __init__(self, *args, **kwargs): super(TopUpdateForm, self).__init__(*args, **kwargs) diff --git a/expobanner/migrations/0002_auto__add_field_top_months.py b/expobanner/migrations/0002_auto__add_field_top_months.py index 4e04f5c7..55f5cec3 100644 --- a/expobanner/migrations/0002_auto__add_field_top_months.py +++ b/expobanner/migrations/0002_auto__add_field_top_months.py @@ -13,6 +13,11 @@ class Migration(SchemaMigration): self.gf('functions.custom_fields.MonthMultiSelectField')(default=None, max_length=255, null=True, blank=True), keep_default=False) + # Adding field 'Top.years' + db.add_column(u'expobanner_top', 'years', + self.gf('django.db.models.fields.PositiveSmallIntegerField')(default=None, null=True, blank=True), + keep_default=False) + # Adding M2M table for field cities on 'Top' m2m_table_name = db.shorten_name(u'expobanner_top_cities') db.create_table(m2m_table_name, ( @@ -27,6 +32,9 @@ class Migration(SchemaMigration): # Deleting field 'Top.months' db.delete_column(u'expobanner_top', 'months') + # Deleting field 'Top.years' + db.delete_column(u'expobanner_top', 'years') + # Removing M2M table for field cities on 'Top' db.delete_table(db.shorten_name(u'expobanner_top_cities')) @@ -171,7 +179,7 @@ class Migration(SchemaMigration): 'country': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': u"orm['country.Country']", 'null': 'True', 'blank': 'True'}), 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), 'flash': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), - 'fr': ('django.db.models.fields.DateField', [], {'default': 'datetime.datetime(2016, 5, 1, 0, 0)'}), + 'fr': ('django.db.models.fields.DateField', [], {'default': 'datetime.datetime(2016, 5, 3, 0, 0)'}), 'group': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'banners'", 'null': 'True', 'to': u"orm['expobanner.BannerGroup']"}), 'html': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), @@ -273,14 +281,15 @@ class Migration(SchemaMigration): 'country': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': u"orm['country.Country']", 'null': 'True', 'blank': 'True'}), 'excluded_cities': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': u"orm['city.City']", 'null': 'True', 'blank': 'True'}), 'excluded_tags': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': u"orm['theme.Tag']", 'null': 'True', 'blank': 'True'}), - 'fr': ('django.db.models.fields.DateField', [], {'default': 'datetime.datetime(2016, 5, 1, 0, 0)'}), + 'fr': ('django.db.models.fields.DateField', [], {'default': 'datetime.datetime(2016, 5, 3, 0, 0)'}), u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'link': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['expobanner.Banner']"}), 'months': ('functions.custom_fields.MonthMultiSelectField', [], {'default': 'None', 'max_length': '255', 'null': 'True', 'blank': 'True'}), 'position': ('django.db.models.fields.PositiveIntegerField', [], {'default': '2', 'null': 'True', 'blank': 'True'}), 'stat_pswd': ('django.db.models.fields.CharField', [], {'max_length': '16'}), 'theme': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': u"orm['theme.Theme']", 'null': 'True', 'blank': 'True'}), - 'to': ('django.db.models.fields.DateField', [], {'null': 'True', 'blank': 'True'}) + 'to': ('django.db.models.fields.DateField', [], {'null': 'True', 'blank': 'True'}), + 'years': ('django.db.models.fields.PositiveSmallIntegerField', [], {'default': 'None', 'null': 'True', 'blank': 'True'}) }, u'expobanner.topstat': { 'Meta': {'object_name': 'TopStat'}, diff --git a/expobanner/models.py b/expobanner/models.py index 600abe48..1b43bf43 100644 --- a/expobanner/models.py +++ b/expobanner/models.py @@ -278,6 +278,11 @@ class Top(models.Model, StatMixin): """ store information about events in top """ + YEARS = map( + lambda x: (x, x), + tuple(xrange(date.today().year - 1, date.today().year + 5)) + ) + link = models.ForeignKey(Banner) catalog = models.CharField(max_length=16, verbose_name=u'Каталог для топа') position = models.PositiveIntegerField(blank=True, default=2, null=True, verbose_name=u'Позиция') @@ -285,7 +290,8 @@ class Top(models.Model, StatMixin): excluded_tags = models.ManyToManyField('theme.Tag', blank=True, null=True, verbose_name=u'Исключить теги') country = models.ManyToManyField('country.Country', blank=True, null=True, verbose_name=u'Страны') cities = models.ManyToManyField('city.City', related_name='top_in_set', blank=True, null=True, verbose_name=_(u'Города')) - months = MonthMultiSelectField(_(u'Топ месяцев'), blank=True, null=True, default=None, max_length=255,) + months = MonthMultiSelectField(_(u'Топ месяца'), blank=True, null=True, default=None, max_length=255,) + years = models.PositiveSmallIntegerField(_(u'Топ года'), choices=YEARS, blank=True, null=True, default=None) excluded_cities = models.ManyToManyField('city.City', blank=True, null=True, verbose_name=u'Исключить города') fr = models.DateField(default=date.today(), verbose_name=u'Начало') to = models.DateField(blank=True, null=True, verbose_name=u'Конец') diff --git a/expobanner/utils.py b/expobanner/utils.py index 1fc4779a..bc665c98 100644 --- a/expobanner/utils.py +++ b/expobanner/utils.py @@ -144,7 +144,8 @@ def get_top_events(tops, params, request): continue # check city if all([city, city in city_ids, not theme]): - if (month and not month in top.months) or (not month and year): + # check for top of month or top of year + if (month and not month in top.months) or (not month and year and year != str(top.years)): continue good_tops.append(top) continue diff --git a/functions/custom_fields.py b/functions/custom_fields.py index 615a7e52..428e8cd3 100644 --- a/functions/custom_fields.py +++ b/functions/custom_fields.py @@ -241,8 +241,7 @@ _regex_validator = validators.RegexValidator( class MonthMultiSelectField(models.CharField): __metaclass__ = models.SubfieldBase _saved_month_names = None - _saved_choices_start_month = None - _saved_choices_start_year = None + _saved_choices_month = None _saved_choices = None _saved_choices_values = None _inst_saved_choices = set() @@ -259,7 +258,7 @@ class MonthMultiSelectField(models.CharField): self.validators.append(_regex_validator) if choices is None: - self._choices = self._make_choices() + self._choices = self.make_choices() @property def flatchoices(self): @@ -300,6 +299,7 @@ class MonthMultiSelectField(models.CharField): def _make_choices(cls): today = date.today() #years = [today.year, today.year + 1] # map(lambda x: today.year + x, xrange(0, 2)) + cls._saved_choices_month == today.replace(day=1) choices = [] cls._saved_choices_values = set() for year in [today.year, today.year + 1]: @@ -310,6 +310,15 @@ class MonthMultiSelectField(models.CharField): choices.append((year, year_choices)) return choices + @classmethod + def _check_relevance(cls): + return cls._saved_choices_month == date.today().replace(day=1) + + def make_choices(self): + if self._saved_choices is not None and self._check_relevance(): + return self._saved_choices + return self._make_choices() + @property def flatchoices(self): return None