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, get_object_or_404
21 from django.template import RequestContext, Context, loader
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 import authenticate, login
25 from django.newforms import form_for_instance
26 from django.core.mail import EmailMessage
27 from django.db import transaction
28 from django.http import get_host
29 from django.conf import settings
31 from eventmanager.decorators import enable_login_form
32 from eventmanager.conteudo.models import Noticia, Menu, Secao
33 from eventmanager.eventos.models import *
34 from eventmanager.forms import *
36 from datetime import datetime
39 FROM_EMAIL = 'Emsl 2007 <noreply@minaslivre.org>'
41 def build_response(request, template, extra={}):
43 Shortcut to build a response based on a C{template} and build a standard
44 context to this template. This context contains news itens, a list of menus
45 and a list of sections to be shown on index. But don't worry, this context
46 is extensible through the C{extra} parameter.
48 @param template: Contains the name of a template found in django
50 @type template: C{str}
52 @param extra: Extra variables to be passed to the context being built.
55 news = Noticia.objects.order_by('-data_criacao')
56 menus = Menu.objects.all()
57 index_sections = Secao.objects.filter(index=True)
58 login_failed = 'login_failed' in request.GET
59 c = {'news': news, 'menu': menus,
60 'index_sections': index_sections,
61 'login_failed': login_failed}
63 return render_to_response(template, Context(c),
64 context_instance=RequestContext(request))
69 return build_response(request, 'index.html')
72 @transaction.commit_manually
74 def cadastro_palestrante(request):
75 form = CadastroPalestrante(request.POST or None)
78 if request.POST and form.is_valid():
79 cd = form.cleaned_data
82 group = Group.objects.get_or_create(name='palestrantes')[0]
84 user = User(username=cd['nome_usuario'], email=cd['email'])
85 user.set_password(cd['senha'])
86 user.is_active = False
88 user.groups.add(group)
93 p.nome = cd['nome_completo']
95 p.telefone = cd['telefone']
96 p.celular = cd['celular']
97 p.instituicao = cd['instituicao']
99 p.numero = cd['numero']
100 p.bairro = cd['bairro']
101 p.cidade = cd['cidade']
103 p.minicurriculo = cd['minicurriculo']
104 p.curriculo = cd['curriculo']
107 for i in cd.get('area_interesse', []):
108 p.area_interesse.add(i)
111 md5_email = sha.new(cd['email']).hexdigest()
112 email = '%s <%s>' % (cd['nome_completo'], cd['email'])
113 link = '%s/verificar?c=%s&c1=%s' % (get_host(request),
115 t = loader.get_template('email-palestrante.html')
116 ec = Context(dict(fulano=['nome_usuario'], link=link))
118 # to be shown in template...
119 c.update({'email': email})
121 # XXX: maybe is not a good so prety put things hardcoded =(
122 m = EmailMessage('Encontro Mineiro de Software Livre',
123 t.render(ec), FROM_EMAIL, [email])
127 ['Desculpe mas não pude enviar o email de confirmação']
128 transaction.rollback()
134 return build_response(request, 'cadastro.html', c)
138 def inscricao(request):
139 return build_response(request, 'inscricao.html')
143 @transaction.commit_manually
144 def inscricao_individual(request):
145 form = Inscricao(request.POST or None)
147 if request.POST and form.is_valid():
148 cd = form.cleaned_data
150 group = Group.objects.get_or_create(name='participantes')[0]
152 user = User(username=cd['nome_usuario'], email=cd['email'])
153 user.set_password(cd['senha'])
154 user.is_active = False
156 user.groups.add(group)
159 p.nome = cd['nome_completo']
161 p.email = cd['email']
163 p.numero = cd['numero']
164 p.bairro = cd['bairro']
165 p.cidade = cd['cidade']
169 p.telefone = cd['telefone']
170 p.home_page = cd['home_page']
173 u = authenticate(username=cd['nome_usuario'], password=cd['senha'])
178 c = {'form': form, 'ok': ok}
179 return build_response(request, 'inscricao_individual.html', c)
183 @transaction.commit_manually
184 def inscricao_caravana(request):
185 form = InscricaoCaravana(request.POST or None)
187 if request.POST and form.is_valid():
188 cd = form.cleaned_data
189 group = Group.objects.get_or_create(name='participantes')[0]
191 user = User(username=cd['nome_usuario'], email=cd['email'])
192 user.set_password(cd['senha'])
193 user.is_active = False
195 user.groups.add(group)
199 p.nome = cd['nome_completo']
201 p.email = cd['email']
203 p.numero = cd['numero']
204 p.bairro = cd['bairro']
205 p.cidade = cd['cidade']
209 p.telefone = cd['telefone']
210 p.home_page = cd['home_page']
215 c.participantes = cd['lista_nomes']
219 u = authenticate(username=cd['nome_usuario'], password=cd['senha'])
223 c = {'form': form, 'ok': ok}
224 return build_response(request, 'inscricao_caravana.html', c)
228 def inscricao_boleto(request):
229 # dynamic values of the form
231 today = datetime.date(now)
232 first_date = datetime.date(datetime(2007, 10, 12))
235 p = request.user.participante_set.get()
236 ca = p.caravana_set.all() and p.caravana_set.get()
241 # o número refran deve ser gerado a cada novo boleto e deve ser único,
242 # mesmo para os testes
243 refs = [x.refbanco for x in Participante.objects.all()]
245 while new_ref in refs or new_ref <= settings.MIN_REF_TRAN:
248 # este dado precisa ser persistente para que possa ser comparado logo acima
254 initial['refTran'] = '1458197%s' % str(new_ref).zfill(10)
255 if today < first_date:
256 initial['valor'] = '3500'
257 initial['dtVenc'] = '12102007'
259 # caso seja uma caravana...
260 if ca and len(ca.parsed_participantes()) >= 10:
261 # sim, o valor aqui é 25 -- Desconto
262 initial['valor'] = '%s00' % (len(ca.parsed_participantes()) * 25)
263 c.update({'caravana': 1})
265 initial['valor'] = '5000'
266 initial['dtVenc'] = '17102007'
268 initial['nome'] = p.nome
269 initial['endereco'] = '%s, %s - %s' % (p.rua, p.numero, p.bairro)
270 initial['cidade'] = p.cidade
272 initial['cep'] = p.cep
274 form = Boleto(request.POST or None, initial=initial)
275 c.update({'form': form})
277 return build_response(request, 'inscricao_boleto.html', c)
281 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
282 def submeter_trabalho(request):
283 form = SubmeterTrabalho(request, request.POST or None)
286 if request.POST and form.is_valid():
287 cd = form.cleaned_data
289 t.titulo = cd['titulo']
290 t.tipo = TipoTrabalho.objects.get(pk=cd['tipo'])
291 t.categoria = CategoriaTrabalho.objects.get_or_create(nome='Pendente')[0]
292 t.descricao_curta = cd['descricao_curta']
293 t.descricao_longa = cd['descricao_longa']
294 t.recursos = cd['recursos']
295 t.evento = Evento.objects.get(pk=1) # XXX: let the hammer play arround!
298 logged_in = request.user.palestrante_set.get()
299 t.palestrante.add(logged_in)
300 for i in cd.get('outros_palestrantes', []):
301 up = Palestrante.objects.get(pk=int(i))
302 t.palestrante.add(up)
305 c = {'form': form, 'ok': ok}
306 return build_response(request, 'inscrever_palestra.html', c)
310 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
311 def meus_trabalhos(request):
313 p = Palestrante.objects.get(usuario=request.user)
314 except Palestrante.DoesNotExist:
316 c = {'palestrante': 0}
317 return build_response(request, 'meus_trabalhos.html', c)
319 t = Trabalho.objects.filter(palestrante=p)
320 c = {'trabalhos': t, 'palestrante': 1}
321 return build_response(request, 'meus_trabalhos.html', c)
325 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
326 def editar_trabalho(request, codigo):
328 p = Palestrante.objects.get(usuario=request.user)
329 except Palestrante.DoesNotExist:
331 c = {'palestrante': 0}
332 return build_response(request, 'meus_trabalhos.html', c)
334 trabalho = get_object_or_404(Trabalho, id=codigo, palestrante=p)
335 Formulario = form_for_instance(trabalho)
337 form = Formulario(request.POST or None)
338 if request.POST and form.is_valid():
340 t = Trabalho.objects.filter(palestrante=p)
341 c = {'trabalhos': t, 'palestrante': 1}
342 c['editado_sucesso'] = trabalho.titulo
343 return build_response(request, 'meus_trabalhos.html', c)
345 c = {'formulario': form}
346 return build_response(request, 'editar_trabalho.html', c)
350 def meus_dados(request):
351 form = EditarPalestrante(request.POST or None)
352 palestrante = request.user.palestrante_set.get()
355 for name, field in form.fields.items():
356 field.initial = getattr(palestrante, name)
358 if request.POST and form.is_valid():
359 cd = form.cleaned_data
360 for name, field in form.fields.items():
361 setattr(palestrante, name, cd[name])
365 c = {'form': form, 'ok': ok}
366 return build_response(request, 'editar_palestrante.html', c)
370 def chamada_trabalhos(request):
371 return build_response(request, 'chamada_trabalhos.html')
374 def avaliacao(request):
375 return build_response(request, 'avaliacao.html')