# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
+from django import newforms as forms
+from django.newforms import PasswordInput, ValidationError
+from django.contrib.auth.models import User
+
+
+class RegisterLecturer(forms.Form):
+ username = forms.CharField(max_length=20, label=u'Usuário para login')
+ password1 = forms.CharField(widget=PasswordInput, label='Senha')
+ password2 = forms.CharField(widget=PasswordInput, label='Confirmar Senha')
+
+ def clean_password2(self):
+ if self.cleaned_data['password1'] != self.cleaned_data['password2']:
+ raise ValidationError('A confirmação não confere com a senha')
+ return self.cleaned_data['password2']
+
+ def clean_username(self):
+ try:
+ User.objects.get(username=self.cleaned_data['username'])
+ raise ValidationError('Já existe um usuário com esse nome')
+ except User.DoesNotExist:
+ return self.cleaned_data['username']
urlpatterns = patterns('',
(r'^login/', 'eventos.views.login'),
(r'^logout/', 'eventos.views.logout'),
+ (r'^lecturer/add/$', 'eventos.views.lecturer_add'),
(r'^lecturer/(\d+)/$', 'eventos.views.lecturer_details'),
(r'^lecturer/(\d+)/talks/$', 'eventos.views.lecturer_talks'),
(r'^talks/(\d+)/$', 'eventos.views.talk_details'),
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 =)