From 897b23b59274cd72453824704dd6aee7d37f9246 Mon Sep 17 00:00:00 2001 From: Kotiuk Nazarii Date: Tue, 1 Sep 2015 15:34:49 +0300 Subject: [PATCH 1/7] Banners. pre js deployment --- expobanner/admin.py | 75 +++++- expobanner/admin_urls.py | 13 +- expobanner/forms.py | 92 +++++++- expobanner/management/__init__.py | 1 + expobanner/management/commands/__init__.py | 1 + .../commands/banner_log_check_previous_day.py | 42 ++++ .../management/commands/banner_log_update.py | 67 ++++++ expobanner/managers.py | 4 + expobanner/mixins.py | 3 + expobanner/models.py | 74 +++++- expobanner/stat_views.py | 57 +++++ expobanner/urls.py | 9 +- expobanner/utils.py | 73 ++++++ expobanner/views.py | 122 +++------- exposition/models.py | 2 + exposition/views.py | 18 +- functions/admin_views.py | 5 +- functions/model_mixin.py | 3 + meta/models.py | 4 +- proj/settings.py | 24 +- templates/admin/expobanner/banner_list.html | 36 +++ templates/admin/expobanner/banner_stat.html | 43 ++++ templates/admin/expobanner/paid_create.html | 71 ++++++ templates/admin/expobanner/paid_list.html | 38 +++ templates/admin/expobanner/paid_stat.html | 43 ++++ templates/admin/expobanner/paid_update.html | 32 +++ templates/admin/includes/admin_nav.html | 1 + templates/client/base_catalog.html | 7 +- templates/client/blank.html | 48 +--- templates/client/expobanners/banner_stat.html | 67 ++++++ templates/client/expobanners/paid_stat.html | 67 ++++++ .../client/exposition/exposition_detail.html | 6 +- .../client/exposition/exposition_price.html | 219 ------------------ .../client/includes/banners/aside_1.html | 4 + .../client/includes/banners/aside_2.html | 4 + .../client/includes/banners/aside_3.html | 4 + .../client/includes/banners/aside_4.html | 4 + .../includes/banners/catalog_inner.html | 3 + .../client/includes/banners/detail_inner.html | 3 + templates/client/includes/banners/header.html | 3 + .../client/includes/banners/search_under.html | 3 + .../client/includes/exposition/expo_paid.html | 10 +- .../includes/exposition/exposition_list.html | 29 +-- .../exposition/exposition_object.html | 24 +- .../client/includes/exposition/price.html | 19 +- templates/client/includes/header.html | 15 +- 46 files changed, 1016 insertions(+), 476 deletions(-) create mode 100644 expobanner/management/__init__.py create mode 100644 expobanner/management/commands/__init__.py create mode 100644 expobanner/management/commands/banner_log_check_previous_day.py create mode 100644 expobanner/management/commands/banner_log_update.py create mode 100644 expobanner/mixins.py create mode 100644 expobanner/stat_views.py create mode 100644 expobanner/utils.py create mode 100644 templates/admin/expobanner/banner_list.html create mode 100644 templates/admin/expobanner/banner_stat.html create mode 100644 templates/admin/expobanner/paid_create.html create mode 100644 templates/admin/expobanner/paid_list.html create mode 100644 templates/admin/expobanner/paid_stat.html create mode 100644 templates/admin/expobanner/paid_update.html create mode 100644 templates/client/expobanners/banner_stat.html create mode 100644 templates/client/expobanners/paid_stat.html delete mode 100644 templates/client/exposition/exposition_price.html create mode 100644 templates/client/includes/banners/aside_1.html create mode 100644 templates/client/includes/banners/aside_2.html create mode 100644 templates/client/includes/banners/aside_3.html create mode 100644 templates/client/includes/banners/aside_4.html create mode 100644 templates/client/includes/banners/catalog_inner.html create mode 100644 templates/client/includes/banners/detail_inner.html create mode 100644 templates/client/includes/banners/header.html create mode 100644 templates/client/includes/banners/search_under.html diff --git a/expobanner/admin.py b/expobanner/admin.py index d1273215..1625121c 100644 --- a/expobanner/admin.py +++ b/expobanner/admin.py @@ -1,8 +1,12 @@ # -*- coding: utf-8 -*- -from django.views.generic import TemplateView, CreateView, ListView, UpdateView +from django.views.generic import TemplateView, CreateView, ListView, UpdateView, DetailView from django.conf import settings -from expobanner.models import URL, BannerGroup, Banner -from expobanner.forms import UrlCreateForm, BannerCreateGroupForm, BannerCreateForm, BannerGroupUpdateForm +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 +from exposition.models import Exposition class BannersControl(TemplateView): @@ -52,6 +56,12 @@ class BannerGroupList(AbstractList): class BannerList(AbstractList): model = Banner verbose = u'Список банеров' + template_name = 'admin/expobanner/banner_list.html' + + def get_queryset(self): + qs = super(BannerList, self).get_queryset() + qs = qs.filter(group__isnull=False) + return qs # UPDATE VIEWS class AbstractUpdate(UpdateView): @@ -71,4 +81,61 @@ class BannerGroupUpdate(AbstractUpdate): class BannerUpdate(AbstractUpdate): model = Banner - form_class = BannerCreateForm \ No newline at end of file + form_class = BannerCreateForm + + +class BannerStat(DetailView): + model = Banner + template_name = 'admin/expobanner/banner_stat.html' + +class PaidList(ListView): + model = Exposition + template_name = 'admin/expobanner/paid_list.html' + paginate_by = settings.ADMIN_PAGINATION + + def get_queryset(self): + return self.model.objects.language().filter(paid_new__isnull=False) + +class PaidCreate(CreateView): + form_class = PaidCreateForm + template_name = 'admin/expobanner/paid_create.html' + success_url = '/admin/expobanners/paid/list/' + +class PaidUpdate(UpdateView): + model = Paid + form_class = PaidUpdateForm + template_name = 'admin/expobanner/paid_update.html' + success_url = '/admin/expobanners/paid/list/' + + def get_initial(self): + """ + Returns the initial data to use for forms on this view. + """ + initial = super(PaidUpdate, self).get_initial() + obj = self.object + initial['tickets'] = obj.tickets.url + initial['participation'] = obj.participation.url + initial['official'] = obj.official.url + + return initial + + def get_context_data(self, **kwargs): + context = super(PaidUpdate, self).get_context_data(**kwargs) + obj = self.object + context['exposition'] = obj.get_event() + return context + + +def paid_turn(request, pk, status): + paid = get_object_or_404(Paid, pk=pk) + if status == 'on': + paid.public = True + else: + paid.public = False + paid.save() + return HttpResponseRedirect('/admin/expobanners/paid/list/') + + +class PaidStat(DetailView): + model = Paid + template_name = 'admin/expobanner/paid_stat.html' \ No newline at end of file diff --git a/expobanner/admin_urls.py b/expobanner/admin_urls.py index db31823e..c03071da 100644 --- a/expobanner/admin_urls.py +++ b/expobanner/admin_urls.py @@ -3,19 +3,22 @@ from django.conf.urls import patterns, url from expobanner.admin import * urlpatterns = patterns('expobanner.admin', + # banners url(r'^banners/control/$', BannersControl.as_view(), name='expobanner-baneers_control'), - url(r'^banners/url/$', CreateUrl.as_view(), name='expobanner-create_url'), url(r'^banners/group/$', CreateBannerGroup.as_view(), name='expobanner-create_group'), url(r'^banners/banner/$', CreateBanner.as_view(), name='expobanner-create_banner'), - url(r'^banners/url/list/$', UrlList.as_view(), name='expobanner-list_url'), url(r'^banners/group/list/$', BannerGroupList.as_view(), name='expobanner-list_group'), url(r'^banners/banner/list/$', BannerList.as_view(), name='expobanner-list_banner'), - url(r'^banners/url/(?P\d+)/edit/$', UrlUpdate.as_view(), name='expobanner-update_url'), url(r'^banners/group/(?P\d+)/edit/$', BannerGroupUpdate.as_view(), name='expobanner-update_group'), url(r'^banners/banner/(?P\d+)/edit/$', BannerUpdate.as_view(), name='expobanner-update_banner'), - - + url(r'^banners/banner/(?P\d+)/stat/$', BannerStat.as_view(), name='expobanner_stat_banner'), + # paid + url(r'^paid/list/$', PaidList.as_view(), name='expobanner-list_paid'), + url(r'^paid/(?P\d+)/edit/$', PaidUpdate.as_view(), name='expobanner-update_paid'), + 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'), ) \ No newline at end of file diff --git a/expobanner/forms.py b/expobanner/forms.py index 82bfd39e..8bb9e0cf 100644 --- a/expobanner/forms.py +++ b/expobanner/forms.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from django import forms -from expobanner.models import URL, BannerGroup, Banner +from expobanner.models import URL, BannerGroup, Banner, Paid +from exposition.models import Exposition from country.models import Country from city.models import City from theme.models import Theme, Tag @@ -34,7 +35,92 @@ class BannerCreateForm(forms.ModelForm): #city = forms.CharField(label=u'Город', widget=forms.HiddenInput(), required=False) #tag = forms.CharField(label=u'Тег', widget=forms.HiddenInput(), required=False) - class Meta: model = Banner - exclude = ['created_at', 'updated_at', 'often', 'paid'] \ No newline at end of file + exclude = ['created_at', 'updated_at', 'often', 'paid', 'stat_pswd'] + + +class ClientStatForm(forms.Form): + stat_pswd = forms.CharField(label=u'Введите пароль:') + + def check_pass(self, obj): + pswd = self.cleaned_data['stat_pswd'] + return obj.stat_pswd == pswd + + +class PaidCreateForm(forms.ModelForm): + verbose = u'Создать проплаченую выставку' + exposition = forms.CharField(label=u'Выставка', widget=forms.HiddenInput()) + tickets = forms.URLField(label=u'Линк на билеты') + participation = forms.URLField(label=u'Линк на участие') + official = forms.URLField(label=u'Линк на официальный сайт') + + class Meta: + model = Paid + fields = ['logo', 'organiser', 'public'] + + def save(self, commit=True): + paid = super(PaidCreateForm, self).save(commit=False) + if commit: + expo = self.cleaned_data['exposition'] + tickets = self.cleaned_data['tickets'] + tickets_link = Banner.objects.create_for_paid(expo, tickets, 'tickets') + participation = self.cleaned_data['participation'] + participation_link = Banner.objects.create_for_paid(expo, participation, 'participation') + official = self.cleaned_data['official'] + official_link = Banner.objects.create_for_paid(expo, official, 'official') + catalog = expo.get_permanent_url() + catalog_link = Banner.objects.create_for_paid(expo, catalog, 'catalog') + + + paid.tickets = tickets_link + paid.participation = participation_link + paid.official = official_link + paid.catalog = catalog_link + paid.save() + + expo.paid_new = paid + expo.save() + return paid + + 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 + +class PaidUpdateForm(forms.ModelForm): + tickets = forms.URLField(label=u'Линк на билеты') + participation = forms.URLField(label=u'Линк на участие') + official = forms.URLField(label=u'Линк на официальный сайт') + + class Meta: + model = Paid + fields = ['logo', 'organiser', 'public'] + + def save(self, commit=True): + paid = super(PaidUpdateForm, self).save(commit=False) + if commit: + tickets = self.cleaned_data['tickets'] + b_tickets = paid.tickets + if tickets != b_tickets.url: + b_tickets.url = tickets + b_tickets.save() + + participation = self.cleaned_data['participation'] + b_participation = paid.participation + if participation != b_participation.url: + b_participation.url = participation + b_participation.save() + + official = self.cleaned_data['official'] + b_official = paid.official + if official != b_official.url: + b_official.url = official + b_official.save() + + paid.save() + + return paid \ No newline at end of file diff --git a/expobanner/management/__init__.py b/expobanner/management/__init__.py new file mode 100644 index 00000000..13ef41a7 --- /dev/null +++ b/expobanner/management/__init__.py @@ -0,0 +1 @@ +__author__ = 'kotzilla' diff --git a/expobanner/management/commands/__init__.py b/expobanner/management/commands/__init__.py new file mode 100644 index 00000000..13ef41a7 --- /dev/null +++ b/expobanner/management/commands/__init__.py @@ -0,0 +1 @@ +__author__ = 'kotzilla' diff --git a/expobanner/management/commands/banner_log_check_previous_day.py b/expobanner/management/commands/banner_log_check_previous_day.py new file mode 100644 index 00000000..3a6a1f10 --- /dev/null +++ b/expobanner/management/commands/banner_log_check_previous_day.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +from datetime import date, timedelta +from django.core.management.base import BaseCommand +from expobanner.models import Log, LogStat, Banner +from django.conf import settings + + +class Command(BaseCommand): + def handle(self, *args, **options): + prev_day = date.today() - timedelta(days=1) + for banner in Banner.objects.select_related('group').filter(): + try: + logstat = LogStat.objects.get(banner=banner, group=banner.group, date=prev_day) + except LogStat.DoesNotExist: + logstat = LogStat(banner=banner, group=banner.group, date=prev_day) + + views = Log.objects.filter(datetime__startswith=prev_day, + banner=banner, + group=banner.group, + type=1).count() + clicks = Log.objects.filter(datetime__startswith=prev_day, + banner=banner, + group=banner.group, + type=2).count() + unique_views = Log.objects.filter(datetime__startswith=prev_day, + banner=banner, + group=banner.group, + type=1).values('ip').distinct().count() + unique_clicks = Log.objects.filter(datetime__startswith=prev_day, + banner=banner, + group=banner.group, + type=2).values('ip').distinct().count() + + if not logstat.click or logstat.click < clicks: + logstat.click = clicks + if not logstat.view or logstat.view < views: + logstat.view = views + if not logstat.unique_click or logstat.unique_click < unique_clicks: + logstat.unique_click = unique_clicks + if not logstat.unique_view or logstat.unique_view < unique_views: + logstat.unique_view = unique_views + logstat.save() diff --git a/expobanner/management/commands/banner_log_update.py b/expobanner/management/commands/banner_log_update.py new file mode 100644 index 00000000..86cfb920 --- /dev/null +++ b/expobanner/management/commands/banner_log_update.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +from datetime import date +from django.core.management.base import BaseCommand +from expobanner.models import Log, LogStat, Banner, PaidStat +from exposition.models import Exposition + + +class Command(BaseCommand): + def handle(self, *args, **options): + today = date.today() + # banners + for banner in Banner.objects.select_related('group').filter(public=True, group__isnull=False): + try: + logstat = LogStat.objects.get(banner=banner, group=banner.group, date=today) + except LogStat.DoesNotExist: + logstat = LogStat(banner=banner, group=banner.group, date=today) + + views = Log.objects.filter(datetime__startswith=today, + banner=banner, + group=banner.group, + type=1).count() + clicks = Log.objects.filter(datetime__startswith=today, + banner=banner, + group=banner.group, + type=2).count() + unique_views = Log.objects.filter(datetime__startswith=today, + banner=banner, + group=banner.group, + type=1).values('ip').distinct().count() + unique_clicks = Log.objects.filter(datetime__startswith=today, + banner=banner, + group=banner.group, + type=2).values('ip').distinct().count() + logstat.click = clicks + logstat.view = views + logstat.unique_click = unique_clicks + logstat.unique_view = unique_views + logstat.save() + + # paid expos + expos = list(Exposition.objects.select_related('paid_new').filter(paid_new__isnull=False)) + for expo in expos: + paid = expo.paid_new + try: + paidstat = PaidStat.objects.get(paid=paid, date=today) + except PaidStat.DoesNotExist: + paidstat = PaidStat(paid=paid, date=today) + + t_clicks = Log.objects.filter(datetime__startswith=today, banner=paid.tickets, type=2).count() + p_clicks = Log.objects.filter(datetime__startswith=today, banner=paid.participation, type=2).count() + o_clicks = Log.objects.filter(datetime__startswith=today, banner=paid.official, type=2).count() + c_clicks = Log.objects.filter(datetime__startswith=today, banner=paid.catalog, type=2).count() + + paidstat.tickets_clicks = t_clicks + paidstat.participation_clicks = p_clicks + paidstat.official_clicks = o_clicks + paidstat.catalog_clicks = c_clicks + + paidstat.save() + + + + + + + + diff --git a/expobanner/managers.py b/expobanner/managers.py index 0735f53c..dfc42384 100644 --- a/expobanner/managers.py +++ b/expobanner/managers.py @@ -23,6 +23,10 @@ class BiasedManager(models.Manager): shuffle(result) return result + def create_for_paid(self, expo, url, role): + alt = u'%s_%s'%(expo.name, role) + return self.create(alt=alt, url=url, paid=True) + class BannerGroupCached(models.Manager): def all(self): key = 'banner_group_all' diff --git a/expobanner/mixins.py b/expobanner/mixins.py new file mode 100644 index 00000000..9520ab37 --- /dev/null +++ b/expobanner/mixins.py @@ -0,0 +1,3 @@ +class StatMixin(object): + def get_cookie_name(self): + return u'%s_%d'%(self._meta.db_table, self.id) \ No newline at end of file diff --git a/expobanner/models.py b/expobanner/models.py index b4ceef75..436abfa5 100644 --- a/expobanner/models.py +++ b/expobanner/models.py @@ -1,11 +1,14 @@ # -*- coding: utf-8 -*- +import random import hashlib from datetime import datetime, date from django.db import models 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 .mixins import StatMixin from theme.models import Theme from country.models import Country @@ -64,7 +67,7 @@ class BannerGroup (models.Model): return '/admin/expobanners/banners/group/%d/edit/'%self.id -class Banner(models.Model): +class Banner(models.Model, StatMixin): objects = BiasedManager() title = models.CharField(verbose_name=u'Заголовок', max_length=255, blank=True) @@ -92,13 +95,18 @@ class Banner(models.Model): html = models.BooleanField(verbose_name=_('HTML?'), default=False) flash = models.BooleanField(verbose_name=_('Flash?'), default=False) + js = models.BooleanField(verbose_name=_('Javascript?'), default=False) paid = models.BooleanField(verbose_name=_('Is Paid event link?'), default=False) - public = models.BooleanField(verbose_name=u'Активный', default=True) created_at = models.DateTimeField(verbose_name=_('Created At'), auto_now_add=True) updated_at = models.DateTimeField(verbose_name=_('Updated At'), auto_now=True) + stat_pswd = models.CharField(max_length=16) + + class Meta: + ordering = ['-public'] + def get_admin_url(self): return '/admin/expobanners/banners/banner/%d/edit/'%self.id @@ -110,10 +118,9 @@ class Banner(models.Model): key = str(datetime.now()) return hashlib.md5(key).hexdigest() - def log(self, request, type, key): + def log(self, request, type): log = { 'type': type, - 'key': key, 'banner': self, 'group': self.group, 'ip': request.META.get('REMOTE_ADDR'), @@ -150,6 +157,9 @@ class Banner(models.Model): return ('banner_click', (), {'banner_id': self.pk, 'key': self.key()}) return get_absolute_url(self) + def get_click_link(self): + return '/expo-b/click/%d/'%self.id + class Meta: ordering = ['sort'] verbose_name = _('Banner') @@ -158,7 +168,7 @@ class Banner(models.Model): class Log(models.Model): banner = models.ForeignKey(Banner, related_name='banner_logs') - group = models.ForeignKey(BannerGroup, related_name='group_logs', verbose_name=_('Group'), blank=True) + group = models.ForeignKey(BannerGroup, related_name='group_logs', verbose_name=_('Group'), blank=True, null=True) urls = models.ManyToManyField(URL, related_name='url_logs', verbose_name=_('URLs'), blank=True) user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, related_name='users', verbose_name=_('User')) @@ -181,7 +191,7 @@ class Log(models.Model): class LogStat(models.Model): banner = models.ForeignKey(Banner, related_name='banner_stat', verbose_name=_('Banner'), blank=True) - group = models.ForeignKey(BannerGroup, related_name='group_stat', verbose_name=_('Group'), blank=True) + group = models.ForeignKey(BannerGroup, related_name='group_stat', verbose_name=_('Group'), blank=True, null=True) urls = models.ManyToManyField(URL, related_name='url_bloks', verbose_name=_('URLs'), null=True, blank=True) date = models.DateField(verbose_name=_('Data')) @@ -195,17 +205,26 @@ class LogStat(models.Model): # ------------------ -class Paid(models.Model): +class Paid(models.Model, StatMixin): tickets = models.ForeignKey(Banner, related_name='paid_tickets') participation = models.ForeignKey(Banner, related_name='paid_participation') official = models.ForeignKey(Banner, related_name='paid_official') - logo = models.ImageField(upload_to='/')# !!!!! - organiser = models.CharField(max_length=100) - active = models.BooleanField(default=True) + catalog = models.ForeignKey(Banner, related_name='paid_catalog') + logo = models.ImageField(upload_to='expo-b/paid', blank=True) + organiser = models.CharField(max_length=100, blank=True) + public = models.BooleanField(default=True, verbose_name=u'Активная') stat_pswd = models.CharField(max_length=16) created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) + class Meta: + ordering = ['-public'] + + def get_event(self): + if self.exposition_set.all().exists(): + return self.exposition_set.all()[0] + return None + class PaidStat(models.Model): paid = models.ForeignKey(Paid) @@ -214,9 +233,12 @@ class PaidStat(models.Model): price_views = models.PositiveIntegerField(default=0) catalog_views = models.PositiveIntegerField(default=0) catalog_clicks = models.PositiveIntegerField(default=0) + tickets_clicks = models.PositiveIntegerField(default=0) + participation_clicks = models.PositiveIntegerField(default=0) + official_clicks = models.PositiveIntegerField(default=0) -class Top(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) @@ -238,4 +260,32 @@ class TopStat(models.Model): country = models.ForeignKey('country.Country', blank=True, null=True) city = models.ForeignKey('city.City', blank=True, null=True) views = models.PositiveIntegerField(default=0) - clicks = models.PositiveIntegerField(default=0) \ No newline at end of file + clicks = models.PositiveIntegerField(default=0) + +def generatePassword(length=5): + """ + generate random password + """ + SYMBOLS = [',', '.', '?', '!', '-', '+', '1', '2', '3', '4', '5', '6', '7', '8', + '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', + 'm', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', + 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '#'] + PASSWORD_LENGTH = length + newPassword = [] + for i in range(PASSWORD_LENGTH): + newPassword.append(SYMBOLS[random.randrange(0, len(SYMBOLS))]) + return ''.join(newPassword) + + +def generate_stat_pass(sender, **kwargs): + obj = kwargs['instance'] + if not obj.stat_pswd: + obj.stat_pswd = generatePassword() + obj.save() + + + +post_save.connect(generate_stat_pass, sender=Banner) +post_save.connect(generate_stat_pass, sender=Paid) +post_save.connect(generate_stat_pass, sender=Top) diff --git a/expobanner/stat_views.py b/expobanner/stat_views.py new file mode 100644 index 00000000..a1ac7ae7 --- /dev/null +++ b/expobanner/stat_views.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +from django.http import HttpResponseRedirect +from django.views.generic import TemplateView, CreateView, ListView, UpdateView, DetailView +from django.views.generic.edit import FormMixin +from django.forms.util import ErrorList +from django.conf import settings +from django.core.urlresolvers import reverse +from expobanner.models import Banner, Paid +from expobanner.forms import ClientStatForm + + + +class BannerStat(FormMixin, DetailView): + model = Banner + form_class = ClientStatForm + template_name = 'client/expobanners/banner_stat.html' + + def get_success_url(self): + return reverse('banner_stat_client', kwargs={'pk': self.object.pk}) + + def get_context_data(self, **kwargs): + context = super(BannerStat, self).get_context_data(**kwargs) + obj = self.object + cookie_name = obj.get_cookie_name() + cookie = self.request.session.get(cookie_name) + if not cookie: + form = context.get('form') + # form in context if form invalid called + if not form: + context['form'] = self.get_form(self.form_class) + + return context + + def post(self, request, *args, **kwargs): + self.object = self.get_object() + form = self.get_form(self.form_class) + if form.is_valid(): + return self.form_valid(form) + else: + return self.form_invalid(form) + + def form_valid(self, form): + obj = self.object + success = form.check_pass(obj) + if success: + self.request.session[obj.get_cookie_name()] = 1 + return HttpResponseRedirect(self.get_success_url()) + else: + form.errors['stat_pswd'] = ErrorList([u'Неправильный пароль']) + return self.form_invalid(form) + +class PaidStat(BannerStat): + model = Paid + template_name = 'client/expobanners/paid_stat.html' + + def get_success_url(self): + return reverse('paid_stat_client', kwargs={'pk': self.object.pk}) \ No newline at end of file diff --git a/expobanner/urls.py b/expobanner/urls.py index 4371bc5f..801aece7 100644 --- a/expobanner/urls.py +++ b/expobanner/urls.py @@ -1,11 +1,12 @@ from django.conf.urls import url - +from expobanner.stat_views import * from . import views urlpatterns = [ - url(r'^click/(?P\d{1,4})/(?P[-\w]+)/$', views.click, name='banner_click'), - url(r'^view/(?P\d+)/(?P[-\w]+)/$', views.view, name='banner_view'), - + url(r'^click/(?P\d{1,4})/$', views.click, name='banner_click'), + #url(r'^view/(?P\d+)/$', views.view, name='banner_view'), # url(r'^get-banners/$', views.get_banners), + 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 new file mode 100644 index 00000000..50e2f2c6 --- /dev/null +++ b/expobanner/utils.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- +import random +from django.db import connection + +def get_client_ip(request): + x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') + if x_forwarded_for: + ip = x_forwarded_for.split(',')[0] + else: + ip = request.META.get('REMOTE_ADDR') + return ip + +def get_by_sort(banner_list): + max_sort = 0 + for banner in banner_list: + sort = banner.sort + if sort > max_sort: + max_sort = sort + result = [banner for banner in banner_list if banner.sort == max_sort] + return result + + +def get_banner_by_params(banners_list, urls, params): + print('START. NUMBER of queries = %d'%len(connection.queries)) + thematic_banners = [] + url_banners = [] + + for banner in banners_list: + #print('-------------------------') + #print('number of queries = %d'%len(connection.queries)) + + # check by theme + banner_theme_ids = [str(theme.id) for theme in banner.theme.all()] + #print('number of queries = %d'%len(connection.queries)) + + if banner_theme_ids: + if params.get('theme'): + theme = params['theme'] + if theme in banner_theme_ids: + thematic_banners.append(banner) + continue + # check by country + banner_country_ids = [str(country.id) for country in banner.country.all()] + #print('number of queries = %d'%len(connection.queries)) + if banner_country_ids: + if params.get('country'): + + country = params['country'] + if country in banner_country_ids: + thematic_banners.append(banner) + continue + + # check by url + if urls: + banner_urls = banner.urls.all() + print('number of queries = %d'%len(connection.queries)) + + if banner_urls: + + banner_urls = set(banner_urls) + common_urls = set(urls).intersection(banner_urls) + + if common_urls: + url_banners.append(banner) + continue + print('-------------------------') + if thematic_banners: + return random.choice(thematic_banners) + if url_banners: + return random.choice(url_banners) + return None + + #print('END. NUMBER of queries = %d'%len(connection.queries)) diff --git a/expobanner/views.py b/expobanner/views.py index b854325b..fd2763a6 100644 --- a/expobanner/views.py +++ b/expobanner/views.py @@ -1,97 +1,27 @@ # -*- coding: utf-8 -*- import json import re -import random from django.http import HttpResponse from django.shortcuts import redirect, get_object_or_404 from .models import Banner, BannerGroup, URL +from expobanner.utils import get_by_sort, get_banner_by_params, get_client_ip -def click(request, banner_id, key): +def click(request, banner_id): banner = get_object_or_404(Banner, pk=banner_id) - banner.log(request, 2, key) + banner.log(request, 2) return redirect(banner.url) -def view(request, banner_id, key): +def view(request, banner_id): banner = get_object_or_404(Banner, pk=banner_id) - banner.log(request, 1, key) + banner.log(request, 1) return redirect(banner.img.url) - -def get_client_ip(request): - x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') - if x_forwarded_for: - ip = x_forwarded_for.split(',')[0] - else: - ip = request.META.get('REMOTE_ADDR') - return ip - -def get_by_sort(banner_list): - max_sort = 0 - for banner in banner_list: - sort = banner.sort - if sort > max_sort: - max_sort = sort - result = [banner for banner in banner_list if banner.sort == max_sort] - return result - -from django.db import connection -def get_banner_by_params(banners_list, urls, params): - print('START. NUMBER of queries = %d'%len(connection.queries)) - good_banners = [] - - for banner in banners_list: - print('-------------------------') - print('number of queries = %d'%len(connection.queries)) - print(banner) - # check by theme - banner_theme_ids = [str(theme.id) for theme in banner.theme.all()] - print('number of queries = %d'%len(connection.queries)) - - if banner_theme_ids: - if params.get('theme'): - theme = params['theme'] - if theme in banner_theme_ids: - good_banners.append(banner) - continue - # check by country - banner_country_ids = [str(country.id) for country in banner.country.all()] - print('number of queries = %d'%len(connection.queries)) - if banner_country_ids: - if params.get('country'): - - country = params['country'] - if country in banner_country_ids: - good_banners.append(banner) - continue - - # check by url - if urls: - banner_urls = banner.urls.all() - print('number of queries = %d'%len(connection.queries)) - - if banner_urls: - - banner_urls = set(banner_urls) - common_urls = set(urls).intersection(banner_urls) - - if common_urls: - good_banners.append(banner) - continue - print('-------------------------') - good_banners = get_by_sort(good_banners) - - print('END. NUMBER of queries = %d'%len(connection.queries)) - - - if good_banners: - return random.choice(good_banners) - return [] - def get_banners(request): + #url = request.GET.get('url', '/') + url = request.META.get('HTTP_REFERER', '/') # get urls by current url - url = request.GET.get('url', '/') urls = URL.cached.all() good_urls = [] for u in urls: @@ -110,17 +40,31 @@ def get_banners(request): group_banners = BannerGroup.cached.group_banners() result = [] + # get banners for all groups for group, banners in group_banners.iteritems(): banner = get_banner_by_params(banners, good_urls, params) - result.append({'id': group, - 'url': banner.url, - 'is_html': banner.html, - 'is_flash': banner.flash, - 'is_img': True, - 'html': banner.text, - 'img': banner.img.url - }) - - return HttpResponse(json.dumps(result, indent=4), content_type='application/json') - - + if banner: + if banner.js or banner.html: + text = banner.text + img = '' + alt = '' + is_img = False + else: + text = '' + img = banner.img.url + alt = banner.alt + is_img = True + result.append({'id': group, + 'url': banner.get_click_link(), + 'is_html': banner.html, + 'is_flash': banner.flash, + 'is_img': is_img, + 'is_js': banner.js, + 'img': img, + 'alt': alt, + 'text': text + }) + # add view log + banner.log(request, 1) + + return HttpResponse(json.dumps(result, indent=4), content_type='application/json') \ No newline at end of file diff --git a/exposition/models.py b/exposition/models.py index dce42537..ed34bff5 100644 --- a/exposition/models.py +++ b/exposition/models.py @@ -155,6 +155,8 @@ class Exposition(TranslatableModel, EventMixin, ExpoMixin): max_open_area = models.PositiveIntegerField(verbose_name='Максимальная цена открытой площади', blank=True, null=True) 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) #set manager of this model(fisrt manager is default) objects = ExpoManager() enable = ClientManager() diff --git a/exposition/views.py b/exposition/views.py index 48b55b3b..b6464599 100644 --- a/exposition/views.py +++ b/exposition/views.py @@ -151,6 +151,7 @@ class ExpoDetail(JitterCacheMixin, MetadataMixin, DetailView): model = Exposition slug_field = 'url' template_name = 'client/exposition/exposition_detail.html' + queryset = Exposition.objects.language().select_related('place', 'city', 'country', 'paid_new') def get_context_data(self, **kwargs): context = super(ExpoDetail, self).get_context_data(**kwargs) @@ -167,6 +168,7 @@ class ExpositionPrice(MetadataMixin, DetailView): model = Exposition slug_field = 'url' template_name = 'client/exposition/price.html' + queryset = Exposition.objects.language().select_related('place', 'city', 'country', 'paid_new') @@ -375,6 +377,11 @@ class ExpoCountryCatalog(ExpoCatalog): qs = self.model.enable.upcoming().filter(country=country) return qs + def get_context_data(self, **kwargs): + context = super(ExpoCountryCatalog, self).get_context_data(**kwargs) + context['country'] = str(self.kwargs['country'].id) + return context + class ExpoCityCatalog(ExpoCatalog): catalog_url = '/expo/city/' def get_filtered_qs(self): @@ -385,6 +392,10 @@ class ExpoCityCatalog(ExpoCatalog): qs = self.model.enable.upcoming().filter(city=city) self.filter_object = city return qs + def get_context_data(self, **kwargs): + context = super(ExpoCityCatalog, self).get_context_data(**kwargs) + context['city'] = str(self.kwargs['city'].id) + return context class ExpoThemeCatalog(ExpoCatalog): @@ -423,10 +434,11 @@ class ExpoThemeCatalog(ExpoCatalog): def get_context_data(self, **kwargs): context = super(ExpoThemeCatalog, self).get_context_data(**kwargs) if self.country: - context['country'] = self.country + context['country'] = str(self.country.id) if self.city: - context['city'] = self.city + context['city'] = str(self.city.id) context['theme_for_filter'] = self.kwargs['theme'] + context['themes'] = [str(self.kwargs['theme'].id)] return context @@ -449,6 +461,8 @@ class ExpoTagCatalog(ExpoCatalog): context = super(ExpoTagCatalog, self).get_context_data(**kwargs) tag = self.kwargs['tag'] context['theme_for_filter'] = tag.theme + context['themes'] = [str(tag.theme.id)] + context['tag'] = str(self.kwargs['tag'].id) return context diff --git a/functions/admin_views.py b/functions/admin_views.py index cb2f18a2..0fb38183 100644 --- a/functions/admin_views.py +++ b/functions/admin_views.py @@ -126,10 +126,7 @@ class AdminListView(FormView): def get_context_data(self, **kwargs): context = super(AdminListView, self).get_context_data(**kwargs) - if hasattr(self.model,'user'): - qs = self.model.objects.language().order_by('user__first_name') - else: - qs = self.model.objects.order_by('first_name') + qs = self.model.objects.language().all().order_by('name') result = paginate_results(qs, page=self.request.GET.get('page')) context['object_list'] = result return context diff --git a/functions/model_mixin.py b/functions/model_mixin.py index a96c53cc..5577ac9c 100644 --- a/functions/model_mixin.py +++ b/functions/model_mixin.py @@ -34,6 +34,9 @@ class EventMixin(object): 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(';') diff --git a/meta/models.py b/meta/models.py index c3be90d8..0bd6bdc0 100644 --- a/meta/models.py +++ b/meta/models.py @@ -127,9 +127,7 @@ from django.core.cache import cache class SeoTextManager(TranslationManager): cache_time = 120 - def cache_get(self, *args, **kwargs): - # ПЕРЕРОБИТИ url = kwargs.get('url') lang = kwargs.get('lang')[:2] or translation.get_language()[:2] key = 'seo_text_cache' @@ -137,7 +135,7 @@ class SeoTextManager(TranslationManager): if result: return result.get(lang+'_' + url) - qs = SeoText.objects.language('all') + qs = list(SeoText.objects.language('all')) value_dict = {obj.language_code+'_'+obj.url:obj for obj in qs} cache.set(key, value_dict, self.cache_time) return value_dict.get(lang+'_'+url) diff --git a/proj/settings.py b/proj/settings.py index 678d9aa5..c2828869 100644 --- a/proj/settings.py +++ b/proj/settings.py @@ -352,7 +352,6 @@ INSTALLED_APPS = ( 'pytils', # ?? 'pymorphy', # ?? 'password_reset', # reset password - 'django_crontab', # crons 'social.apps.django_app.default', # social auth 'core', ) @@ -402,14 +401,21 @@ LOGGING = { } } -CRONJOBS = [ - ('0 */1 * * *', 'django.core.management.call_command', ['update_index conference --age=1']), - ('0 */1 * * *', 'django.core.management.call_command', ['update_index exposition --age=1']), - ('0 */12 * * *', 'django.core.management.call_command', ['update_index place_exposition --age=12']), - ('0 */24 * * *', 'django.core.management.call_command', ['update_index company --age=24']), - ('0 */24 * * *', 'django.core.management.call_command', ['update_index theme --age=24']), - ('0 */24 * * *', 'django.core.management.call_command', ['update_index tag --age=24']), -] +# TODO automate crons +""" +# update search indexes +0 * * * * /usr/bin/python /var/www/proj/manage.py update_index conference --remove --age=6 +0 * * * * /usr/bin/python /var/www/proj/manage.py update_index exposition --remove --age=6 +0 1,13 * * * /usr/bin/python /var/www/proj/manage.py update_index place_exposition --remove --age=24 +0 3 * * * /usr/bin/python /var/www/proj/manage.py update_index company --remove --age=48 +0 4 * * * /usr/bin/python /var/www/proj/manage.py update_index theme --remove --age=48 +0 5 * * * /usr/bin/python /var/www/proj/manage.py update_index tag --remove --age=48 +0 6 * * * /usr/bin/python /var/www/proj/manage.py update_index country --remove --age=48 +0 7 * * * /usr/bin/python /var/www/proj/manage.py update_index city --remove --age=48 +# update banner logs +10 * * * * /usr/bin/python /var/www/proj/manage.py banner_log_update +20 2,14 * * * /usr/bin/python /var/www/proj/manage.py banner_log_check_previous_day +""" THUMBNAIL_DEBUG = DEBUG diff --git a/templates/admin/expobanner/banner_list.html b/templates/admin/expobanner/banner_list.html new file mode 100644 index 00000000..2b41f228 --- /dev/null +++ b/templates/admin/expobanner/banner_list.html @@ -0,0 +1,36 @@ +{% extends 'base.html' %} + +{% block body %} + +
+
+

{{ verbose }}

+
+
+ {% 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/expobanner/banner_stat.html b/templates/admin/expobanner/banner_stat.html new file mode 100644 index 00000000..15d51bc2 --- /dev/null +++ b/templates/admin/expobanner/banner_stat.html @@ -0,0 +1,43 @@ +{% extends 'base.html' %} +{% load static %} +{% block scripts %} + +{% endblock %} + + +{% block body %} +
+
+
+

{{ object }} (Пароль: {{ object.stat_pswd }})

+
+
+ + + + + + + + + + + + {% with stats=object.banner_stat.all %} + {% for stat in stats %} + + + + + + + + {% endfor %} + {% endwith %} + +
ДатаПоказыКликиУникальные показыУникальные клики
{{ stat.date|date:"Y-m-d" }}{{ stat.view }}{{ stat.click }}{{ stat.unique_view }}{{ stat.unique_click }}
+
+
+
+ +{% endblock %} \ No newline at end of file diff --git a/templates/admin/expobanner/paid_create.html b/templates/admin/expobanner/paid_create.html new file mode 100644 index 00000000..4e3af395 --- /dev/null +++ b/templates/admin/expobanner/paid_create.html @@ -0,0 +1,71 @@ +{% 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/paid_list.html b/templates/admin/expobanner/paid_list.html new file mode 100644 index 00000000..81402128 --- /dev/null +++ b/templates/admin/expobanner/paid_list.html @@ -0,0 +1,38 @@ +{% extends 'base.html' %} + +{% block body %} + +
+
+

Список проплаченых выставок

+
+
+ {% block list_table %} + Добавить выставку + + + + + + + + + + + {% for item in object_list %} + + + + + + + {% endfor %} + +
Выставка   
{{ item }}Изменить {% if item.paid_new.public %}отключить{% else %}включить{% endif %} Статистика
+ {% 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/expobanner/paid_stat.html b/templates/admin/expobanner/paid_stat.html new file mode 100644 index 00000000..b935251c --- /dev/null +++ b/templates/admin/expobanner/paid_stat.html @@ -0,0 +1,43 @@ +{% extends 'base.html' %} +{% load static %} +{% block scripts %} + +{% endblock %} + + +{% block body %} +
+
+
+

{{ object.get_event }} (Пароль: {{ object.stat_pswd }})

+
+
+ + + + + + + + + + + + {% with stats=object.paidstat_set.all %} + {% for stat in stats %} + + + + + + + + {% endfor %} + {% endwith %} + +
ДатаОфициальный сайтБилетыУчастиеПереходы с каталога
{{ stat.date|date:"Y-m-d" }}{{ stat.official_clicks }}{{ stat.tickets_clicks }}{{ stat.participation_clicks }}{{ stat.catalog_clicks }}
+
+
+
+ +{% endblock %} \ No newline at end of file diff --git a/templates/admin/expobanner/paid_update.html b/templates/admin/expobanner/paid_update.html new file mode 100644 index 00000000..d9b82a11 --- /dev/null +++ b/templates/admin/expobanner/paid_update.html @@ -0,0 +1,32 @@ +{% extends 'base.html' %} +{% load static %} + +{% block scripts %} +{% endblock %} + +{% block body %} +
{% csrf_token %} +
+
+
+

{{ exposition }}

+
+
+ {% for field in form %} +
+ +
{{ field }} + {{ field.errors }} +
+
+ {% endfor %} +
+
+
+ +
+ + +
+
+{% endblock %} \ No newline at end of file diff --git a/templates/admin/includes/admin_nav.html b/templates/admin/includes/admin_nav.html index 9f33389b..ac114c8b 100644 --- a/templates/admin/includes/admin_nav.html +++ b/templates/admin/includes/admin_nav.html @@ -109,6 +109,7 @@ Реклама diff --git a/templates/client/base_catalog.html b/templates/client/base_catalog.html index 24827f0b..106ec884 100644 --- a/templates/client/base_catalog.html +++ b/templates/client/base_catalog.html @@ -17,6 +17,7 @@
{% include 'client/includes/online_consult.html' %} + {% include 'client/includes/banners/aside_1.html' %} {% block aside_banner1 %} {% if theme_for_filter.id == 27 or theme_for_filter.id == 9 or theme_for_filter.id == 48 %} @@ -52,15 +53,19 @@ {% endblock %} {% include 'client/includes/side_confs.html' %}
+ {% include 'client/includes/banners/aside_2.html' %} +
{% include 'client/includes/news.html' with news=news_list %}
+ {% include 'client/includes/banners/aside_3.html' %} {% block aside_vk %}
{% include 'client/includes/social_widjet.html' %}
{% endblock %} + {% include 'client/includes/banners/aside_4.html' %}
@@ -68,7 +73,7 @@ {% include 'client/includes/catalog_search.html' %} {% endwith %} {% block under_search_baner %} - {% include 'client/includes/banners/under_search.html' %} + {% include 'client/includes/banners/search_under.html' %} {% endblock %} {% block bread_scrumbs %} diff --git a/templates/client/blank.html b/templates/client/blank.html index 5c14ac22..6ffdd844 100644 --- a/templates/client/blank.html +++ b/templates/client/blank.html @@ -77,51 +77,17 @@ This template include basic anf main styles and js files, socialInputMask:['http://','https://'] }); - - - - - + + {% include 'client/includes/banners/catalog_inner.html' %} {%endif %} {% endfor %} diff --git a/templates/client/includes/exposition/exposition_object.html b/templates/client/includes/exposition/exposition_object.html index 18d2a2c7..8ae51315 100644 --- a/templates/client/includes/exposition/exposition_object.html +++ b/templates/client/includes/exposition/exposition_object.html @@ -208,29 +208,7 @@

- + {% include 'client/includes/banners/detail_inner.html' %}

diff --git a/templates/client/includes/exposition/price.html b/templates/client/includes/exposition/price.html index b7006c6a..f2470f8b 100644 --- a/templates/client/includes/exposition/price.html +++ b/templates/client/includes/exposition/price.html @@ -151,14 +151,15 @@
- {% if not exposition.paid %} - {% if exposition.country_id in sng_countries %} - {% else %} - {% trans 'Заказать билет' %} - {% endif %} + {% if exposition.paid_new_id and exposition.paid_new.public %} + {% trans 'Заказать билет' %} + {% else %} + {% if exposition.country_id in sng_countries %} {% else %} - {% trans 'Заказать билет' %} + {% trans 'Заказать билет' %} {% endif %} + {% endif %} +
{% if exposition.get_audience %}
@@ -222,10 +223,10 @@ {% else %}

{% trans 'Цены на площадь доступны по запросу' %}

{% endif %} - {% if not exposition.paid %} - {% trans 'Заявка на участие' %} + {% if exposition.paid_new_id and exposition.paid_new.public %} + {% trans 'Заявка на участие' %} {% else %} - {% trans 'Заявка на участие' %} + {% trans 'Заявка на участие' %} {% endif %} {% if exposition.min_stand_size or exposition.registration_payment or exposition.application_deadline %} diff --git a/templates/client/includes/header.html b/templates/client/includes/header.html index a58bb786..911ea5a4 100644 --- a/templates/client/includes/header.html +++ b/templates/client/includes/header.html @@ -37,7 +37,6 @@ {% endfor %} {% if user.is_staff %}
  • admin
  • -
  • TEST
  • {% endif %}
    @@ -98,19 +97,7 @@
    -
    - {% block header_banner %} - - - - - {% endblock %} -
    + {% include 'client/includes/banners/header.html' %}
    From 8bc67e642d71ec571b9b70f36e0e1d14b0cc762c Mon Sep 17 00:00:00 2001 From: re0ne Date: Tue, 1 Sep 2015 19:28:10 +0300 Subject: [PATCH 2/7] Banners --- templates/client/blank.html | 18 ++-- templates/client/static_client/js/banners.js | 93 ++++++++++++++++++++ 2 files changed, 100 insertions(+), 11 deletions(-) create mode 100644 templates/client/static_client/js/banners.js diff --git a/templates/client/blank.html b/templates/client/blank.html index 6ffdd844..9d4b6acf 100644 --- a/templates/client/blank.html +++ b/templates/client/blank.html @@ -80,18 +80,14 @@ This template include basic anf main styles and js files, + diff --git a/templates/client/static_client/js/banners.js b/templates/client/static_client/js/banners.js new file mode 100644 index 00000000..b073f897 --- /dev/null +++ b/templates/client/static_client/js/banners.js @@ -0,0 +1,93 @@ +(function () { + "use strict"; + + var API_URL_ROOT = "/expo-b/get-banners/"; + + var getUrl = function () { + var data = window.sendData; + + if (data instanceof Object) { + var search = []; + + for (var key in data) { + if (data.hasOwnProperty(key)) { + var value = data[key]; + + if (value instanceof Array) { + for (var i = 0, l = value.length; i < l; i++) { + search.push(key + "=" + value[i]); + } + } else { + search.push(key + "=" + value); + } + } + } + + if (search.length) { + return API_URL_ROOT + "?" + search.join("&"); + } + } + + return API_URL_ROOT; + }; + + var getBanners = function (url, callback) { + var xhr = new XMLHttpRequest(); + xhr.open("GET", url, true); + + xhr.onreadystatechange = function (event) { + if (event.target.readyState === 4) { + if (event.target.status === 200) { + try { + callback(JSON.parse(event.target.responseText)); + } catch (error) { + // do nothing + } + } + } + }; + + xhr.send(); + }; + + var addBanner = function (banner) { + console.log(banner); + + var parent = document.getElementById(banner.id); + + if (parent) { + console.log(parent); + + if (banner.is_img) { + var a = document.createElement("a"); + a.href = banner.url; + + var img = document.createElement("img"); + img.src = banner.img; + img.alt = banner.alt; + + a.appendChild(img); + parent.appendChild(a); + } else if (banner.is_html || banner.is_js) { + parent.innerHTML = banner.text; + // $(parent).html(""); + // $(parent).html(banner.text); + } + } + + }; + + var insertBanners = function (data) { + if (data instanceof Array) { + for (var i = 0, l = data.length; i < l; i++) { + addBanner(data[i]); + } + } + }; + + var main = function () { + getBanners(getUrl(), insertBanners); + }; + + window.addEventListener("load", main); +})(); From e002028cab71db33df99dfe6ce9d0d4d219afb3d Mon Sep 17 00:00:00 2001 From: Kotiuk Nazarii Date: Tue, 1 Sep 2015 21:59:19 +0300 Subject: [PATCH 3/7] 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 %}
      • From 9bcb8fa322098361198f6705a7b8fdc8344159bf Mon Sep 17 00:00:00 2001 From: re0ne Date: Tue, 1 Sep 2015 22:42:45 +0300 Subject: [PATCH 4/7] Removed temporary code --- templates/client/static_client/js/banners.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/templates/client/static_client/js/banners.js b/templates/client/static_client/js/banners.js index b073f897..bd32b4fb 100644 --- a/templates/client/static_client/js/banners.js +++ b/templates/client/static_client/js/banners.js @@ -51,13 +51,9 @@ }; var addBanner = function (banner) { - console.log(banner); - var parent = document.getElementById(banner.id); if (parent) { - console.log(parent); - if (banner.is_img) { var a = document.createElement("a"); a.href = banner.url; @@ -68,10 +64,8 @@ a.appendChild(img); parent.appendChild(a); - } else if (banner.is_html || banner.is_js) { + } else if (banner.is_html) { parent.innerHTML = banner.text; - // $(parent).html(""); - // $(parent).html(banner.text); } } From d8d8395f5acc7321731206bb43e74ac86b7e08fb Mon Sep 17 00:00:00 2001 From: re0ne Date: Tue, 1 Sep 2015 23:00:39 +0300 Subject: [PATCH 5/7] Minor fixes --- templates/client/static_client/js/banners.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/client/static_client/js/banners.js b/templates/client/static_client/js/banners.js index bd32b4fb..1f6126ca 100644 --- a/templates/client/static_client/js/banners.js +++ b/templates/client/static_client/js/banners.js @@ -15,10 +15,10 @@ if (value instanceof Array) { for (var i = 0, l = value.length; i < l; i++) { - search.push(key + "=" + value[i]); + search.push(encodeURIComponent(key) + "=" + encodeURIComponent(value[i])); } } else { - search.push(key + "=" + value); + search.push(encodeURIComponent(key) + "=" + encodeURIComponent(value)); } } } From 3f5345ea5e82dce0982bf36d9884135c3c101b06 Mon Sep 17 00:00:00 2001 From: Kotiuk Nazarii Date: Wed, 2 Sep 2015 00:17:20 +0300 Subject: [PATCH 6/7] Master commit --- templates/client/base_catalog.html | 2 ++ templates/client/blank.html | 19 ++------------- .../client/exposition/exposition_detail.html | 24 ++++--------------- .../client/includes/banner_blocks/aside2.html | 22 +++++++++++++++++ .../client/includes/banners/under_search.html | 5 +--- 5 files changed, 32 insertions(+), 40 deletions(-) create mode 100644 templates/client/includes/banner_blocks/aside2.html diff --git a/templates/client/base_catalog.html b/templates/client/base_catalog.html index 42d7fcaa..c3dad2cc 100644 --- a/templates/client/base_catalog.html +++ b/templates/client/base_catalog.html @@ -52,6 +52,8 @@ {% endblock %} {% include 'client/includes/side_confs.html' %}
        + {% include 'client/includes/banner_blocks/aside2.html' %} +
        {% include 'client/includes/news.html' with news=news_list %}
        diff --git a/templates/client/blank.html b/templates/client/blank.html index 169d8388..4db2da11 100644 --- a/templates/client/blank.html +++ b/templates/client/blank.html @@ -164,14 +164,6 @@ This template include basic anf main styles and js files, {% if not request.COOKIES.popover_test1 %} {% if theme_for_filter %} - - {% if theme_for_filter.id == 2 %} - {% if False|random_social %} - {% include 'client/popups/auto_modal.html' %} - {% else %} - {% include 'client/popups/auto_banner.html' %} - {% endif %} - {% endif %} {% if theme_for_filter.id == 54 or theme_for_filter.id == 26 or theme_for_filter.id == 22 or theme_for_filter.id == 15 or theme_for_filter.id == 44 or theme_for_filter.id == 30 %} {% with r=False|random3 %} {% if r == 1 %} @@ -187,18 +179,11 @@ This template include basic anf main styles and js files, {% endif %} {% if theme_for_filter.id == 32 %} - {% with r=False|random5 %} + {% with r=False|random3 %} {% if r == 1 %} - {% include 'client/popups/auto_modal.html' %} - {% endif %} - - {% if r == 2 %} - {% include 'client/popups/auto_banner.html' %} - {% endif %} - {% if r == 3 %} {% include 'client/popups/cemat_modal.html' %} {% endif %} - {% if r == 4 %} + {% if r == 2 %} {% include 'client/popups/cemat_banner1.html' %} {% endif %} {% if r == 0 %} diff --git a/templates/client/exposition/exposition_detail.html b/templates/client/exposition/exposition_detail.html index 8a365de1..7cc0cb3d 100644 --- a/templates/client/exposition/exposition_detail.html +++ b/templates/client/exposition/exposition_detail.html @@ -34,21 +34,14 @@ {% with theme_ids=object.theme_ids %} {% if 32 in theme_ids %} - {% with r=False|random5 %} - {% if r == 1 %} - {% include 'client/popups/auto_modal.html' %} - {% endif %} - - {% if r == 2 %} - {% include 'client/popups/auto_banner.html' %} - {% endif %} - {% if r == 3 %} + {% with r=False|random3 %} + {% if r == 0 %} {% include 'client/popups/cemat_modal.html' %} {% endif %} - {% if r == 4 %} + {% if r == 1 %} {% include 'client/popups/cemat_banner1.html' %} {% endif %} - {% if r == 5 %} + {% if r == 2 %} {% include 'client/popups/cemat_banner2.html' %} {% endif %} {% endwith %} @@ -65,14 +58,7 @@ {% endif %} {% endif %} {% endwith %} - {% else %} - {% if 2 in theme_ids %} - {% if False|random_social %} - {% include 'client/popups/auto_modal.html' %} - {% else %} - {% include 'client/popups/auto_banner.html' %} - {% endif %} - {% endif %} + {% endif %} {% endif %} {% endwith %} diff --git a/templates/client/includes/banner_blocks/aside2.html b/templates/client/includes/banner_blocks/aside2.html new file mode 100644 index 00000000..b8bc1003 --- /dev/null +++ b/templates/client/includes/banner_blocks/aside2.html @@ -0,0 +1,22 @@ +
        + + + +
        \ No newline at end of file diff --git a/templates/client/includes/banners/under_search.html b/templates/client/includes/banners/under_search.html index 4d80cf36..f2c3f38f 100644 --- a/templates/client/includes/banners/under_search.html +++ b/templates/client/includes/banners/under_search.html @@ -2,7 +2,7 @@ {% load template_filters %}
        - {% with r=False|random4 %} + {% with r=False|random3 %} {% ifequal r 0 %} {% endifequal %} @@ -10,9 +10,6 @@ {% endifequal %} {% ifequal r 2 %} - - {% endifequal %} - {% ifequal r 3 %} {% endifequal %} {% endwith %} From b990914d5174d0bc3ad6355c2d0bc64b9afc075e Mon Sep 17 00:00:00 2001 From: Kotiuk Nazarii Date: Wed, 2 Sep 2015 00:20:25 +0300 Subject: [PATCH 7/7] Pre deployed commit --- expobanner/forms.py | 4 +- expobanner/models.py | 5 ++ expobanner/utils.py | 55 ++++++++++++++++--- expobanner/views.py | 28 ++-------- exposition/views.py | 16 +----- templates/client/exposition/catalog.html | 3 - .../client/exposition/catalog_theme.html | 5 -- .../client/includes/banners/aside_2.html | 20 +++++++ .../includes/banners/catalog_inner.html | 23 ++++++++ .../client/includes/banners/detail_inner.html | 24 +++++++- templates/client/includes/banners/header.html | 9 +++ 11 files changed, 138 insertions(+), 54 deletions(-) diff --git a/expobanner/forms.py b/expobanner/forms.py index f489d731..5c3b6473 100644 --- a/expobanner/forms.py +++ b/expobanner/forms.py @@ -164,13 +164,13 @@ class TopCreateForm(forms.ModelForm): theme_ids = self.cleaned_data['theme'] if theme_ids: return Theme.objects.filter(id__in=theme_ids) - return None + return Theme.objects.none() def clean_country(self): country_ids = self.cleaned_data['country'] if country_ids: return Country.objects.filter(id__in=country_ids) - return None + return Country.objects.none() def clean_exposition(self): expo_id = self.cleaned_data['exposition'] diff --git a/expobanner/models.py b/expobanner/models.py index e543b29e..b49c914f 100644 --- a/expobanner/models.py +++ b/expobanner/models.py @@ -255,6 +255,11 @@ class Top(models.Model, StatMixin): class Meta: ordering = ['position'] + def get_event(self): + try: + return self.exposition_set.all()[0] + except IndexError: + return None class TopStat(models.Model): date = models.DateField() diff --git a/expobanner/utils.py b/expobanner/utils.py index a7a9cd53..9c4222a2 100644 --- a/expobanner/utils.py +++ b/expobanner/utils.py @@ -21,22 +21,21 @@ def get_by_sort(banner_list): def get_banner_by_params(banners_list, urls, params): - #print('START. NUMBER of queries = %d'%len(connection.queries)) thematic_banners = [] url_banners = [] for banner in banners_list: - #print('-------------------------') - #print('number of queries = %d'%len(connection.queries)) - # check by theme banner_theme_ids = [str(theme.id) for theme in banner.theme.all()] - #print('number of queries = %d'%len(connection.queries)) - if banner_theme_ids: if params.get('theme'): theme = params['theme'] - if theme in banner_theme_ids: + + flag = False + for th in theme: + if th in banner_theme_ids: + flag = True + if flag: thematic_banners.append(banner) continue # check by country @@ -63,7 +62,7 @@ def get_banner_by_params(banners_list, urls, params): if common_urls: url_banners.append(banner) continue - print('-------------------------') + if thematic_banners: return random.choice(thematic_banners) if url_banners: @@ -71,3 +70,43 @@ def get_banner_by_params(banners_list, urls, params): return None #print('END. NUMBER of queries = %d'%len(connection.queries)) + +def get_top_events(tops, params): + catalog = params.get('catalog') + country = params.get('country', '') + theme = params.get('theme', []) + city = params.get('city', '') + tag = params.get('tag', '') + catalog_tops = [item for item in tops if item.catalog == catalog] + good_tops = [] + for top in catalog_tops: + + country_ids = [str(item.id) for item in top.country.all()] + theme_ids = [str(item.id) for item in top.theme.all()] + excluded_tags_ids = [str(item.id) for item in top.excluded_tags.all()] + excluded_cities_ids = [str(item.id) for item in top.excluded_cities.all()] + if not country_ids and not theme_ids: + # universal top + good_tops.append(top) + continue + # check country + if country in country_ids and city not in excluded_cities_ids : + good_tops.append(top) + continue + # check theme + if tag in excluded_tags_ids: + continue + flag = False + for th in theme: + if th in theme_ids: + flag = True + continue + if flag: + good_tops.append(top) + sorted_top = sorted(good_tops, key=lambda x: x.position) + events = [] + for top in sorted_top: + event = top.get_event() + if event: + events.append(event) + return events \ No newline at end of file diff --git a/expobanner/views.py b/expobanner/views.py index d0e38527..96540cdc 100644 --- a/expobanner/views.py +++ b/expobanner/views.py @@ -3,8 +3,10 @@ import json import re from django.http import HttpResponse from django.shortcuts import redirect, get_object_or_404 +from django.shortcuts import render_to_response +from django.template import RequestContext from .models import Banner, BannerGroup, URL, Top -from expobanner.utils import get_by_sort, get_banner_by_params, get_client_ip +from expobanner.utils import get_by_sort, get_banner_by_params, get_client_ip, get_top_events def click(request, banner_id): @@ -32,7 +34,7 @@ def get_banners(request): elif url == u.url: good_urls.append(u) # fill parameters dict - params = {'theme': request.GET.get('theme'), + params = {'theme': request.GET.getlist('theme', []), 'tag': request.GET.get('tag'), 'country': request.GET.get('country'), 'city': request.GET.get('city'), @@ -69,25 +71,8 @@ def get_banners(request): 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'), + params = {'theme': request.GET.getlist('theme', []), 'tag': request.GET.get('tag'), 'country': request.GET.get('country'), 'city': request.GET.get('city'), @@ -95,6 +80,5 @@ def get_top(request): tops = Top.cached.all() events = get_top_events(tops, params) - expos = Exposition.objects.filter(top__isnull=False) - context = {'objects': expos} + context = {'objects': events} 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/views.py b/exposition/views.py index b6464599..721bd91c 100644 --- a/exposition/views.py +++ b/exposition/views.py @@ -303,7 +303,6 @@ class ExpoCatalog(MetadataMixin, ListView): month = None country = None city = None - paid = None def get_filtered_qs(self): # diferent for views @@ -355,8 +354,6 @@ class ExpoCatalog(MetadataMixin, ListView): def get_context_data(self, **kwargs): context = super(ExpoCatalog, self).get_context_data(**kwargs) - if self.paid: - context['paid'] = self.paid context['search_form'] = self.search_form context['filter_object'] = self.filter_object context['year'] = self.year @@ -411,11 +408,8 @@ class ExpoThemeCatalog(ExpoCatalog): city_slug = self.kwargs.get('city_slug') theme = get_object_or_404(Theme, url=slug) self.kwargs['theme'] = theme + qs = self.model.enable.upcoming().filter(theme=theme) - qs = self.model.enable.upcoming().filter(theme=theme).exclude(paid__isnull=False) - paid= list(self.model.enable.filter(theme=theme).filter(paid__isnull=False)) - if paid: - self.paid = paid if country_slug: country = get_object_or_404(Country, url=country_slug) self.country = country @@ -449,11 +443,7 @@ class ExpoTagCatalog(ExpoCatalog): slug = self.kwargs.get('slug') tag = get_object_or_404(Tag, url=slug) self.kwargs['tag'] = tag - qs = self.model.enable.upcoming().filter(tag=tag).exclude(paid__isnull=False) - paid= list(self.model.enable.filter(tag=tag).filter(paid__isnull=False)) - if paid: - self.paid = paid - + qs = self.model.enable.upcoming().filter(tag=tag) self.filter_object = tag return qs @@ -550,7 +540,7 @@ from django.core.mail import send_mail from django.core.mail import EmailMessage def send_to_organiser(request, slug): exposition = get_object_or_404(Exposition, url=slug) - mail_send = exposition.paid.organiser + mail_send = 'evm@expomap.ru' name = request.POST.get('person_inf') email = request.POST.get('person') phone = request.POST.get('phone', '') diff --git a/templates/client/exposition/catalog.html b/templates/client/exposition/catalog.html index de61d7eb..cb8941dc 100644 --- a/templates/client/exposition/catalog.html +++ b/templates/client/exposition/catalog.html @@ -33,9 +33,6 @@ {% endblock %} {% block content_list %} - {% if paid %} - {% include 'includes/exposition/expo_list_paid.html' with object_list=paid %} - {% endif %} {% include 'includes/exposition/exposition_list.html' with object_list=object_list %} {% endblock %} diff --git a/templates/client/exposition/catalog_theme.html b/templates/client/exposition/catalog_theme.html index ec72ee80..8d199340 100644 --- a/templates/client/exposition/catalog_theme.html +++ b/templates/client/exposition/catalog_theme.html @@ -49,18 +49,13 @@ {% block page_title %}
        - {#

        {% trans 'Выставки' %}: {{ filter_object.name }}

        #}

        {% if meta %}{{ meta.h1 }}{% else %}{% trans 'Выставки' %}: {{ filter_object.name }}{% endif %}

        - {% include 'includes/exposition/catalog_filter_period.html' %} {% endblock %} {% block content_list %} - {% if paid %} - {% include 'includes/exposition/exposition_list.html' with object_list=paid %} - {% endif %} {% include 'includes/exposition/exposition_list.html' with object_list=object_list %} {% endblock %} diff --git a/templates/client/includes/banners/aside_2.html b/templates/client/includes/banners/aside_2.html index 9fdc80bd..439bfe68 100644 --- a/templates/client/includes/banners/aside_2.html +++ b/templates/client/includes/banners/aside_2.html @@ -1,4 +1,24 @@
        + + +
        \ No newline at end of file diff --git a/templates/client/includes/banners/catalog_inner.html b/templates/client/includes/banners/catalog_inner.html index 591ea030..ccf1add4 100644 --- a/templates/client/includes/banners/catalog_inner.html +++ b/templates/client/includes/banners/catalog_inner.html @@ -1,3 +1,26 @@
        +
        \ No newline at end of file diff --git a/templates/client/includes/banners/detail_inner.html b/templates/client/includes/banners/detail_inner.html index fe2082ea..6d0760aa 100644 --- a/templates/client/includes/banners/detail_inner.html +++ b/templates/client/includes/banners/detail_inner.html @@ -1,3 +1,25 @@
        - +
        \ No newline at end of file diff --git a/templates/client/includes/banners/header.html b/templates/client/includes/banners/header.html index 41416632..89c7f5a2 100644 --- a/templates/client/includes/banners/header.html +++ b/templates/client/includes/banners/header.html @@ -1,3 +1,12 @@
        + + + +
        \ No newline at end of file