removendo variável desnecessária do contexto
[cascardo/eventmanager.git] / views.py
1 # -*- coding: utf8; -*-
2 """
3 Copyright (C) 2007 Lincoln de Sousa <lincoln@archlinux-br.org>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 """
20 from django.shortcuts import render_to_response
21 from django.template import RequestContext, Context
22 from django.contrib.auth.decorators import login_required
23 from django.contrib.auth.models import Group, User
24 from django.contrib.auth.forms import AuthenticationForm
25 from django.contrib.auth import login
26
27 from eventmanager.decorators import enable_login_form
28 from eventmanager.forms import InscreverPalestra, CadastroPalestrante
29 from eventmanager.conteudo.models import Noticia, Menu, Secao
30 from eventmanager.eventos.models import *
31
32 @enable_login_form
33 def index(request):
34     news = Noticia.objects.order_by('-data_criacao')
35     menus = Menu.objects.all()
36     index_sections = Secao.objects.filter(index=True)
37
38     c = Context({'news': news, 'menu': menus,
39         'index_sections': index_sections})
40     return render_to_response('index.html', c,
41             context_instance=RequestContext(request))
42
43
44 @enable_login_form
45 def cadastro(request):
46     # context extension
47     c = {}
48
49     if request.POST:
50         # django's newforms lib requires a new instance of a form to be bounded
51         # with data, to be validated and used.
52         form = CadastroPalestrante(request.POST)
53         if form.is_valid():
54             cd = form.cleaned_data
55             wrong = False
56
57             # XXX: really ugly hack to use django validation machinary...
58             badattr = form._BaseForm__errors
59
60             if not cd['telefone_comercial'] and \
61                not cd['telefone_residencial'] and \
62                not cd['telefone_celular']:
63                 badattr['telefone_comercial'] = ['Algum número de telefone '
64                                                  'precisa ser informado']
65                 wrong = True
66
67             # don't save duplicated users...
68             try:
69                 User.objects.get(username=cd['nome_usuario'])
70                 badattr['nome_usuario'] = ['Este nome de usuário já existe!']
71                 wrong = True
72             except User.DoesNotExist:
73                 pass
74
75             if not wrong:
76                 group = Group.objects.get_or_create(name='palestrantes')[0]
77
78                 p = Palestrante()
79                 p.user = User(username=cd['nome_usuario'], email=cd['email'])
80                 p.user.set_password(cd['senha'])
81                 p.user.save()
82                 p.user.groups.add(group)
83
84                 p.nome = cd['nome_completo']
85                 p.email = cd['email']
86                 p.telefone_comercial = cd['telefone_comercial']
87                 p.telefone_residencial = cd['telefone_residencial']
88                 p.telefone_celular = cd['telefone_celular']
89                 p.rua = cd['rua']
90                 p.numero = cd['numero']
91                 p.bairro = cd['bairro']
92                 p.cidade = cd['cidade']
93                 p.uf = cd['uf']
94                 p.minicurriculo = cd['minicurriculo']
95                 p.save()
96
97                 for i in cd['area_interesse']:
98                     p.area_interesse.add(i)
99
100                 c.update({'ok': 1})
101
102                 fakepost = request.POST.copy()
103                 fakepost['username'] = cd['nome_usuario']
104                 fakepost['password'] = cd['senha']
105
106                 manipulator = AuthenticationForm(request)
107                 errors = manipulator.get_validation_errors(fakepost)
108                 got_user = manipulator.get_user()
109                 login(request, got_user)
110     else:
111         form = CadastroPalestrante()
112
113     c.update({'form': form})
114     return render_to_response('cadastro.html', Context(c),
115             context_instance=RequestContext(request))
116
117
118 @login_required
119 def inscrever_palestra(request):
120     c = {}
121     if request.POST:
122         form = InscreverPalestra(request.POST)
123         if form.is_valid():
124             cd = form.cleaned_data
125             p = Palestra()
126             p.titulo = cd['titulo']
127             p.tema = cd['tema']
128             p.categoria = CategoriaPalestra.objects.get(pk=cd['categoria'])
129             p.descricao_curta = cd['descricao_curta']
130             p.descricao_longa = cd['descricao_longa']
131             p.evento = Evento.objects.get(pk=1) # let the hammer play arround!
132             p.save()
133
134             up = User.objects.get(pk=request.user.id)
135             p.palestrante.add()
136     else:
137         form = InscreverPalestra()
138     c.update({'form': form})
139
140     return render_to_response('inscrever_palestra.html', Context(c),
141             context_instance=RequestContext(request))