arrumando o cadastro de palestrantes
[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 *
29 from eventmanager.conteudo.models import Noticia, Menu, Secao
30 from eventmanager.eventos.models import *
31
32 def build_response(request, template, extra={}):
33     news = Noticia.objects.order_by('-data_criacao')
34     menus = Menu.objects.all()
35     index_sections = Secao.objects.filter(index=True)
36     c = {'news': news, 'menu': menus,
37         'index_sections': index_sections}
38     c.update(extra)
39     return render_to_response(template, Context(c),
40             context_instance=RequestContext(request))
41
42
43 @enable_login_form
44 def index(request):
45     return build_response(request, 'index.html')
46
47
48 @enable_login_form
49 def cadastro(request):
50     c = {}
51     if request.POST:
52         # django's newforms lib requires a new instance of a form to be bounded
53         # with data, to be validated and used.
54         form = CadastroPalestrante(request.POST)
55         if form.is_valid():
56             cd = form.cleaned_data
57             badattr = form.errors
58             wrong = False
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                 user = User(username=cd['nome_usuario'], email=cd['email'])
79                 user.set_password(cd['senha'])
80                 user.save()
81                 user.groups.add(group)
82
83                 p = Palestrante()
84                 p.usuario = user
85
86                 p.nome = cd['nome_completo']
87                 p.email = cd['email']
88                 p.telefone_comercial = cd['telefone_comercial']
89                 p.telefone_residencial = cd['telefone_residencial']
90                 p.telefone_celular = cd['telefone_celular']
91                 p.rua = cd['rua']
92                 p.numero = cd['numero']
93                 p.bairro = cd['bairro']
94                 p.cidade = cd['cidade']
95                 p.uf = cd['uf']
96                 p.minicurriculo = cd['minicurriculo']
97                 p.save()
98
99                 for i in cd['area_interesse']:
100                     p.area_interesse.add(i)
101
102                 c.update({'ok': 1})
103
104                 fakepost = request.POST.copy()
105                 fakepost['username'] = cd['nome_usuario']
106                 fakepost['password'] = cd['senha']
107
108                 manipulator = AuthenticationForm(request)
109                 errors = manipulator.get_validation_errors(fakepost)
110                 got_user = manipulator.get_user()
111                 login(request, got_user)
112     else:
113         form = CadastroPalestrante()
114     c.update({'form': form})
115     return build_response(request, 'cadastro.html', c)
116
117
118 @enable_login_form
119 def inscricao(request):
120     if request.POST:
121         form = Inscricao(request.POST)
122     else:
123         form = Inscricao()
124     return build_response(request, 'inscricao.html', {'form': form})
125
126
127 @login_required
128 def inscrever_palestra(request):
129     if request.POST:
130         form = InscreverPalestra(request.POST)
131         if form.is_valid():
132             cd = form.cleaned_data
133             p = Palestra()
134             p.titulo = cd['titulo']
135             p.tema = cd['tema']
136             p.categoria = CategoriaPalestra.objects.get(pk=cd['categoria'])
137             p.descricao_curta = cd['descricao_curta']
138             p.descricao_longa = cd['descricao_longa']
139             p.evento = Evento.objects.get(pk=1) # let the hammer play arround!
140             p.save()
141
142             up = User.objects.get(pk=request.user.id)
143             p.palestrante.add()
144     else:
145         form = InscreverPalestra()
146     return build_response(request, 'inscrever_palestra.html', {'form': form})