# -*- coding: utf-8 -*- from django.views.generic import TemplateView, CreateView, ListView, UpdateView, DetailView from django.conf import settings 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, TopCreateForm from exposition.models import Exposition class BannersControl(TemplateView): template_name = 'admin/expobanner/banners_control.html' # CREATE VIEWS class AbstractCreate(CreateView): template_name = '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 # LISTS VIEWS class AbstractList(ListView): paginate_by = settings.ADMIN_PAGINATION template_name = '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 = '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): template_name = '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 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' 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/'