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
149 group = Group.objects.get_or_create(name='participantes')[0]
151 user = User(username=cd['nome_usuario'], email=cd['email'])
152 user.set_password(cd['senha'])
153 user.is_active = False
155 user.groups.add(group)
158 p.nome = cd['nome_completo']
160 p.email = cd['email']
162 p.numero = cd['numero']
163 p.bairro = cd['bairro']
164 p.cidade = cd['cidade']
168 p.telefone = cd['telefone']
169 p.home_page = cd['home_page']
170 p.comercial = cd['inscricao_comercial']
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']
211 p.comercial = False # yeah, always false!
216 c.participantes = cd['lista_nomes']
220 u = authenticate(username=cd['nome_usuario'], password=cd['senha'])
224 c = {'form': form, 'ok': ok}
225 return build_response(request, 'inscricao_caravana.html', c)
229 def inscricao_boleto(request):
230 # dynamic values of the form
232 today = datetime.date(now)
233 first_date = datetime.date(datetime(2007, 10, 12))
236 p = request.user.participante_set.get()
237 ca = p.caravana_set.all() and p.caravana_set.get()
242 # o número refTran deve ser gerado a cada novo boleto e deve ser único,
243 # mesmo para os testes
244 refs = [x.refbanco for x in Participante.objects.all()]
246 while new_ref in refs or new_ref <= settings.MIN_REF_TRAN:
249 # este dado precisa ser persistente para que possa ser comparado logo acima
255 initial['refTran'] = '1458197%s' % str(new_ref).zfill(10)
256 if today < first_date:
257 initial['dtVenc'] = '12102007'
259 initial['valor'] = '3500'
261 initial['valor'] = '8000'
263 # caso seja uma caravana...
264 if ca and len(ca.parsed_participantes()) >= 10:
265 # sim, o valor aqui é 25 -- Desconto
266 initial['valor'] = '%s00' % (len(ca.parsed_participantes()) * 25)
267 c.update({'caravana': 1})
269 initial['valor'] = '5000'
270 initial['dtVenc'] = '17102007'
272 initial['nome'] = p.nome
273 initial['endereco'] = '%s, %s - %s' % (p.rua, p.numero, p.bairro)
274 initial['cidade'] = p.cidade
276 initial['cep'] = p.cep
278 form = Boleto(request.POST or None, initial=initial)
279 c.update({'form': form})
281 return build_response(request, 'inscricao_boleto.html', c)
285 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
286 def submeter_trabalho(request):
287 form = SubmeterTrabalho(request, request.POST or None)
290 if request.POST and form.is_valid():
291 cd = form.cleaned_data
293 t.titulo = cd['titulo']
294 t.tipo = TipoTrabalho.objects.get(pk=cd['tipo'])
295 t.categoria = CategoriaTrabalho.objects.get_or_create(nome='Pendente')[0]
296 t.descricao_curta = cd['descricao_curta']
297 t.descricao_longa = cd['descricao_longa']
298 t.recursos = cd['recursos']
299 t.evento = Evento.objects.get(pk=1) # XXX: let the hammer play arround!
302 logged_in = request.user.palestrante_set.get()
303 t.palestrante.add(logged_in)
304 for i in cd.get('outros_palestrantes', []):
305 up = Palestrante.objects.get(pk=int(i))
306 t.palestrante.add(up)
309 c = {'form': form, 'ok': ok}
310 return build_response(request, 'inscrever_palestra.html', c)
314 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
315 def meus_trabalhos(request):
317 p = Palestrante.objects.get(usuario=request.user)
318 except Palestrante.DoesNotExist:
320 c = {'palestrante': 0}
321 return build_response(request, 'meus_trabalhos.html', c)
322 t = Trabalho.objects.filter(palestrante=p)
323 c = {'trabalhos': t, 'palestrante': 1}
324 return build_response(request, 'meus_trabalhos.html', c)
327 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
328 def editar_trabalho(request,codigo):
330 p = Palestrante.objects.get(usuario=request.user)
331 except Palestrante.DoesNotExist:
333 c = {'palestrante': 0}
334 return build_response(request, 'meus_trabalhos.html', c)
335 trabalho = get_object_or_404(Trabalho, id=codigo,palestrante=p)
336 Formulario = form_for_instance(trabalho)
337 if request.method == 'POST':
338 form = Formulario(request.POST)
341 t = Trabalho.objects.filter(palestrante=p)
342 c = {'trabalhos': t, 'palestrante': 1}
343 c['editado_sucesso']=trabalho.titulo
344 return build_response(request, 'meus_trabalhos.html', c)
348 c = {'formulario':form}
349 return build_response(request, 'editar_trabalho.html', c)
352 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
353 def editar_trabalho(request, codigo):
355 p = Palestrante.objects.get(usuario=request.user)
356 except Palestrante.DoesNotExist:
358 c = {'palestrante': 0}
359 return build_response(request, 'meus_trabalhos.html', c)
361 trabalho = get_object_or_404(Trabalho, id=codigo, palestrante=p)
362 Formulario = form_for_instance(trabalho)
364 form = Formulario(request.POST or None)
365 if request.POST and form.is_valid():
367 t = Trabalho.objects.filter(palestrante=p)
368 c = {'trabalhos': t, 'palestrante': 1}
369 c['editado_sucesso'] = trabalho.titulo
370 return build_response(request, 'meus_trabalhos.html', c)
372 c = {'formulario': form}
373 return build_response(request, 'editar_trabalho.html', c)
377 def meus_dados(request):
378 form = EditarPalestrante(request.POST or None)
379 palestrante = request.user.palestrante_set.get()
382 for name, field in form.fields.items():
383 field.initial = getattr(palestrante, name)
385 if request.POST and form.is_valid():
386 cd = form.cleaned_data
387 for name, field in form.fields.items():
388 setattr(palestrante, name, cd[name])
392 c = {'form': form, 'ok': ok}
393 return build_response(request, 'editar_palestrante.html', c)
397 def chamada_trabalhos(request):
398 return build_response(request, 'chamada_trabalhos.html')
401 def avaliacao(request):
402 return build_response(request, 'avaliacao.html')