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 """This is a function that will be used as a front-end to the
28 django's login system. It receives username and password fields
29 from a POST request and tries to login the user.
31 If login is successful, user will be redirected to the referer
32 address, otherwise will be redirected to /?login_failed.
35 manipulator = AuthenticationForm(request)
37 errors = manipulator.get_validation_errors(request.POST)
38 got_user = manipulator.get_user()
40 auth.login(request, got_user)
42 request.session.delete_test_cookie()
45 return HttpResponseRedirect('/')
47 return HttpResponseRedirect('/?login_failed')
49 request.session.set_test_cookie()
50 return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
53 """Simple front-end to django's logout stuff. This function should
54 be mapped to an url and simply called without any parameter.
57 return HttpResponseRedirect('/')
59 def lecturer_details(request, lid):
60 """Shows a simple form containing all editable fields of a
61 lecturer and gives the lecturer the possibility to save them =)
63 entity = request.user.palestrante_set.get()
64 # avoiding problems if some other user tries to edit the lecturer
66 if entity.id != int(lid):
67 return HttpResponseForbidden('<h2>You are not '
71 FormKlass = form_for_instance(entity)
72 del FormKlass.base_fields['usuario']
74 form = FormKlass(request.POST or None)
75 if request.POST and form.is_valid():
79 return render_to_response('eventos/lecturer-details.html', Context(c),
80 context_instance=RequestContext(request))
82 def lecturer_talks(request, lid):
83 """Lists all talks of a lecturer (based on lecturer id -- lid
86 lecturer = get_object_or_404(Palestrante, pk=lid)
87 talks = Trabalho.objects.filter(palestrante=lecturer)
88 c = {'lecturer': lecturer, 'talks': talks}
89 return render_to_response('eventos/talk-list.html', Context(c),
90 context_instance=RequestContext(request))
92 def talk_details(request, tid):
93 """Shows a form to edit a talk
95 entity = get_object_or_404(Trabalho, pk=tid)
96 FormKlass = form_for_instance(entity)
97 form = FormKlass(request.POST or None)
98 if request.POST and form.is_valid():
102 return render_to_response('eventos/talk-details.html', Context(c),
103 context_instance=RequestContext(request))
105 def talk_delete(request, tid):
106 """Drops a talk but only if the logged in user is its owner.
108 entity = get_object_or_404(Trabalho, pk=tid)
109 palestrante = request.user.palestrante_set.get()
110 owner = Trabalho.objects.filter(pk=tid, palestrante=palestrante)
112 return HttpResponseForbidden('<h2>You are not '
116 return HttpResponseRedirect('/lecturer/%d/talks/' % palestrante.id)
118 def talk_add(request):
119 """Shows a form to the lecturer send a talk
121 palestrante = request.user.palestrante_set.get()
122 FormKlass = form_for_model(Trabalho)
123 form = FormKlass(request.POST or None)
125 other = Palestrante.objects.exclude(pk=palestrante.id)
126 form.fields['palestrante'].label = u'Outros Palestrantes'
127 form.fields['palestrante'].required = False
128 form.fields['palestrante']._set_queryset(other)
130 if request.POST and form.is_valid():
131 instance = form.save()
132 instance.palestrante.add(palestrante)
133 return HttpResponseRedirect('/lecturer/%d/talks/' % palestrante.id)
136 return render_to_response('eventos/talk-add.html', Context(c),
137 context_instance=RequestContext(request))