adicionando link para inscrição
[cascardo/eventmanager.git] / views.py
index 8573791..1d97374 100644 (file)
--- a/views.py
+++ b/views.py
@@ -1,3 +1,4 @@
+# -*- coding: utf8; -*-
 """
 Copyright (C) 2007 Lincoln de Sousa <lincoln@archlinux-br.org>
 
@@ -18,26 +19,123 @@ Boston, MA 02111-1307, USA.
 """
 from django.shortcuts import render_to_response
 from django.template import RequestContext, Context
+from django.contrib.auth.decorators import login_required
+from django.contrib.auth.models import Group, User
+from django.contrib.auth.forms import AuthenticationForm
+from django.contrib.auth import login
+
 from eventmanager.decorators import enable_login_form
 from eventmanager.forms import InscreverPalestra, CadastroPalestrante
-from eventmanager.conteudo.models import Noticia
+from eventmanager.conteudo.models import Noticia, Menu, Secao
+from eventmanager.eventos.models import *
 
 @enable_login_form
 def index(request):
     news = Noticia.objects.order_by('-data_criacao')
-    c = Context({'news': news})
+    menus = Menu.objects.all()
+    index_sections = Secao.objects.filter(index=True)
+
+    c = Context({'news': news, 'menu': menus,
+        'index_sections': index_sections})
     return render_to_response('index.html', c,
             context_instance=RequestContext(request))
 
+
 @enable_login_form
 def cadastro(request):
-    form = CadastroPalestrante()
-    c = Context({'form': form})
-    return render_to_response('cadastro.html', c,
+    # context extension
+    c = {}
+
+    if request.POST:
+        # django's newforms lib requires a new instance of a form to be bounded
+        # with data, to be validated and used.
+        form = CadastroPalestrante(request.POST)
+        if form.is_valid():
+            cd = form.cleaned_data
+            wrong = False
+
+            # XXX: really ugly hack to use django validation machinary...
+            badattr = form._BaseForm__errors
+
+            if not cd['telefone_comercial'] and \
+               not cd['telefone_residencial'] and \
+               not cd['telefone_celular']:
+                badattr['telefone_comercial'] = ['Algum número de telefone '
+                                                 'precisa ser informado']
+                wrong = True
+
+            # don't save duplicated users...
+            try:
+                User.objects.get(username=cd['nome_usuario'])
+                badattr['nome_usuario'] = ['Este nome de usuário já existe!']
+                wrong = True
+            except User.DoesNotExist:
+                pass
+
+            if not wrong:
+                group = Group.objects.get_or_create(name='palestrantes')[0]
+
+                p = Palestrante()
+                p.user = User(username=cd['nome_usuario'], email=cd['email'])
+                p.user.set_password(cd['senha'])
+                p.user.save()
+                p.user.groups.add(group)
+
+                p.nome = cd['nome_completo']
+                p.email = cd['email']
+                p.telefone_comercial = cd['telefone_comercial']
+                p.telefone_residencial = cd['telefone_residencial']
+                p.telefone_celular = cd['telefone_celular']
+                p.rua = cd['rua']
+                p.numero = cd['numero']
+                p.bairro = cd['bairro']
+                p.cidade = cd['cidade']
+                p.uf = cd['uf']
+                p.minicurriculo = cd['minicurriculo']
+                p.save()
+
+                for i in cd['area_interesse']:
+                    p.area_interesse.add(i)
+
+                c.update({'ok': 1})
+
+                fakepost = request.POST.copy()
+                fakepost['username'] = cd['nome_usuario']
+                fakepost['password'] = cd['senha']
+
+                manipulator = AuthenticationForm(request)
+                errors = manipulator.get_validation_errors(fakepost)
+                got_user = manipulator.get_user()
+                login(request, got_user)
+    else:
+        form = CadastroPalestrante()
+
+    c.update({'form': form})
+    return render_to_response('cadastro.html', Context(c),
             context_instance=RequestContext(request))
 
+
+@login_required
 def inscrever_palestra(request):
-    form = InscreverPalestra()
-    c = Context({'form': form})
-    return render_to_response('inscrever_palestra.html', c,
+    c = {}
+    if request.POST:
+        form = InscreverPalestra(request.POST)
+        if form.is_valid():
+            cd = form.cleaned_data
+            p = Palestra()
+            p.titulo = cd['titulo']
+            p.tema = cd['tema']
+            p.categoria = CategoriaPalestra.objects.get(pk=cd['categoria'])
+            p.descricao_curta = cd['descricao_curta']
+            p.descricao_longa = cd['descricao_longa']
+            p.evento = Evento.objects.get(pk=1) # let the hammer play arround!
+            p.save()
+
+            up = User.objects.get(pk=request.user.id)
+            p.palestrante.add()
+    else:
+        form = InscreverPalestra()
+    c.update({'form': form})
+
+    return render_to_response('inscrever_palestra.html', Context(c),
             context_instance=RequestContext(request))