-def cadastro(request):
- 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
-
- 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]
-
- user = User(username=cd['nome_usuario'], email=cd['email'])
- user.set_password(cd['senha'])
- user.save()
- user.groups.add(group)
-
- p = Palestrante()
- p.usuario = user
-
- 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})
+def cadastro_palestrante(request):
+ form = CadastroPalestrante(request.POST or None)
+ ok = False
+ if request.POST and form.is_valid():
+ cd = form.cleaned_data
+ badattr = form.errors
+ wrong = False
+
+ if not cd['telefone'] and not cd['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
+ transaction.rollback()
+ except User.DoesNotExist:
+ pass
+
+ if cd['senha'] != cd['senha_2']:
+ badattr['senha_2'] = ['A senha não confere']
+ wrong = True
+ transaction.rollback()
+
+ 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.usuario = user
+
+ p.nome = cd['nome_completo']
+ p.email = cd['email']
+ p.telefone = cd['telefone']
+ p.celular = cd['celular']
+ p.instituicao = cd['instituicao']
+ 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.curriculo = cd['curriculo']
+ p.save()
+
+ for i in cd.get('area_interesse', []):
+ p.area_interesse.add(i)
+
+ 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)
+ transaction.commit()
+ ok = True
+ c = {'form': form, 'ok': ok}