# -*- coding: utf-8 -*- from datetime import date, datetime from itertools import chain from django.conf import settings from django.core.urlresolvers import reverse_lazy from django.contrib import admin from django.db.models import Q, Sum from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404 from django.utils.translation import ugettext_lazy as _ from django.views.generic import ( CreateView, DeleteView, DetailView, ListView, TemplateView, UpdateView ) from expobanner.forms import ( BannerCreateForm, BannerCreateGroupForm, BannerGroupUpdateForm, BannerLinkCreateForm, CustomerCreateForm, MainConfCreateForm, MainCreateForm, MainUpdateForm, PaidConfCreateForm, PaidCreateForm, PaidUpdateForm, TopCreateForm, TopUpdateForm, UrlCreateForm, TopPlaceCreateForm, TopPlaceUpdateForm, ) from expobanner.models import ( URL, Banner, BannerGroup, Customer, MainPage, Paid, Top ) from exposition.models import Exposition from conference.models import Conference from settings.forms import CommentForm, HtmlForm, ParticipationCommentForm from settings.models import Html, LandingComment, ParticipationComment admin.site.register(BannerGroup) class BannersControl(TemplateView): template_name = 'c_admin/expobanner/banners_control.html' # CREATE VIEWS class AbstractCreate(CreateView): template_name = 'c_admin/expobanner/default_form.html' success_url = '/admin/expobanners/banners/control/' class CreateUrl(AbstractCreate): model = URL form_class = UrlCreateForm class CreateBannerGroup(AbstractCreate): model = BannerGroup form_class = BannerCreateGroupForm class CreateBanner(AbstractCreate): model = Banner form_class = BannerCreateForm class CreateLink(AbstractCreate): model = Banner form_class = BannerLinkCreateForm class CreateCustomer(AbstractCreate): model = Customer form_class = CustomerCreateForm # LISTS VIEWS class AbstractList(ListView): paginate_by = settings.ADMIN_PAGINATION template_name = 'c_admin/expobanner/default_list.html' def get_context_data(self, **kwargs): context = super(AbstractList, self).get_context_data(**kwargs) context['verbose'] = self.verbose return context class UrlList(AbstractList): model = URL verbose = _(u'Список урлов') class BannerGroupList(AbstractList): model = BannerGroup verbose = _(u'Список груп') class BannerList(AbstractList): model = Banner verbose = _(u'Список банеров') template_name = 'c_admin/expobanner/banner_list.html' def get_queryset(self): qs = self.model.objects.filter(group__isnull=False).order_by('-fr') if not self.request.GET.get('show_inactive'): qs = qs.filter(public=True) qs = qs.filter(Q(fr__lte=date.today()) & (Q(to__isnull=True) | Q( to__gte=date.today()))) if self.request.GET.get('q'): qs = qs.filter(title__icontains=self.request.GET['q']) return qs class LinkList(AbstractList): model = Banner verbose = _(u'Список ссылок') template_name = 'c_admin/expobanner/link_list.html' def get_queryset(self): qs = super(LinkList, self).get_queryset() qs = qs.filter(link=True).order_by('-created_at') return qs class CustomerList(AbstractList): model = Customer verbose = _(u'Список заказчиков') template_name = 'c_admin/expobanner/customer_list.html' # UPDATE VIEWS class AbstractUpdate(UpdateView): template_name = 'c_admin/expobanner/default_form.html' success_url = '/admin/expobanners/banners/control/' class UrlUpdate(AbstractUpdate): model = URL form_class = UrlCreateForm class BannerGroupUpdate(AbstractUpdate): model = BannerGroup form_class = BannerGroupUpdateForm class BannerUpdate(AbstractUpdate): model = Banner form_class = BannerCreateForm class LinkUpdate(AbstractUpdate): model = Banner form_class = BannerLinkCreateForm class UpdateCustomer(AbstractUpdate): model = Customer form_class = CustomerCreateForm class BannerStat(DetailView): model = Banner template_name = 'c_admin/expobanner/banner_stat.html' def get_context_data(self, **kwargs): context = super(BannerStat, self).get_context_data(**kwargs) obj = self.object qs = obj.banner_stat.all() date_from, date_to = self.request.GET.get('date_from'), self.request.GET.get('date_to') if date_from: date_from = datetime.strptime(date_from, "%d.%m.%Y") qs = qs.filter(date__gte=date_from) if date_to: date_to = datetime.strptime(date_to, "%d.%m.%Y") qs = qs.filter(date__lte=date_to) context['all'] = qs.aggregate( view=Sum('view'), click=Sum('click'), unique_view=Sum('unique_view'), unique_click=Sum('unique_click') ) context['stats'] = qs return context ############################################################################### # DELETE views ############################################################################### class DeleteCustomer(DeleteView): template_name = 'c_admin/expobanner/customer_confirm_delete.html' model = Customer success_url = reverse_lazy('expobanner-customer_list') # slug_field = 'url' # slug_url_kwarg = 'url' ############################################################################### # LANGING COMMENTS ############################################################################### class CommentMixin(object): form_class = CommentForm model = LandingComment template_name = 'c_admin/expobanner/comment_create.html' success_url = reverse_lazy('expobaner-comment_list') def get_context_data(self, *args, **kwargs): ctx = super(CommentMixin, self).get_context_data(*args, **kwargs) ctx['languages'] = settings.LANGUAGES return ctx def get_success_url(self): return self.success_url class ParticipationMixin(CommentMixin): form_class = ParticipationCommentForm model = ParticipationComment success_url = reverse_lazy('expobaner-pcomment_list') class CommentList(CommentMixin, ListView): template_name = 'c_admin/expobanner/comment_list.html' def get_queryset(self): return self.model.objects.language() class ParticipationCommentList(ParticipationMixin, CommentList): template_name = 'c_admin/expobanner/pcomment_list.html' class CommentCreate(CommentMixin, CreateView): pass class ParticipationCommentCreate(ParticipationMixin, CreateView): pass class CommentUpdate(CommentMixin, UpdateView): pass class ParticipationCommentUpdate(ParticipationMixin, UpdateView): pass class CommentDelete(CommentMixin, DeleteView): def get(self, request, *args, **kwargs): return self.post(request, *args, **kwargs) class ParticipationCommentDelete(ParticipationMixin, CommentDelete): pass ############################################################################### # Other ############################################################################### class PaidList(ListView): expo = { 'model': Exposition, 'context': { 'create_link': reverse_lazy('expobanner-create_paid'), 'object_name': _(u'выставка'), 'object_name_plural': _(u'выставок'), 'object_name_dative': _(u'выставку'), }, } conf = { 'model': Conference, 'context': { 'create_link': reverse_lazy('confbanner-create_paid'), 'object_name': _(u'конференция'), 'object_name_plural': _(u'конференций'), 'object_name_dative': _(u'конференцию'), } } model = None template_name = 'c_admin/expobanner/paid_list.html' paginate_by = settings.ADMIN_PAGINATION def get(self, *args, **kwargs): self.target = self.kwargs.get('target') self.model = self.target.get('model') return super(PaidList, self).get(*args, **kwargs) def get_context_data(self, **kwargs): context = super(PaidList, self).get_context_data(**kwargs) context.update(self.target.get('context')) return context def get_queryset(self): qs = self.model.objects.language().filter(paid_new__isnull=False).order_by('-paid_new__public') if not self.request.GET.get('show_inactive'): qs = qs.filter(paid_new__public=True) if self.request.GET.get('q'): qs = qs.filter(name__icontains=self.request.GET['q']) return qs class PaidCreate(CreateView): form_class = PaidCreateForm template_name = 'c_admin/expobanner/paid_create.html' success_url = '/admin/expobanners/paid/list/' class PaidConfCreate(PaidCreate): form_class = PaidConfCreateForm success_url = '/admin/expobanners/paid_conf/list/' class PaidUpdate(UpdateView): model = Paid form_class = None template_name = 'c_admin/expobanner/paid_update.html' success_url = '/admin/expobanners/paid/list/' def dispatch(self, request, *args, **kwargs): self.success_url = kwargs.get('success_url', self.success_url) self.form_class = self.kwargs.get('form_class') return super(PaidUpdate, self).dispatch(request, *args, **kwargs) 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 if obj.participation: 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 = 'c_admin/expobanner/paid_stat.html' def get_context_data(self, **kwargs): context = super(PaidStat, self).get_context_data(**kwargs) obj = self.object context['all'] = obj.paidstat_set.aggregate( official=Sum('official_clicks'), ticket=Sum('tickets_clicks'), participation=Sum('participation_clicks'), catalog=Sum('catalog_clicks') ) qs = obj.paidstat_set.all() date_from, date_to = self.request.GET.get('date_from'), self.request.GET.get('date_to') if date_from: date_from = datetime.strptime(date_from, "%d.%m.%Y") qs = qs.filter(date__gte=date_from) if date_to: date_to = datetime.strptime(date_to, "%d.%m.%Y") qs = qs.filter(date__lte=date_to) context['stats'] = qs return context # ---------------------------------- class MainList(ListView): model = Exposition template_name = 'c_admin/expobanner/main_list.html' paginate_by = settings.ADMIN_PAGINATION def get_queryset(self): qs = self.model.objects.language().filter(main__isnull=False).order_by('-main__public') if not self.request.GET.get('show_inactive'): qs = qs.filter(main__public=True) return qs class MainCreate(CreateView): form_class = MainCreateForm template_name = 'c_admin/expobanner/paid_create.html' success_url = '/admin/expobanners/main/list/' class MainUpdate(UpdateView): model = MainPage form_class = MainUpdateForm template_name = 'c_admin/expobanner/default_form.html' success_url = '/admin/expobanners/main/list/' def get_context_data(self, **kwargs): context = super(MainUpdate, self).get_context_data(**kwargs) obj = self.object context['exposition'] = obj.get_event() return context class MainConfList(ListView): model = Conference template_name = 'c_admin/expobanner/main_list.html' paginate_by = settings.ADMIN_PAGINATION def get_queryset(self): qs = self.model.objects.language().filter(main__isnull=False).order_by('-main__public') if not self.request.GET.get('show_inactive'): qs = qs.filter(main__public=True) return qs def get_context_data(self, **kwargs): context = super(MainConfList, self).get_context_data(**kwargs) context.update({'conf': True}) return context class MainConfCreate(CreateView): form_class = MainConfCreateForm template_name = 'c_admin/expobanner/paid_create.html' success_url = reverse_lazy('expobanner-conf-list_main') def get_context_data(self, **kwargs): context = super(MainConfCreate, self).get_context_data(**kwargs) context.update({'conf': True}) return context class MainConfUpdate(UpdateView): model = MainPage form_class = MainUpdateForm template_name = 'c_admin/expobanner/default_form.html' success_url = reverse_lazy('expobanner-conf-list_main') def get_context_data(self, **kwargs): context = super(MainConfUpdate, self).get_context_data(**kwargs) obj = self.object context['conference'] = obj.get_event() return context def main_turn(request, pk, status): main = get_object_or_404(MainPage, pk=pk) if status == 'on': main.public = True else: main.public = False main.save() return HttpResponseRedirect(request.META['HTTP_REFERER']) class MainStat(DetailView): model = MainPage template_name = 'c_admin/expobanner/main_stat.html' def get_context_data(self, **kwargs): context = super(MainStat, self).get_context_data(**kwargs) obj = self.object context['all'] = obj.link.banner_stat.aggregate( views=Sum('view'), clicks=Sum('click'), unique_clicks=Sum('unique_click'), unique_views=Sum('unique_view') ) qs = obj.link.banner_stat.all() date_from, date_to = self.request.GET.get('date_from'), self.request.GET.get('date_to') if date_from: date_from = datetime.strptime(date_from, "%d.%m.%Y") qs = qs.filter(date__gte=date_from) if date_to: date_to = datetime.strptime(date_to, "%d.%m.%Y") qs = qs.filter(date__lte=date_to) context['stats'] = qs return context class TopStat(MainStat): model = Top class TopList(ListView): model = Exposition template_name = 'c_admin/expobanner/top_list.html' paginate_by = settings.ADMIN_PAGINATION def get_queryset(self): expo = Exposition.objects.language().filter(top__isnull=False).order_by('-top__fr') conf = Conference.objects.language().filter(top__isnull=False).order_by('-top__fr') if not self.request.GET.get('show_inactive'): expo = expo.filter(top__fr__lte=date.today(), top__to__gte=date.today()) conf = conf.filter(top__fr__lte=date.today(), top__to__gte=date.today()) # qs = qs.filter(top__fr__lte=date.today(), top__to__gte=date.today()) return list(chain(expo, conf)) class TopCreate(CreateView): form_class = TopCreateForm template_name = 'c_admin/expobanner/top_create.html' success_url = '/admin/expobanners/top/list/' class TopUpdate(UpdateView): model = Top form_class = TopUpdateForm template_name = 'c_admin/expobanner/top_create.html' success_url = '/admin/expobanners/top/list/' def get_context_data(self, **kwargs): context = super(TopUpdate, self).get_context_data(**kwargs) obj = self.object context['exposition'] = obj.get_event() return context class TopPlaceList(ListView): model = Top template_name = 'c_admin/expobanner/top_place_list.html' paginate_by = settings.ADMIN_PAGINATION def get_queryset(self): qs = Top.objects\ .filter(catalog='places')\ .order_by('-fr') if not self.request.GET.get('show_inactive'): qs = qs.filter(fr__lte=date.today(), to__gte=date.today()) return qs class TopPlaceCreate(CreateView): model = Top form_class = TopPlaceCreateForm template_name = 'c_admin/expobanner/top_place_create.html' success_url = reverse_lazy('expobanner-list_top_place') class TopPlaceUpdate(UpdateView): model = Top form_class = TopPlaceUpdateForm template_name = 'c_admin/expobanner/top_place_create.html' success_url = reverse_lazy('expobanner-list_top_place') def get_context_data(self, **kwargs): context = super(TopPlaceUpdate, self).get_context_data(**kwargs) obj = self.object context['exposition'] = obj.get_event() return context class MainPageBlock(UpdateView): model = Html form_class = HtmlForm template_name = 'c_admin/expobanner/main_page_block.html' def get_context_data(self, **kwargs): context = super(MainPageBlock, self).get_context_data(**kwargs) context['languages'] = settings.LANGUAGES return context def get_success_url(self): return '/admin/'