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.newforms import form_for_instance
25 from django.core.mail import EmailMessage
26 from django.db import transaction
27 from django.http import get_host
29 from eventmanager.decorators import enable_login_form
30 from eventmanager.forms import *
31 from eventmanager.conteudo.models import Noticia, Menu, Secao
32 from eventmanager.eventos.models import *
35 FROM_EMAIL = 'Emsl 2007 <noreply@minaslivre.org>'
37 def build_response(request, template, extra={}):
39 Shortcut to build a response based on a C{template} and build a standard
40 context to this template. This context contains news itens, a list of menus
41 and a list of sections to be shown on index. But don't worry, this context
42 is extensible through the C{extra} parameter.
44 @param template: Contains the name of a template found in django
46 @type template: C{str}
48 @param extra: Extra variables to be passed to the context being built.
51 news = Noticia.objects.order_by('-data_criacao')
52 menus = Menu.objects.all()
53 index_sections = Secao.objects.filter(index=True)
54 login_failed = 'login_failed' in request.GET
55 c = {'news': news, 'menu': menus,
56 'index_sections': index_sections,
57 'login_failed': login_failed}
59 return render_to_response(template, Context(c),
60 context_instance=RequestContext(request))
65 return build_response(request, 'index.html')
68 @transaction.commit_manually
70 def cadastro_palestrante(request):
71 form = CadastroPalestrante(request.POST or None)
73 if request.POST and form.is_valid():
74 cd = form.cleaned_data
78 if not cd['telefone'] and not cd['celular']:
79 badattr['telefone_comercial'] = \
80 ['Algum número de telefone precisa ser informado']
83 # don't save duplicated users...
85 User.objects.get(username=cd['nome_usuario'])
86 badattr['nome_usuario'] = ['Este nome de usuário já existe!']
88 transaction.rollback()
89 except User.DoesNotExist:
92 if cd['senha'] != cd['senha_2']:
93 badattr['senha_2'] = ['A senha não confere']
95 transaction.rollback()
98 group = Group.objects.get_or_create(name='palestrantes')[0]
100 user = User(username=cd['nome_usuario'], email=cd['email'])
101 user.set_password(cd['senha'])
102 user.is_active = False
104 user.groups.add(group)
109 p.nome = cd['nome_completo']
110 p.email = cd['email']
111 p.telefone = cd['telefone']
112 p.celular = cd['celular']
113 p.instituicao = cd['instituicao']
115 p.numero = cd['numero']
116 p.bairro = cd['bairro']
117 p.cidade = cd['cidade']
119 p.minicurriculo = cd['minicurriculo']
120 p.curriculo = cd['curriculo']
123 for i in cd.get('area_interesse', []):
124 p.area_interesse.add(i)
127 md5_email = sha.new(cd['email']).hexdigest()
128 email = '%s <%s>' % (cd['nome_completo'], cd['email'])
129 link = '%s/verificar?c=%s&c1=%s' % (get_host(request),
131 t = loader.get_template('email-palestrante.html')
132 c = Context(dict(fulano=['nome_usuario'], link=link))
134 m = EmailMessage('Encontro Mineiro de Software Livre',
135 t.render(c), FROM_EMAIL, [email])
139 ['Desculpe mas não pude enviar o email de confirmação']
140 transaction.rollback()
145 c = {'form': form, 'ok': ok}
146 return build_response(request, 'cadastro.html', c)
150 def inscricao(request):
151 return build_response(request, 'inscricao.html')
155 @transaction.commit_manually
156 def inscricao_individual(request):
157 form = Inscricao(request.POST or None)
159 if request.POST and form.is_valid():
160 cd = form.cleaned_data
161 badattr = form.errors
164 # don't save duplicated users...
166 User.objects.get(username=cd['nome_usuario'])
167 badattr['nome_usuario'] = ['Este nome de usuário já existe!']
169 transaction.rollback()
170 except User.DoesNotExist:
173 if cd['senha'] != cd['senha_2']:
174 badattr['senha_2'] = ['A senha não confere']
176 transaction.rollback()
179 group = Group.objects.get_or_create(name='participantes')[0]
181 user = User(username=cd['nome_usuario'], email=cd['email'])
182 user.set_password(cd['senha'])
183 user.is_active = False
185 user.groups.add(group)
189 return build_response(request, 'inscricao_individual.html', {'form': form})
193 def inscricao_caravana(request):
194 form_coordenador = InscricaoCoordenador(request.POST or None)
195 form = InscricaoCaravana(request.POST or None)
196 return build_response(request, 'inscricao_caravana.html',
197 {'form_coordenador': form_coordenador, 'form': form})
201 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
202 def submeter_trabalho(request):
203 form = SubmeterTrabalho(request, request.POST or None)
206 if request.POST and form.is_valid():
207 cd = form.cleaned_data
209 t.titulo = cd['titulo']
210 t.tipo = TipoTrabalho.objects.get(pk=cd['tipo'])
211 t.categoria = CategoriaTrabalho.objects.get_or_create(nome='Pendente')[0]
212 t.descricao_curta = cd['descricao_curta']
213 t.descricao_longa = cd['descricao_longa']
214 t.recursos = cd['recursos']
215 t.evento = Evento.objects.get(pk=1) # let the hammer play arround!
218 logged_in = request.user.palestrante_set.get()
219 t.palestrante.add(logged_in)
220 for i in cd.get('outros_palestrantes', []):
221 up = Palestrante.objects.get(pk=int(i))
222 t.palestrante.add(up)
225 c = {'form': form, 'ok': ok}
226 return build_response(request, 'inscrever_palestra.html', c)
230 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
231 def meus_trabalhos(request):
233 p = Palestrante.objects.get(usuario=request.user)
234 except Palestrante.DoesNotExist:
236 c = {'palestrante': 0}
237 return build_response(request, 'meus_trabalhos.html', c)
239 t = Trabalho.objects.filter(palestrante=p)
240 c = {'trabalhos': t, 'palestrante': 1}
241 return build_response(request, 'meus_trabalhos.html', c)
245 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
246 def editar_trabalho(request, codigo):
248 p = Palestrante.objects.get(usuario=request.user)
249 except Palestrante.DoesNotExist:
251 c = {'palestrante': 0}
252 return build_response(request, 'meus_trabalhos.html', c)
253 trabalho = get_object_or_404(Trabalho, id=codigo, palestrante=p)
254 Formulario = form_for_instance(trabalho)
255 if request.method == 'POST':
256 form = Formulario(request.POST)
259 t = Trabalho.objects.filter(palestrante=p)
260 c = {'trabalhos': t, 'palestrante': 1}
261 c['editado_sucesso']=trabalho.titulo
262 return build_response(request, 'meus_trabalhos.html', c)
266 c = {'formulario':form}
267 return build_response(request, 'editar_trabalho.html', c)
271 def meus_dados(request):
272 form = EditarPalestrante(request.POST or None)
273 palestrante = request.user.palestrante_set.get()
276 for name, field in form.fields.items():
277 field.initial = getattr(palestrante, name)
279 if request.POST and form.is_valid():
280 cd = form.cleaned_data
281 for name, field in form.fields.items():
282 setattr(palestrante, name, cd[name])
286 c = {'form': form, 'ok': ok}
287 return build_response(request, 'editar_palestrante.html', c)
291 def chamada_trabalhos(request):
292 return build_response(request, 'chamada_trabalhos.html')
295 def avaliacao(request):
296 return build_response(request, 'avaliacao.html')