Ejemplo n.º 1
0
    def post(self, request):
        form = VotanteForm(request.POST)
        if form.is_valid():
            votante = form.get_votante()
            personas = Persona.objects.all()
            habilidades = Habilidad.objects.all()
            votante_form = VotanteForm()
            initial_data = {'edit': True}

            for p in personas:
                for h in habilidades:
                    try:
                        val = Voto.objects.get(persona=p, habilidad=h, votante=votante).valor
                    except Voto.DoesNotExist:
                        val = 0
                    finally:
                        initial_data["%d_%d" % (p.pk, h.pk)] = val

            votos_form = VotosForm(initial=initial_data)

            templates_vars = {
                'personas': personas,
                'habilidades': habilidades,
                'votante_form': votante_form,
                'votos_form': votos_form,
                }
            return render_to_response('appraisal_form.html', templates_vars, context_instance=RequestContext(request))

        return render_to_response(self.template_name, {'form': form}, context_instance=RequestContext(request))
Ejemplo n.º 2
0
    def post(self, request):
        votante_form = VotanteForm(request.POST)
        votos_form = VotosForm(request.POST)
        habilidades = Habilidad.objects.all()
        personas = Persona.objects.all()
        errores = ''

        if votos_form.is_valid() and votante_form.is_valid():
            votante_verify = votante_form.get_votante()
            edit = votos_form.cleaned_data['edit']

            personas_dict = {}
            for p in personas:
                personas_dict[p.pk] = p

            habilidades_dict = {}
            for h in habilidades:
                habilidades_dict[h.pk] = h

            for (key, val) in votos_form.cleaned_data.items():
                if key == 'edit':
                    continue
                p_pk = int(key.split('_')[0])
                h_pk = int(key.split('_')[1])

                if edit:
                    voto, created = Voto.objects.get_or_create(persona=personas_dict[p_pk], habilidad=habilidades_dict[h_pk], votante=votante_verify)
                    voto.valor = val
                    voto.save()
                else:
                    try:
                        Voto(persona=personas_dict[p_pk], habilidad=habilidades_dict[h_pk], votante=votante_verify, valor=val).save()
                    except IntegrityError:
                        errores = 'Ya votaste!!'
                        break

            if not errores:
                return redirect('stats')

        templates_vars = {
                'personas': personas,
                'habilidades': habilidades,
                'votante_form': votante_form,
                'votos_form': votos_form,
                'errores': errores,
                }

        return render_to_response('appraisal_form.html', templates_vars, context_instance=RequestContext(request))