vixi, mesma mensagem do commit anterior...
[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     menu_sections = Secao.objects.filter(index=False)
38
39     c = Context({'news': news, 'menu': menus, 'menu_sections': menu_sections,
40         'index_sections': index_sections})
41     return render_to_response('index.html', c,
42             context_instance=RequestContext(request))
43
44
45 @enable_login_form
46 def cadastro(request):
47     # context extension
48     c = {}
49
50     if request.POST:
51         # django's newforms lib requires a new instance of a form to be bounded
52         # with data, to be validated and used.
53         form = CadastroPalestrante(request.POST)
54         if form.is_valid():
55             cd = form.cleaned_data
56             wrong = False
57
58             # XXX: really ugly hack to use django validation machinary...
59             badattr = form._BaseForm__errors
60
61             if not cd['telefone_comercial'] and \
62                not cd['telefone_residencial'] and \
63                not cd['telefone_celular']:
64                 badattr['telefone_comercial'] = ['Algum número de telefone '
65                                                  'precisa ser informado']
66                 wrong = True
67
68             # don't save duplicated users...
69             try:
70                 User.objects.get(username=cd['nome_usuario'])
71                 badattr['nome_usuario'] = ['Este nome de usuário já existe!']
72                 wrong = True
73             except User.DoesNotExist:
74                 pass
75
76             if not wrong:
77                 group = Group.objects.get_or_create(name='palestrantes')[0]
78
79                 p = Palestrante()
80                 p.user = User(username=cd['nome_usuario'], email=cd['email'])
81                 p.user.set_password(cd['senha'])
82                 p.user.save()
83                 p.user.groups.add(group)
84
85                 p.nome = cd['nome_completo']
86                 p.email = cd['email']
87                 p.telefone_comercial = cd['telefone_comercial']
88                 p.telefone_residencial = cd['telefone_residencial']
89                 p.telefone_celular = cd['telefone_celular']
90                 p.rua = cd['rua']
91                 p.numero = cd['numero']
92                 p.bairro = cd['bairro']
93                 p.cidade = cd['cidade']
94                 p.uf = cd['uf']
95                 p.minicurriculo = cd['minicurriculo']
96                 p.save()
97
98                 for i in cd['area_interesse']:
99                     p.area_interesse.add(i)
100
101                 c.update({'ok': 1})
102
103                 fakepost = request.POST.copy()
104                 fakepost['username'] = cd['nome_usuario']
105                 fakepost['password'] = cd['senha']
106
107                 manipulator = AuthenticationForm(request)
108                 errors = manipulator.get_validation_errors(fakepost)
109                 got_user = manipulator.get_user()
110                 login(request, got_user)
111     else:
112         form = CadastroPalestrante()
113
114     c.update({'form': form})
115     return render_to_response('cadastro.html', Context(c),
116             context_instance=RequestContext(request))
117
118
119 @login_required
120 def inscrever_palestra(request):
121     c = {}
122     if request.POST:
123         form = InscreverPalestra(request.POST)
124         if form.is_valid():
125             cd = form.cleaned_data
126             p = Palestra()
127             p.titulo = cd['titulo']
128             p.tema = cd['tema']
129             p.categoria = CategoriaPalestra.objects.get(pk=cd['categoria'])
130             p.descricao_curta = cd['descricao_curta']
131             p.descricao_longa = cd['descricao_longa']
132             p.evento = Evento.objects.get(pk=1) # let the hammer play arround!
133             p.save()
134
135             up = User.objects.get(pk=request.user.id)
136             p.palestrante.add()
137     else:
138         form = InscreverPalestra()
139     c.update({'form': form})
140
141     return render_to_response('inscrever_palestra.html', Context(c),
142             context_instance=RequestContext(request))