From e002028cab71db33df99dfe6ce9d0d4d219afb3d Mon Sep 17 00:00:00 2001 From: Kotiuk Nazarii Date: Tue, 1 Sep 2015 21:59:19 +0300 Subject: [PATCH] Expobanners. Top: Hardcoded for js --- expobanner/admin.py | 19 +++- expobanner/admin_urls.py | 5 + expobanner/forms.py | 59 +++++++++- expobanner/managers.py | 10 ++ expobanner/models.py | 21 ++-- expobanner/urls.py | 1 + expobanner/utils.py | 2 +- expobanner/views.py | 34 +++++- exposition/models.py | 1 + templates/admin/expobanner/top_create.html | 84 +++++++++++++++ templates/admin/expobanner/top_list.html | 36 +++++++ templates/admin/includes/admin_nav.html | 1 + templates/client/includes/banners/tops.html | 13 +++ .../client/includes/exposition/expo_top.html | 102 ++++++++++++++++++ .../includes/exposition/exposition_list.html | 3 +- 15 files changed, 374 insertions(+), 17 deletions(-) create mode 100644 templates/admin/expobanner/top_create.html create mode 100644 templates/admin/expobanner/top_list.html create mode 100644 templates/client/includes/banners/tops.html create mode 100644 templates/client/includes/exposition/expo_top.html diff --git a/expobanner/admin.py b/expobanner/admin.py index 1625121c..5bb709d7 100644 --- a/expobanner/admin.py +++ b/expobanner/admin.py @@ -5,7 +5,7 @@ from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404 from expobanner.models import URL, BannerGroup, Banner, Paid from expobanner.forms import UrlCreateForm, BannerCreateGroupForm, BannerCreateForm, BannerGroupUpdateForm,\ - PaidCreateForm, PaidUpdateForm + PaidCreateForm, PaidUpdateForm, TopCreateForm from exposition.models import Exposition @@ -138,4 +138,19 @@ def paid_turn(request, pk, status): class PaidStat(DetailView): model = Paid - template_name = 'admin/expobanner/paid_stat.html' \ No newline at end of file + template_name = 'admin/expobanner/paid_stat.html' + + +class TopList(ListView): + model = Exposition + template_name = 'admin/expobanner/top_list.html' + paginate_by = settings.ADMIN_PAGINATION + + def get_queryset(self): + return self.model.objects.language().filter(top__isnull=False) + + +class TopCreate(CreateView): + form_class = TopCreateForm + template_name = 'admin/expobanner/top_create.html' + success_url = '/admin/expobanners/top/list/' diff --git a/expobanner/admin_urls.py b/expobanner/admin_urls.py index c03071da..5497da30 100644 --- a/expobanner/admin_urls.py +++ b/expobanner/admin_urls.py @@ -21,4 +21,9 @@ urlpatterns = patterns('expobanner.admin', url(r'^paid/$', PaidCreate.as_view(), name='expobanner-create_paid'), url(r'^paid/turn/(?P\d+)/(?P.*)/$', paid_turn, name='expobanner-paid-turn'), url(r'^paid/(?P\d+)/stat/$', PaidStat.as_view(), name='expobanner_stat_paid'), + # top + url(r'^top/list/$', TopList.as_view(), name='expobanner-list_top'), + url(r'^top/(?P\d+)/edit/$', PaidUpdate.as_view(), name='expobanner-update_top'), + url(r'^top/$', TopCreate.as_view(), name='expobanner-create_top'), + url(r'^top/(?P\d+)/stat/$', PaidStat.as_view(), name='expobanner_stat_top'), ) \ No newline at end of file diff --git a/expobanner/forms.py b/expobanner/forms.py index 8bb9e0cf..f489d731 100644 --- a/expobanner/forms.py +++ b/expobanner/forms.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- from django import forms -from expobanner.models import URL, BannerGroup, Banner, Paid +from expobanner.models import URL, BannerGroup, Banner, Paid, Top from exposition.models import Exposition from country.models import Country from city.models import City @@ -123,4 +123,59 @@ class PaidUpdateForm(forms.ModelForm): paid.save() - return paid \ No newline at end of file + return paid + + +class TopCreateForm(forms.ModelForm): + verbose = u'Создать выставку в топе' + exposition = forms.CharField(label=u'Выставка', widget=forms.HiddenInput()) + country = forms.MultipleChoiceField(label=u'Страна', choices=[('', ' ')] + [(c.id, c.name) for c in Country.objects.all()], required=False) + theme = forms.MultipleChoiceField(label=u'Тематика', required=False, + choices=[('', ' ')] + [(item.id, item.name) for item in Theme.objects.language().all()]) + excluded_cities = forms.CharField(label=u'Город', widget=forms.HiddenInput(), required=False) + excluded_tags = forms.CharField(label=u'Тег', widget=forms.HiddenInput(), required=False) + class Meta: + model = Top + fields = ['catalog', 'position', 'theme', 'excluded_tags', 'country', 'excluded_cities', 'fr', 'to'] + + def save(self, commit=True): + top = super(TopCreateForm, self).save(commit=False) + # Prepare a 'save_m2m' method for the form, + old_save_m2m = self.save_m2m + + def save_m2m(): + old_save_m2m() + # This is where we actually link the pizza with toppings + top.theme.clear() + for theme in self.cleaned_data['theme']: + top.theme.add(theme) + + self.save_m2m = save_m2m + + if commit: + expo = self.cleaned_data['exposition'] + top.save() + self.save_m2m() + expo.top = top + expo.save() + return top + + def clean_theme(self): + theme_ids = self.cleaned_data['theme'] + if theme_ids: + return Theme.objects.filter(id__in=theme_ids) + return None + + def clean_country(self): + country_ids = self.cleaned_data['country'] + if country_ids: + return Country.objects.filter(id__in=country_ids) + return None + + def clean_exposition(self): + expo_id = self.cleaned_data['exposition'] + try: + expo = Exposition.objects.get(id=expo_id) + except Exposition.DoesNotExist: + raise forms.ValidationError(u'Такой выставки не существует') + return expo \ No newline at end of file diff --git a/expobanner/managers.py b/expobanner/managers.py index dfc42384..73cd002c 100644 --- a/expobanner/managers.py +++ b/expobanner/managers.py @@ -59,4 +59,14 @@ class URLCached(models.Manager): if not result: result = list(self.filter(public=True)) cache.set(key, result, 150) + return result + +class TopCached(models.Manager): + def all(self): + key = 'expo_b_top_all' + result = cache.get(key) + if not result: + result = list(self.prefetch_related('theme', 'country', 'excluded_tags', 'excluded_cities').all()) + cache.set(key, result, 80) + return result \ No newline at end of file diff --git a/expobanner/models.py b/expobanner/models.py index 436abfa5..e543b29e 100644 --- a/expobanner/models.py +++ b/expobanner/models.py @@ -7,7 +7,7 @@ from django.utils.translation import ugettext_lazy as _ from django.conf import settings from django.contrib.sites.models import Site from django.db.models.signals import post_save -from .managers import BiasedManager, BannerGroupCached, URLCached +from .managers import BiasedManager, BannerGroupCached, URLCached, TopCached from .mixins import StatMixin from theme.models import Theme from country.models import Country @@ -239,16 +239,19 @@ class PaidStat(models.Model): class Top(models.Model, StatMixin): - catalog = models.CharField(max_length=16) - position = models.PositiveIntegerField(blank=True, null=True) - theme = models.ManyToManyField('theme.Theme', blank=True, null=True) - excluded_tags = models.ManyToManyField('theme.Tag', blank=True, null=True) - country = models.ManyToManyField('country.Country', blank=True, null=True) - excluded_cities = models.ManyToManyField('city.City', blank=True, null=True) - fr = models.DateField(default=date.today()) - to = models.DateField(blank=True, null=True) + catalog = models.CharField(max_length=16, verbose_name=u'Каталог для топа') + position = models.PositiveIntegerField(blank=True, default=2, null=True, verbose_name=u'Позиция') + theme = models.ManyToManyField('theme.Theme', blank=True, null=True, verbose_name=u'Тематики') + 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'Страны') + 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'Конец') stat_pswd = models.CharField(max_length=16) + objects = models.Manager() + cached = TopCached() + class Meta: ordering = ['position'] diff --git a/expobanner/urls.py b/expobanner/urls.py index 801aece7..189b2023 100644 --- a/expobanner/urls.py +++ b/expobanner/urls.py @@ -7,6 +7,7 @@ urlpatterns = [ #url(r'^view/(?P\d+)/$', views.view, name='banner_view'), # url(r'^get-banners/$', views.get_banners), + url(r'^get-tops/$', views.get_top), url(r'^banner/(?P\d+)/stat/$', BannerStat.as_view(), name='banner_stat_client'), url(r'^paid/(?P\d+)/stat/$', PaidStat.as_view(), name='paid_stat_client'), ] diff --git a/expobanner/utils.py b/expobanner/utils.py index 50e2f2c6..a7a9cd53 100644 --- a/expobanner/utils.py +++ b/expobanner/utils.py @@ -21,7 +21,7 @@ def get_by_sort(banner_list): def get_banner_by_params(banners_list, urls, params): - print('START. NUMBER of queries = %d'%len(connection.queries)) + #print('START. NUMBER of queries = %d'%len(connection.queries)) thematic_banners = [] url_banners = [] diff --git a/expobanner/views.py b/expobanner/views.py index fd2763a6..d0e38527 100644 --- a/expobanner/views.py +++ b/expobanner/views.py @@ -3,7 +3,7 @@ import json import re from django.http import HttpResponse from django.shortcuts import redirect, get_object_or_404 -from .models import Banner, BannerGroup, URL +from .models import Banner, BannerGroup, URL, Top from expobanner.utils import get_by_sort, get_banner_by_params, get_client_ip @@ -67,4 +67,34 @@ def get_banners(request): # add view log banner.log(request, 1) - return HttpResponse(json.dumps(result, indent=4), content_type='application/json') \ No newline at end of file + return HttpResponse(json.dumps(result, indent=4), content_type='application/json') + +def get_top_events(tops, params): + catalog = params.get('catalog') + country = params.get('country', '') + theme = params.get('theme', []) + good_tops = [] + for top in tops: + + if top.catalog != catalog: + continue + country_ids = [str(item.id) for item in top.country.all()] + if not country in country_ids: + continue + + +from exposition.models import Exposition +from django.shortcuts import render_to_response +from django.template import RequestContext +def get_top(request): + params = {'theme': request.GET.get('theme'), + 'tag': request.GET.get('tag'), + 'country': request.GET.get('country'), + 'city': request.GET.get('city'), + 'catalog': request.GET.get('catalog')} + + tops = Top.cached.all() + events = get_top_events(tops, params) + expos = Exposition.objects.filter(top__isnull=False) + context = {'objects': expos} + return render_to_response('client/includes/exposition/expo_top.html', context, context_instance=RequestContext(request)) \ No newline at end of file diff --git a/exposition/models.py b/exposition/models.py index ed34bff5..76b7177f 100644 --- a/exposition/models.py +++ b/exposition/models.py @@ -157,6 +157,7 @@ class Exposition(TranslatableModel, EventMixin, ExpoMixin): registration_payment = models.PositiveIntegerField(verbose_name='Регистрационный взнос', blank=True, null=True) paid_new = models.ForeignKey('expobanner.Paid', blank=True, null=True, on_delete=models.SET_NULL) + top = models.ForeignKey('expobanner.Top', blank=True, null=True, on_delete=models.SET_NULL) #set manager of this model(fisrt manager is default) objects = ExpoManager() enable = ClientManager() diff --git a/templates/admin/expobanner/top_create.html b/templates/admin/expobanner/top_create.html new file mode 100644 index 00000000..4479d055 --- /dev/null +++ b/templates/admin/expobanner/top_create.html @@ -0,0 +1,84 @@ +{% extends 'base.html' %} +{% load static %} + +{% block scripts %} +{# selects #} + + + +{% endblock %} + +{% block body %} +
{% csrf_token %} +
+
+
+

{{ form.verbose }}

+
+
+ {% for field in form %} +
+ +
{{ field }} + {{ field.errors }} +
+
+ {% endfor %} +
+
+
+ +
+ + +
+
+{% endblock %} \ No newline at end of file diff --git a/templates/admin/expobanner/top_list.html b/templates/admin/expobanner/top_list.html new file mode 100644 index 00000000..78642de7 --- /dev/null +++ b/templates/admin/expobanner/top_list.html @@ -0,0 +1,36 @@ +{% extends 'base.html' %} + +{% block body %} + +
+
+

Список выставок в топе

+
+
+ {% block list_table %} + Добавить выставку + + + + + + + + + + {% for item in object_list %} + + + + + + {% endfor %} + +
Выставка  
{{ item }}Изменить Статистика
+ {% endblock %} +
+ {# pagination #} + {% include 'admin/includes/admin_pagination.html' with page_obj=object_list %} +
+ +{% endblock %} \ No newline at end of file diff --git a/templates/admin/includes/admin_nav.html b/templates/admin/includes/admin_nav.html index ac114c8b..59a41ebf 100644 --- a/templates/admin/includes/admin_nav.html +++ b/templates/admin/includes/admin_nav.html @@ -110,6 +110,7 @@ diff --git a/templates/client/includes/banners/tops.html b/templates/client/includes/banners/tops.html new file mode 100644 index 00000000..0a7d4176 --- /dev/null +++ b/templates/client/includes/banners/tops.html @@ -0,0 +1,13 @@ +
+ +
+ \ No newline at end of file diff --git a/templates/client/includes/exposition/expo_top.html b/templates/client/includes/exposition/expo_top.html new file mode 100644 index 00000000..639c0425 --- /dev/null +++ b/templates/client/includes/exposition/expo_top.html @@ -0,0 +1,102 @@ +{% load static %} +{% load i18n %} +{% load template_filters %} + +
    + + {% for obj in objects %} +
  • +
    + {% if not obj.canceled %} + + {% if obj.expohit %} +
    + {% endif %} +
    + {% with obj=obj %} + {% include 'client/includes/show_logo.html' %} + {% endwith %} +
    +
    + + {% else %} +
    +
    + {% with obj=obj %} + {% include 'client/includes/show_logo.html' %} + {% endwith %} +
    + {% endif %} +
    +
    + {% if obj.quality_label.ufi.is_set %} +
    + +
    + {% endif %} +
    + +
    +
    + {{ obj.main_title|safe }} +
    +
    +
    +
    + {% with obj=obj %} + {% include 'client/includes/show_date_block.html' %} + {% endwith %} +
    + {% if obj.country %} +
    + {{ obj.country }}, {{ obj.city }} + {% if obj.place %} + , {{ obj.place }} + {% endif %} +
    + {% endif %} +
    +
    +
    +
    + {% include 'client/includes/exposition/services.html' with obj=obj %} + {% include 'client/includes/calendar_button.html' with obj=obj%} +
    + {% with note=obj|note_by_user:request.user %} + {% trans 'заметка' %} +
    +
    + +
    +
    + {% endwith %} +
    + {% if request.user.is_admin %} + + {% endif %} +
    +
    +
    + {% include 'client/buttons/booking_button.html' with object=obj %} +
    +
    +
    +
    +
    + {% if obj.visitors %} + {{ obj.visitors }} + {% endif %} + {% if obj.members %} + {{ obj.members }} + {% endif %} +
    +
    + {% include 'client/includes/exposition/tags.html' with obj=obj %} +
    +
    +
  • + {% endfor %} + +
diff --git a/templates/client/includes/exposition/exposition_list.html b/templates/client/includes/exposition/exposition_list.html index 9b0dc718..5e73135e 100644 --- a/templates/client/includes/exposition/exposition_list.html +++ b/templates/client/includes/exposition/exposition_list.html @@ -3,7 +3,8 @@ {% load template_filters %} {% with objects=object_list %} {% if objects %} -
    + {% include 'client/includes/banners/tops.html' %} +
      {% for obj in objects %}