from django.http import HttpResponseRedirect, HttpResponseForbidden
from django.contrib import auth
from django.contrib.auth.forms import AuthenticationForm
-from django.newforms import form_for_instance, form_for_model, HiddenInput
+from django.contrib.auth.models import User, Group
+from django.newforms import form_for_instance, form_for_model
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext, Context, loader
from eventos.models import Palestrante, Trabalho
+from eventos.forms import RegisterLecturer
forbidden = \
HttpResponseForbidden('<h2>You are not allowed to do this action.<h2>')
auth.logout(request)
return HttpResponseRedirect('/')
+def lecturer_add(request):
+ """Adds a new lecturer to the system.
+ """
+ uform = RegisterLecturer(request.POST or None)
+
+ FormKlass = form_for_model(Palestrante)
+ form = FormKlass(request.POST or None)
+ del form.fields['usuario']
+
+ if request.POST and form.is_valid() and uform.is_valid():
+ cd = uform.cleaned_data
+ group = Group.objects.get_or_create(name='palestrantes')[0]
+
+ # creating the user that will be set as the user of the
+ # lecturer.
+ user = User(username=cd['username'])
+ user.set_password(cd['password1'])
+ user.is_active = True
+ user.save()
+ user.groups.add(group)
+
+ # this commit=False is to avoid IntegritErrors, because at
+ # this point, the lecturer doesn't have an user associated
+ # with it.
+ instance = form.save(commit=False)
+ instance.usuario = user
+ instance.save()
+ return HttpResponseRedirect('/')
+
+ c = {'form': form, 'uform': uform}
+ return render_to_response('eventos/lecturer-add.html', Context(c),
+ context_instance=RequestContext(request))
+
def lecturer_details(request, lid):
"""Shows a simple form containing all editable fields of a
lecturer and gives the lecturer the possibility to save them =)