@login_required
def update_editors_agendas(request):
    if request.method == 'POST':
        object_type = request.POST.get('form-0-object_type',None)
        object_id = request.POST.get('form-0-obj_id',None)
        vl_formset = object_formset_classes[object_type](request.POST)
        if vl_formset.is_valid():
            for a in vl_formset.cleaned_data:
                if a:
                    # Check that the user is an editor of the agenda
                    # he's trying to edit
                    try:
                        agenda = Agenda.objects.get(pk=a['agenda_id'])
                        if agenda.editors.all() not in request.user:
                            return HttpResponseForbidden()
                    except Agenda.DoesNotExist:
                        return HttpResponseForbidden()

                    if a['object_type'] == 'vote':
                        if a['DELETE']:
                            try:
                                object_id = a['obj_id']
                                av = AgendaVote.objects.get(
                                       agenda__id=a['agenda_id'],
                                       vote__id = a['obj_id'])
                                av.delete()
                            except AgendaVote.DoesNotExist:
                                pass
                        else: # not delete, so try to create
                            if (a['weight'] is not '' and
                                a['importance'] is not ''):
                                try:
                                    object_id = a['obj_id']
                                    av = AgendaVote.objects.get(
                                           agenda__id=a['agenda_id'],
                                           vote__id = a['obj_id'])
                                    av.score = a['weight']
                                    av.importance = a['importance']
                                    av.reasoning = a['reasoning']
                                    av.save()
                                except AgendaVote.DoesNotExist:
                                    av = AgendaVote(
                                           agenda_id=int(a['agenda_id']),
                                           vote_id=int(a['obj_id']),
                                           score = a['weight'],
                                           importance = a['importance'],
                                           reasoning = a['reasoning'])
                                    av.save()
                    if a['object_type'] == 'bill':
                        if a['DELETE']:
                            try:
                                object_id = a['obj_id']
                                av = AgendaBill.objects.get(
                                       agenda__id=a['agenda_id'],
                                       bill__id = a['obj_id'])
                                av.delete()
                            except AgendaBill.DoesNotExist:
                                pass
                        else: # not delete, so try to create
                            if (a['weight'] is not '' and
                                a['importance'] is not ''):
                                try:
                                    object_id = a['obj_id']
                                    av = AgendaBill.objects.get(
                                           agenda__id=a['agenda_id'],
                                           bill__id = a['obj_id'])
                                    av.score = a['weight']
                                    av.importance = a['importance']
                                    av.reasoning = a['reasoning']
                                    av.save()
                                except AgendaBill.DoesNotExist:
                                    av = AgendaBill(
                                           agenda_id=int(a['agenda_id']),
                                           bill_id=int(a['obj_id']),
                                           score = a['weight'],
                                           importance = a['importance'],
                                           reasoning = a['reasoning'])
                                    av.save()
                    if a['object_type'] == 'committeemeeting':
                        if a['DELETE']:
                            try:
                                object_id = a['obj_id']
                                av = AgendaMeeting.objects.get(
                                       agenda__id=a['agenda_id'],
                                       meeting__id = a['obj_id'])
                                av.delete()
                            except AgendaMeeting.DoesNotExist:
                                pass
                        else: # not delete, so try to create
                            try:
                                object_id = a['obj_id']
                                av = AgendaMeeting.objects.get(
                                       agenda__id=a['agenda_id'],
                                       meeting__id = a['obj_id'])
                                av.score = a['weight']
                                av.reasoning = a['reasoning']
                                av.save()
                            except AgendaMeeting.DoesNotExist:
                                av = AgendaMeeting(
                                       agenda_id=int(a['agenda_id']),
                                       meeting_id=int(a['obj_id']),
                                       score = a['weight'],
                                       reasoning = a['reasoning'])
                                av.save()
                else:
                    logger.info("invalid form")

        else:
            # TODO: Error handling: what to do with illeal forms?
            logger.info("invalid formset")
            logger.info("%s" % vl_formset.errors)
        if object_type in object_redirect:
            if object_id: # return to object page
                return HttpResponseRedirect(
                        reverse(object_redirect[object_type][0],
                                kwargs={'pk':object_id}))
            else: # return to list
                return HttpResponseRedirect(reverse(object_redirect[object_type][1]))
        else:
            logger.warn('unknown object_type')
            return HttpResponseRedirect(reverse('main'))
    else:
        return HttpResponseNotAllowed(['POST'])
dataset/ETHPy150Open ofri/Open-Knesset/agendas/views.py/update_editors_agendas/swapped operands of `not in`