1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2008 Lincoln de Sousa <lincoln@minaslivre.org>
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 2 of the
7 # License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public
15 # License along with this program; if not, write to the
16 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 # Boston, MA 02111-1307, USA.
18 from django.http import HttpResponseRedirect, HttpResponseForbidden
19 from django.contrib import auth
20 from django.contrib.auth.forms import AuthenticationForm
21 from django.newforms import form_for_instance, form_for_model
22 from django.shortcuts import render_to_response, get_object_or_404
23 from django.template import RequestContext, Context, loader
24 from eventos.models import Palestrante, Trabalho
27 HttpResponseForbidden('<h2>You are not allowed to do this action.<h2>')
30 """This is a function that will be used as a front-end to the
31 django's login system. It receives username and password fields
32 from a POST request and tries to login the user.
34 If login is successful, user will be redirected to the referer
35 address, otherwise will be redirected to /?login_failed.
38 manipulator = AuthenticationForm(request)
40 errors = manipulator.get_validation_errors(request.POST)
41 got_user = manipulator.get_user()
43 auth.login(request, got_user)
45 request.session.delete_test_cookie()
48 return HttpResponseRedirect('/')
50 return HttpResponseRedirect('/?login_failed')
52 request.session.set_test_cookie()
53 return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
56 """Simple front-end to django's logout stuff. This function should
57 be mapped to an url and simply called without any parameter.
60 return HttpResponseRedirect('/')
62 def lecturer_details(request, lid):
63 """Shows a simple form containing all editable fields of a
64 lecturer and gives the lecturer the possibility to save them =)
66 if not hasattr(request.user, 'palestrante_set'):
69 entity = request.user.palestrante_set.get()
70 if entity.id != int(lid):
73 FormKlass = form_for_instance(entity)
74 del FormKlass.base_fields['usuario']
76 form = FormKlass(request.POST or None)
77 if request.POST and form.is_valid():
81 return render_to_response('eventos/lecturer-details.html', Context(c),
82 context_instance=RequestContext(request))
84 def lecturer_talks(request, lid):
85 """Lists all talks of a lecturer (based on lecturer id -- lid
88 if not hasattr(request.user, 'palestrante_set'):
91 entity = request.user.palestrante_set.get()
92 if entity.id != int(lid):
95 talks = Trabalho.objects.filter(palestrante=entity)
96 c = {'lecturer': entity, 'talks': talks}
97 return render_to_response('eventos/talk-list.html', Context(c),
98 context_instance=RequestContext(request))
100 def talk_details(request, tid):
101 """Shows a form to edit a talk
103 entity = get_object_or_404(Trabalho, pk=tid)
104 FormKlass = form_for_instance(entity)
105 form = FormKlass(request.POST or None)
106 if request.POST and form.is_valid():
110 return render_to_response('eventos/talk-details.html', Context(c),
111 context_instance=RequestContext(request))
113 def talk_delete(request, tid):
114 """Drops a talk but only if the logged in user is its owner.
116 if not hasattr(request.user, 'palestrante_set'):
119 entity = request.user.palestrante_set.get()
120 if entity.id != int(lid):
123 owner = Trabalho.objects.filter(pk=tid, palestrante=entity)
128 return HttpResponseRedirect('/lecturer/%d/talks/' % entity.id)
130 def talk_add(request):
131 """Shows a form to the lecturer send a talk
133 if not hasattr(request.user, 'palestrante_set'):
136 entity = request.user.palestrante_set.get()
137 if entity.id != int(lid):
140 FormKlass = form_for_model(Trabalho)
141 form = FormKlass(request.POST or None)
143 other = Palestrante.objects.exclude(pk=entity.id)
144 form.fields['palestrante'].label = u'Outros Palestrantes'
145 form.fields['palestrante'].required = False
146 form.fields['palestrante']._set_queryset(other)
148 if request.POST and form.is_valid():
149 instance = form.save()
150 instance.palestrante.add(entity)
151 return HttpResponseRedirect('/lecturer/%d/talks/' % entity.id)
154 return render_to_response('eventos/talk-add.html', Context(c),
155 context_instance=RequestContext(request))