adição da view meus_dados e um cleanup bacana no código de outras views
authorLincoln de Sousa <pythonwarrior@pidinti.localdomain>
Wed, 15 Aug 2007 15:16:41 +0000 (12:16 -0300)
committerLincoln de Sousa <pythonwarrior@pidinti.localdomain>
Wed, 15 Aug 2007 15:16:41 +0000 (12:16 -0300)
views.py

index 677bbc0..b28f98f 100644 (file)
--- a/views.py
+++ b/views.py
@@ -62,114 +62,106 @@ def index(request):
 @transaction.commit_manually
 @enable_login_form
 def cadastro_palestrante(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'] 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.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.get('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)
-                transaction.commit()
-    else:
-        form = CadastroPalestrante()
-    c.update({'form': form})
+    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}
     return build_response(request, 'cadastro.html', c)
 
 
 @enable_login_form
 def inscricao(request):
-    if request.POST:
-        form = Inscricao(request.POST)
-    else:
-        form = Inscricao()
+    form = Inscricao(request.POST or None)
     return build_response(request, 'inscricao.html', {'form': form})
 
 
 @login_required
 @user_passes_test(lambda u:u.palestrante_set.count() == 1, login_url='/')
 def submeter_trabalho(request):
-    c = {}
-    if request.POST:
-        form = SubmeterTrabalho(request.POST)
-        if form.is_valid():
-            cd = form.cleaned_data
-            p = Trabalho()
-            p.titulo = cd['titulo']
-            p.tipo = TipoTrabalho.objects.get(pk=cd['tipo'])
-            p.categoria = CategoriaTrabalho.objects.get(pk=cd['categoria'])
-            p.descricao_curta = cd['descricao_curta']
-            p.descricao_longa = cd['descricao_longa']
-            p.recursos = cd['recursos']
-            p.evento = Evento.objects.get(pk=1) # let the hammer play arround!
-            p.save()
-
-            logged_in = request.user.palestrante_set.get()
-            p.palestrante.add(logged_in)
-            for i in cd.get('outros_palestrantes', []):
-                up = Palestrante.objects.get(pk=int(i))
-                p.palestrante.add(up)
-            c.update({'ok': 1})
-    else:
-        form = SubmeterTrabalho()
-    c.update({'form': form})
+    form = SubmeterTrabalho(request.POST or None)
+    ok = False
+
+    if request.POST and form.is_valid():
+        cd = form.cleaned_data
+        t = Trabalho()
+        t.titulo = cd['titulo']
+        t.tipo = TipoTrabalho.objects.get(pk=cd['tipo'])
+        t.categoria = CategoriaTrabalho.objects.get(pk=cd['categoria'])
+        t.descricao_curta = cd['descricao_curta']
+        t.descricao_longa = cd['descricao_longa']
+        t.recursos = cd['recursos']
+        t.evento = Evento.objects.get(pk=1) # let the hammer play arround!
+        t.save()
+
+        logged_in = request.user.palestrante_set.get()
+        t.palestrante.add(logged_in)
+        for i in cd.get('outros_palestrantes', []):
+            up = Palestrante.objects.get(pk=int(i))
+            t.palestrante.add(up)
+        ok = True
+
+    c = {'form': form, 'ok': ok}
     return build_response(request, 'inscrever_palestra.html', c)
 
 
@@ -188,6 +180,26 @@ def meus_trabalhos(request):
     return build_response(request, 'meus_trabalhos.html', c)
 
 
+@login_required
+def meus_dados(request):
+    form = EditarPalestrante(request.POST or None)
+    palestrante = request.user.palestrante_set.get()
+    ok = False
+
+    for name, field in form.fields.items():
+        field.initial = getattr(palestrante, name)
+
+    if request.POST and form.is_valid():
+        cd = form.cleaned_data
+        for name, field in form.fields.items():
+            setattr(palestrante, name, cd[name])
+        palestrante.save()
+        ok = True
+
+    c = {'form': form, 'ok': ok}
+    return build_response(request, 'editar_palestrante.html', c)
+
+
 @enable_login_form
 def chamada_trabalhos(request):
     return build_response(request, 'chamada_trabalhos.html')