from django.contrib.auth import login
from eventmanager.decorators import enable_login_form
-from eventmanager.forms import InscreverPalestra, CadastroPalestrante
+from eventmanager.forms import *
from eventmanager.conteudo.models import Noticia, Menu, Secao
from eventmanager.eventos.models import *
-@enable_login_form
-def index(request):
+def build_response(request, template, extra={}):
+ """
+ Shortcut to build a response based on a C{template} and build a standard
+ context to this template. This context contains news itens, a list of menus
+ and a list of sections to be shown on index. But don't worry, this context
+ is extensible through the C{extra} parameter.
+
+ @param template: Contains the name of a template found in django
+ C{TEMPLATE_DIRS}
+ @type template: C{str}
+
+ @param extra: Extra variables to be passed to the context being built.
+ @type extra: C{dict}
+ """
news = Noticia.objects.order_by('-data_criacao')
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,
+ c = {'news': news, 'menu': menus,
+ 'index_sections': index_sections}
+ c.update(extra)
+ return render_to_response(template, Context(c),
context_instance=RequestContext(request))
+@enable_login_form
+def index(request):
+ return build_response(request, 'index.html')
+
+
@enable_login_form
def cadastro(request):
- # 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
+ badattr = form.errors
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']:
if not wrong:
group = Group.objects.get_or_create(name='palestrantes')[0]
+ user = User(username=cd['nome_usuario'], email=cd['email'])
+ user.set_password(cd['senha'])
+ user.save()
+ user.groups.add(group)
+
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.usuario = user
p.nome = cd['nome_completo']
p.email = cd['email']
login(request, got_user)
else:
form = CadastroPalestrante()
-
c.update({'form': form})
- return render_to_response('cadastro.html', Context(c),
- context_instance=RequestContext(request))
+ return build_response(request, 'cadastro.html', c)
+
+
+@enable_login_form
+def inscricao(request):
+ if request.POST:
+ form = Inscricao(request.POST)
+ else:
+ form = Inscricao()
+ return build_response(request, 'inscricao.html', {'form': form})
@login_required
def inscrever_palestra(request):
- c = {}
if request.POST:
form = InscreverPalestra(request.POST)
if form.is_valid():
p.palestrante.add()
else:
form = InscreverPalestra()
- c.update({'form': form})
-
- return render_to_response('inscrever_palestra.html', Context(c),
- context_instance=RequestContext(request))
+ return build_response(request, 'inscrever_palestra.html', {'form': form})