You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
7.4 KiB
198 lines
7.4 KiB
# -*- coding: utf-8 -*-
|
|
from django.shortcuts import render_to_response
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
|
from django.core.context_processors import csrf
|
|
from django.conf import settings
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.forms.formsets import formset_factory
|
|
from django.forms.models import modelformset_factory
|
|
#
|
|
from theme.models import Tag
|
|
from models import Webinar, Statistic
|
|
from forms import WebinarChangeForm, WebinarCreateForm, WebinarDeleteForm, StatisticForm, WebinarFilterForm
|
|
from file.models import FileModel, TmpFile
|
|
from file.forms import FileModelForm
|
|
#python
|
|
import random
|
|
#custom views
|
|
from functions.custom_views import objects_list, delete_object
|
|
from functions.views_help import get_referer
|
|
from functions.admin_views import AdminListView
|
|
|
|
|
|
def webinar_all(request):
|
|
"""
|
|
Return list of all webinars with pagination
|
|
"""
|
|
return objects_list(request, Webinar, 'webinar_all.html')
|
|
|
|
@login_required
|
|
def webinar_copy(request, url):
|
|
|
|
webinar = Webinar.objects.safe_get(url=url)
|
|
if not webinar:
|
|
return HttpResponseRedirect(get_referer(request))
|
|
else:
|
|
webinar.clone()
|
|
return HttpResponseRedirect(get_referer(request))
|
|
|
|
@login_required
|
|
def webinar_switch(request, url, action):
|
|
"""
|
|
turn on or off webinar
|
|
|
|
take:
|
|
url as url of webinar
|
|
|
|
action as action what to do('on' or 'off')
|
|
|
|
"""
|
|
webinar = Webinar.objects.safe_get(url=url)
|
|
if not webinar:
|
|
return HttpResponse('error')
|
|
else:
|
|
if action == 'on':
|
|
webinar.on()
|
|
return HttpResponse('on')
|
|
elif action == 'off':
|
|
webinar.off()
|
|
return HttpResponse('off')
|
|
else:
|
|
return HttpResponse('error')
|
|
|
|
|
|
@login_required
|
|
def webinar_add(request):
|
|
"""
|
|
Returns form of webinar and post it on the server.
|
|
|
|
If form is posted redirect on the page of all webinars.
|
|
"""
|
|
# formset of StatisticForm
|
|
StatisticFormSet = formset_factory(StatisticForm)
|
|
#if form would be not valid key must be same
|
|
if request.POST.get('key'):
|
|
key = request.POST['key']
|
|
else:
|
|
key = random.getrandbits(128)
|
|
|
|
file_form = FileModelForm(initial={'key': key})
|
|
|
|
if request.POST:
|
|
form = WebinarCreateForm(request.POST)
|
|
form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.all()]
|
|
#
|
|
formset_statistic = StatisticFormSet(request.POST)
|
|
if form.is_valid() and formset_statistic.is_valid():
|
|
webinar = form.save()
|
|
for item in formset_statistic.forms:
|
|
#saves forms if its valid and not empty
|
|
if item.is_valid() and item.has_changed():
|
|
statistic = item.save(commit=False)
|
|
statistic.webinar = webinar
|
|
statistic.save()
|
|
return HttpResponseRedirect('/admin/webinar/all/')
|
|
else:
|
|
form = WebinarCreateForm(initial={'key': key})
|
|
formset_statistic = StatisticFormSet()
|
|
|
|
args = {}
|
|
args.update(csrf(request))
|
|
|
|
args['form'] = form
|
|
args['languages'] = settings.LANGUAGES
|
|
args['file_form'] = file_form
|
|
args['files'] = TmpFile.objects.filter(key=key)
|
|
args['formset_statistic'] = formset_statistic
|
|
|
|
return render_to_response('webinar_add.html', args)
|
|
|
|
|
|
def webinar_delete(request, url):
|
|
return delete_object(request, Webinar, WebinarDeleteForm, url, '/admin/webinar/all')
|
|
|
|
|
|
@login_required
|
|
def webinar_change(request, url):
|
|
"""
|
|
Return form of Webinar and fill it with existing Webinar object data.
|
|
|
|
If form of webinar is posted redirect on the page of all webinars.
|
|
|
|
"""
|
|
try:
|
|
#check if webinar_id exists else redirect to the list of webinars
|
|
webinar = Webinar.objects.get(url=url)
|
|
file_form = FileModelForm(initial={'model': 'webinar.Webinar'})
|
|
except:
|
|
return HttpResponseRedirect('/admin/webinar/all/')
|
|
|
|
if request.POST:
|
|
StatisticFormSet = formset_factory(StatisticForm)
|
|
form = WebinarChangeForm(request.POST)
|
|
form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.all()]
|
|
formset_statistic = StatisticFormSet(request.POST)
|
|
if form.is_valid() and formset_statistic.is_valid():
|
|
webinar = form.save(getattr(webinar, 'id'))
|
|
#delete old halls
|
|
Statistic.objects.filter(webinar=getattr(webinar, 'id')).delete()
|
|
|
|
for item in formset_statistic.forms:
|
|
#saves new statistic if its valid and not empty
|
|
if item.is_valid() and item.has_changed():
|
|
statistic = item.save(commit=False)
|
|
statistic.webinar = webinar
|
|
statistic.save()
|
|
return HttpResponseRedirect('/admin/webinar/all/')
|
|
else:
|
|
#initial StatisticFormSet
|
|
StatisticFormSet = modelformset_factory(Statistic, form=StatisticForm, exclude=('webinar',))
|
|
#fill form with data from database
|
|
data = {'web_page':webinar.web_page, 'data_begin':webinar.data_begin, 'currency':webinar.currency,
|
|
'tax':webinar.tax, 'min_price':webinar.min_price, 'link':webinar.link,
|
|
'max_price':webinar.max_price, 'webinar_id':webinar.id, 'expohit': webinar.expohit,
|
|
'discount': webinar.discount, 'canceled': webinar.canceled, 'moved': webinar.moved,
|
|
'visitors': webinar.visitors, 'members': webinar.members, 'foundation_year': webinar.foundation_year,
|
|
'quality_label': [item for item, bool in webinar.quality_label if bool==True]}
|
|
|
|
data['theme'] = [item.id for item in webinar.theme.all()]
|
|
data['tag'] = [item.id for item in webinar.tag.all()]
|
|
#data from translated fields
|
|
for code, name in settings.LANGUAGES:
|
|
obj = Webinar._meta.translations_model.objects.get(language_code = code,master__id=getattr(webinar, 'id')) #access to translated fields
|
|
data['name_%s' % code] = obj.name
|
|
data['programm_%s' % code] = obj.programm
|
|
data['main_title_%s' % code] = obj.main_title
|
|
data['discount_description_%s' % code] = obj.discount_description
|
|
data['title_%s' % code] = obj.title
|
|
data['keywords_%s' % code] = obj.keywords
|
|
data['descriptions_%s' % code] = obj.descriptions
|
|
#initial form
|
|
form = WebinarChangeForm(initial=data)
|
|
form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.filter(theme__in=data['theme'])]
|
|
#get existing statistic
|
|
statistic = Statistic.objects.filter(webinar=getattr(webinar, 'id'))
|
|
#fill HallFormSet
|
|
formset_statistic = StatisticFormSet(queryset=statistic)
|
|
|
|
args = {}
|
|
args.update(csrf(request))
|
|
|
|
args['form'] = form
|
|
args['languages'] = settings.LANGUAGES
|
|
args['file_form'] = file_form
|
|
args['formset_statistic'] = formset_statistic
|
|
|
|
#get list of files which connected with specific model object
|
|
args['files'] = FileModel.objects.filter(content_type=ContentType.objects.get_for_model(webinar),
|
|
object_id=getattr(webinar, 'id'))
|
|
args['obj_id'] = getattr(webinar, 'id')
|
|
|
|
return render_to_response('webinar_add.html', args)
|
|
|
|
|
|
class WebinarListView(AdminListView):
|
|
template_name = 'admin/webinar/webinar_list.html'
|
|
form_class = WebinarFilterForm
|
|
model = Webinar |