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.
155 lines
5.2 KiB
155 lines
5.2 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 theme.models import Tag
|
|
from models import Webinar
|
|
from forms import WebinarChangeForm, WebinarCreateForm, WebinarDeleteForm
|
|
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
|
|
|
|
|
|
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.
|
|
"""
|
|
#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()]
|
|
if form.is_valid():
|
|
form.save()
|
|
return HttpResponseRedirect('/admin/webinar/all/')
|
|
else:
|
|
form = WebinarCreateForm(initial={'key': key})
|
|
|
|
args = {}
|
|
args.update(csrf(request))
|
|
|
|
args['form'] = form
|
|
args['languages'] = settings.LANGUAGES
|
|
args['file_form'] = file_form
|
|
args['files'] = TmpFile.objects.filter(key=key)
|
|
|
|
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:
|
|
form = WebinarChangeForm(request.POST)
|
|
form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.all()]
|
|
if form.is_valid():
|
|
form.save(getattr(webinar, 'id'))
|
|
return HttpResponseRedirect('/admin/webinar/all/')
|
|
else:
|
|
#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}
|
|
|
|
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_%s' % code] = obj.discount
|
|
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'])]
|
|
|
|
args = {}
|
|
args.update(csrf(request))
|
|
|
|
args['form'] = form
|
|
args['languages'] = settings.LANGUAGES
|
|
args['file_form'] = file_form
|
|
|
|
#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) |