preventing unwanted users to see private info
[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 forbidden = \
27     HttpResponseForbidden('<h2>You are not allowed to do this action.<h2>')
28
29 def login(request):
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.
33
34     If login is successful, user will be redirected to the referer
35     address, otherwise will be redirected to /?login_failed.
36     """
37     errors = {}
38     manipulator = AuthenticationForm(request)
39     if request.POST:
40         errors = manipulator.get_validation_errors(request.POST)
41         got_user = manipulator.get_user()
42         if got_user:
43             auth.login(request, got_user)
44             try:
45                 request.session.delete_test_cookie()
46             except KeyError:
47                 pass
48             return HttpResponseRedirect('/')
49         else:
50             return HttpResponseRedirect('/?login_failed')
51
52     request.session.set_test_cookie()
53     return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
54
55 def logout(request):
56     """Simple front-end to django's logout stuff. This function should
57     be mapped to an url and simply called without any parameter.
58     """
59     auth.logout(request)
60     return HttpResponseRedirect('/')
61
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 =)
65     """
66     if not hasattr(request.user, 'palestrante_set'):
67         return forbidden
68
69     entity = request.user.palestrante_set.get()
70     if entity.id != int(lid):
71         return forbidden
72
73     FormKlass = form_for_instance(entity)
74     del FormKlass.base_fields['usuario']
75
76     form = FormKlass(request.POST or None)
77     if request.POST and form.is_valid():
78         form.save()
79
80     c = {'form': form}
81     return render_to_response('eventos/lecturer-details.html', Context(c),
82                               context_instance=RequestContext(request))
83
84 def lecturer_talks(request, lid):
85     """Lists all talks of a lecturer (based on lecturer id -- lid
86     parameter).
87     """
88     if not hasattr(request.user, 'palestrante_set'):
89         return forbidden
90
91     entity = request.user.palestrante_set.get()
92     if entity.id != int(lid):
93         return forbidden
94
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))
99
100 def talk_details(request, tid):
101     """Shows a form to edit a talk
102     """
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():
107         form.save()
108
109     c = {'form': form}
110     return render_to_response('eventos/talk-details.html', Context(c),
111                               context_instance=RequestContext(request))
112
113 def talk_delete(request, tid):
114     """Drops a talk but only if the logged in user is its owner.
115     """
116     if not hasattr(request.user, 'palestrante_set'):
117         return forbidden
118
119     entity = request.user.palestrante_set.get()
120     if entity.id != int(lid):
121         return forbidden
122
123     owner = Trabalho.objects.filter(pk=tid, palestrante=entity)
124     if not owner:
125         return forbidden
126
127     entity.delete()
128     return HttpResponseRedirect('/lecturer/%d/talks/' % entity.id)
129
130 def talk_add(request):
131     """Shows a form to the lecturer send a talk
132     """
133     if not hasattr(request.user, 'palestrante_set'):
134         return forbidden
135
136     entity = request.user.palestrante_set.get()
137     if entity.id != int(lid):
138         return forbidden
139
140     FormKlass = form_for_model(Trabalho)
141     form = FormKlass(request.POST or None)
142
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)
147
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)
152
153     c = {'form': form}
154     return render_to_response('eventos/talk-add.html', Context(c),
155                               context_instance=RequestContext(request))