setting the owner of a talk when adding it
[cascardo/ema.git] / eventos / views.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2008 Lincoln de Sousa <lincoln@minaslivre.org>
3 #
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.
8 #
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.
13 #
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
25
26 def login(request):
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.
30
31     If login is successful, user will be redirected to the referer
32     address, otherwise will be redirected to /?login_failed.
33     """
34     errors = {}
35     manipulator = AuthenticationForm(request)
36     if request.POST:
37         errors = manipulator.get_validation_errors(request.POST)
38         got_user = manipulator.get_user()
39         if got_user:
40             auth.login(request, got_user)
41             try:
42                 request.session.delete_test_cookie()
43             except KeyError:
44                 pass
45             return HttpResponseRedirect('/')
46         else:
47             return HttpResponseRedirect('/?login_failed')
48
49     request.session.set_test_cookie()
50     return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
51
52 def logout(request):
53     """Simple front-end to django's logout stuff. This function should
54     be mapped to an url and simply called without any parameter.
55     """
56     auth.logout(request)
57     return HttpResponseRedirect('/')
58
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 =)
62     """
63     entity = request.user.palestrante_set.get()
64     # avoiding problems if some other user tries to edit the lecturer
65     # info.
66     if entity.id != int(lid):
67         return HttpResponseForbidden('<h2>You are not '
68                                      'allowed to edit '
69                                      'this info.<h2>')
70
71     FormKlass = form_for_instance(entity)
72     del FormKlass.base_fields['usuario']
73
74     form = FormKlass(request.POST or None)
75     if request.POST and form.is_valid():
76         form.save()
77
78     c = {'form': form}
79     return render_to_response('eventos/lecturer-details.html', Context(c),
80                               context_instance=RequestContext(request))
81
82 def lecturer_talks(request, lid):
83     """Lists all talks of a lecturer (based on lecturer id -- lid
84     parameter).
85     """
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))
91
92 def talk_details(request, tid):
93     """Shows a form to edit a talk
94     """
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():
99         form.save()
100
101     c = {'form': form}
102     return render_to_response('eventos/talk-details.html', Context(c),
103                               context_instance=RequestContext(request))
104
105 def talk_delete(request, tid):
106     """Drops a talk but only if the logged in user is its owner.
107     """
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)
111     if not owner:
112         return HttpResponseForbidden('<h2>You are not '
113                                      'allowed to edit '
114                                      'this info.<h2>')
115     entity.delete()
116     return HttpResponseRedirect('/lecturer/%d/talks/' % palestrante.id)
117
118 def talk_add(request):
119     """Shows a form to the lecturer send a talk
120     """
121     palestrante = request.user.palestrante_set.get()
122     FormKlass = form_for_model(Trabalho)
123     form = FormKlass(request.POST or None)
124
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)
129
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)
134
135     c = {'form': form}
136     return render_to_response('eventos/talk-add.html', Context(c),
137                               context_instance=RequestContext(request))