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']
171 p.cpf_cnpj = cd['cpf_cnpj']
174 u = authenticate(username=cd['nome_usuario'], password=cd['senha'])
179 c = {'form': form, 'ok': ok}
180 return build_response(request, 'inscricao_individual.html', c)
184 @transaction.commit_manually
185 def inscricao_caravana(request):
186 form = InscricaoCaravana(request.POST or None)
188 if request.POST and form.is_valid():
189 cd = form.cleaned_data
190 group = Group.objects.get_or_create(name='participantes')[0]
192 user = User(username=cd['nome_usuario'], email=cd['email'])
193 user.set_password(cd['senha'])
194 user.is_active = False
196 user.groups.add(group)
200 p.nome = cd['nome_completo']
202 p.email = cd['email']
204 p.numero = cd['numero']
205 p.bairro = cd['bairro']
206 p.cidade = cd['cidade']
210 p.telefone = cd['telefone']
211 p.home_page = cd['home_page']
212 p.comercial = False # yeah, always false!
218 c.participantes = cd['lista_nomes']
222 u = authenticate(username=cd['nome_usuario'], password=cd['senha'])
226 c = {'form': form, 'ok': ok}
227 return build_response(request, 'inscricao_caravana.html', c)
231 def inscricao_boleto(request):
232 # dynamic values of the form
234 today = datetime.date(now)
235 first_date = datetime.date(datetime(2007, 10, 12))
238 p = request.user.participante_set.get()
239 ca = p.caravana_set.all() and p.caravana_set.get()
244 # o número refTran deve ser gerado a cada novo boleto e deve ser único,
245 # mesmo para os testes
246 refs = [x.refbanco for x in Participante.objects.all()]
248 while new_ref in refs or new_ref <= settings.MIN_REF_TRAN:
251 # este dado precisa ser persistente para que possa ser comparado logo acima
257 initial['refTran'] = '1458197%s' % str(new_ref).zfill(10)
258 if today < first_date:
259 initial['dtVenc'] = '12102007'
261 initial['valor'] = '3500'
263 initial['valor'] = '8000'
265 # caso seja uma caravana...
266 if ca and len(ca.parsed_participantes()) >= 10:
267 # sim, o valor aqui é 25 -- Desconto
268 initial['valor'] = '%s00' % (len(ca.parsed_participantes()) * 25)
269 c.update({'caravana': 1})
271 initial['valor'] = '5000'
272 initial['dtVenc'] = '17102007'
274 initial['nome'] = p.nome
275 initial['endereco'] = '%s, %s - %s' % (p.rua, p.numero, p.bairro)
276 initial['cidade'] = p.cidade
278 initial['cep'] = p.cep
280 form = Boleto(request.POST or None, initial=initial)
281 c.update({'form': form})
283 return build_response(request, 'inscricao_boleto.html', c)
287 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
288 def submeter_trabalho(request):
289 form = SubmeterTrabalho(request, request.POST or None)
292 if request.POST and form.is_valid():
293 cd = form.cleaned_data
295 t.titulo = cd['titulo']
296 t.tipo = TipoTrabalho.objects.get(pk=cd['tipo'])
297 t.categoria = CategoriaTrabalho.objects.get_or_create(nome='Pendente')[0]
298 t.descricao_curta = cd['descricao_curta']
299 t.descricao_longa = cd['descricao_longa']
300 t.recursos = cd['recursos']
301 t.evento = Evento.objects.get(pk=1) # XXX: let the hammer play arround!
304 logged_in = request.user.palestrante_set.get()
305 t.palestrante.add(logged_in)
306 for i in cd.get('outros_palestrantes', []):
307 up = Palestrante.objects.get(pk=int(i))
308 t.palestrante.add(up)
311 c = {'form': form, 'ok': ok}
312 return build_response(request, 'inscrever_palestra.html', c)
316 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
317 def meus_trabalhos(request):
319 p = Palestrante.objects.get(usuario=request.user)
320 except Palestrante.DoesNotExist:
322 c = {'palestrante': 0}
323 return build_response(request, 'meus_trabalhos.html', c)
324 t = Trabalho.objects.filter(palestrante=p)
325 c = {'trabalhos': t, 'palestrante': 1}
326 return build_response(request, 'meus_trabalhos.html', c)
329 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
330 def editar_trabalho(request,codigo):
332 p = Palestrante.objects.get(usuario=request.user)
333 except Palestrante.DoesNotExist:
335 c = {'palestrante': 0}
336 return build_response(request, 'meus_trabalhos.html', c)
337 trabalho = get_object_or_404(Trabalho, id=codigo,palestrante=p)
338 Formulario = form_for_instance(trabalho)
339 if request.method == 'POST':
340 form = Formulario(request.POST)
343 t = Trabalho.objects.filter(palestrante=p)
344 c = {'trabalhos': t, 'palestrante': 1}
345 c['editado_sucesso']=trabalho.titulo
346 return build_response(request, 'meus_trabalhos.html', c)
350 c = {'formulario':form}
351 return build_response(request, 'editar_trabalho.html', c)
354 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
355 def editar_trabalho(request, codigo):
357 p = Palestrante.objects.get(usuario=request.user)
358 except Palestrante.DoesNotExist:
360 c = {'palestrante': 0}
361 return build_response(request, 'meus_trabalhos.html', c)
363 trabalho = get_object_or_404(Trabalho, id=codigo, palestrante=p)
364 Formulario = form_for_instance(trabalho)
366 form = Formulario(request.POST or None)
367 if request.POST and form.is_valid():
369 t = Trabalho.objects.filter(palestrante=p)
370 c = {'trabalhos': t, 'palestrante': 1}
371 c['editado_sucesso'] = trabalho.titulo
372 return build_response(request, 'meus_trabalhos.html', c)
374 c = {'formulario': form}
375 return build_response(request, 'editar_trabalho.html', c)
378 def meus_dados(request):
380 entity = request.user.palestrante_set.get()
381 except Palestrante.DoesNotExist:
382 entity = request.user.participante_set.get()
384 FormKlass = form_for_instance(entity)
386 # ugly hammer to hide some fields...
387 del FormKlass.base_fields['usuario']
390 form = FormKlass(request.POST or None)
391 if request.POST and form.is_valid():
395 c = {'form': form, 'ok': ok, 'title': entity.__class__.__name__}
396 return build_response(request, 'editar_usuario.html', c)
400 def chamada_trabalhos(request):
401 return build_response(request, 'chamada_trabalhos.html')
404 def avaliacao(request):
405 return build_response(request, 'avaliacao.html')