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.
175 lines
6.5 KiB
175 lines
6.5 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
|
|
from file.models import FileModel, TmpFile
|
|
from file.forms import FileModelForm
|
|
#python
|
|
import random
|
|
|
|
|
|
|
|
@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('/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)
|
|
|
|
|
|
@login_required
|
|
def webinar_change(request, webinar_id):
|
|
"""
|
|
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(id=webinar_id)
|
|
file_form = FileModelForm(initial={'model': 'webinar.Webinar'})
|
|
except:
|
|
return HttpResponseRedirect('/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(webinar_id)
|
|
return HttpResponseRedirect('/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}
|
|
|
|
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=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(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'] = webinar_id
|
|
|
|
return render_to_response('webinar_add.html', args)
|
|
|
|
|
|
@login_required
|
|
def webinar_change2(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('/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('/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}
|
|
|
|
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)
|
|
|
|
|
|
|
|
@login_required
|
|
def webinar_all(request):
|
|
"""
|
|
Return list of all webinars
|
|
"""
|
|
webinars = Webinar.objects.all()
|
|
return render_to_response('webinar_all.html', {'webinars':webinars})
|
|
|