1 # -*- coding: utf-8; -*-
3 Copyright (C) 2007 Lincoln de Sousa <lincoln@archlinux-br.org>
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.
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.
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.
20 from django.shortcuts import render_to_response
21 from django.template import RequestContext, Context
22 from django.contrib.auth.decorators import login_required, user_passes_test
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 from django.db import transaction
28 from eventmanager.decorators import enable_login_form
29 from eventmanager.forms import *
30 from eventmanager.conteudo.models import Noticia, Menu, Secao
31 from eventmanager.eventos.models import *
33 def build_response(request, template, extra={}):
35 Shortcut to build a response based on a C{template} and build a standard
36 context to this template. This context contains news itens, a list of menus
37 and a list of sections to be shown on index. But don't worry, this context
38 is extensible through the C{extra} parameter.
40 @param template: Contains the name of a template found in django
42 @type template: C{str}
44 @param extra: Extra variables to be passed to the context being built.
47 news = Noticia.objects.order_by('-data_criacao')
48 menus = Menu.objects.all()
49 index_sections = Secao.objects.filter(index=True)
50 c = {'news': news, 'menu': menus,
51 'index_sections': index_sections}
53 return render_to_response(template, Context(c),
54 context_instance=RequestContext(request))
59 return build_response(request, 'index.html')
62 @transaction.commit_manually
64 def cadastro_palestrante(request):
65 form = CadastroPalestrante(request.POST or None)
67 if request.POST and form.is_valid():
68 cd = form.cleaned_data
72 if not cd['telefone'] and not cd['celular']:
73 badattr['telefone_comercial'] = ['Algum número de telefone '
74 'precisa ser informado']
77 # don't save duplicated users...
79 User.objects.get(username=cd['nome_usuario'])
80 badattr['nome_usuario'] = ['Este nome de usuário já existe!']
82 transaction.rollback()
83 except User.DoesNotExist:
86 if cd['senha'] != cd['senha_2']:
87 badattr['senha_2'] = ['A senha não confere']
89 transaction.rollback()
92 group = Group.objects.get_or_create(name='palestrantes')[0]
94 user = User(username=cd['nome_usuario'], email=cd['email'])
95 user.set_password(cd['senha'])
97 user.groups.add(group)
102 p.nome = cd['nome_completo']
103 p.email = cd['email']
104 p.telefone = cd['telefone']
105 p.celular = cd['celular']
106 p.instituicao = cd['instituicao']
108 p.numero = cd['numero']
109 p.bairro = cd['bairro']
110 p.cidade = cd['cidade']
112 p.minicurriculo = cd['minicurriculo']
113 p.curriculo = cd['curriculo']
116 for i in cd.get('area_interesse', []):
117 p.area_interesse.add(i)
119 fakepost = request.POST.copy()
120 fakepost['username'] = cd['nome_usuario']
121 fakepost['password'] = cd['senha']
123 manipulator = AuthenticationForm(request)
124 errors = manipulator.get_validation_errors(fakepost)
125 got_user = manipulator.get_user()
126 login(request, got_user)
129 c = {'form': form, 'ok': ok}
130 return build_response(request, 'cadastro.html', c)
134 def inscricao(request):
135 form = Inscricao(request.POST or None)
136 return build_response(request, 'inscricao.html', {'form': form})
140 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
141 def submeter_trabalho(request):
142 form = SubmeterTrabalho(request, request.POST or None)
145 if request.POST and form.is_valid():
146 cd = form.cleaned_data
148 t.titulo = cd['titulo']
149 t.tipo = TipoTrabalho.objects.get(pk=cd['tipo'])
150 t.categoria = CategoriaTrabalho.objects.get_or_create(nome='Pendente')[0]
151 t.descricao_curta = cd['descricao_curta']
152 t.descricao_longa = cd['descricao_longa']
153 t.recursos = cd['recursos']
154 t.evento = Evento.objects.get(pk=1) # let the hammer play arround!
157 logged_in = request.user.palestrante_set.get()
158 t.palestrante.add(logged_in)
159 for i in cd.get('outros_palestrantes', []):
160 up = Palestrante.objects.get(pk=int(i))
161 t.palestrante.add(up)
164 c = {'form': form, 'ok': ok}
165 return build_response(request, 'inscrever_palestra.html', c)
169 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
170 def meus_trabalhos(request):
172 p = Palestrante.objects.get(usuario=request.user)
173 except Palestrante.DoesNotExist:
175 c = {'palestrante': 0}
176 return build_response(request, 'meus_trabalhos.html', c)
178 t = Trabalho.objects.filter(palestrante=p)
179 c = {'trabalhos': t, 'palestrante': 1}
180 return build_response(request, 'meus_trabalhos.html', c)
184 def meus_dados(request):
185 form = EditarPalestrante(request.POST or None)
186 palestrante = request.user.palestrante_set.get()
189 for name, field in form.fields.items():
190 field.initial = getattr(palestrante, name)
192 if request.POST and form.is_valid():
193 cd = form.cleaned_data
194 for name, field in form.fields.items():
195 setattr(palestrante, name, cd[name])
199 c = {'form': form, 'ok': ok}
200 return build_response(request, 'editar_palestrante.html', c)
204 def chamada_trabalhos(request):
205 return build_response(request, 'chamada_trabalhos.html')