Пример #1
0
 def clean(self):
     super(OfficerEditForm, self).clean()
     if any(self.errors):
         print self.errors
     
     election = self.election
     cleaned_data = self.cleaned_data        
     super_p = cleaned_data.get('super_p')
     user_id = cleaned_data.get('user_id')
     
     print super_p
     
     admins = None
     if not super_p:
         admins = ElectionOfficer.objects.filter(election=election,super_p=True).exclude(user__user_id=user_id)
         print admins
         if not admins:  
             raise forms.ValidationError("You need to have at least one (1) %s  for this election. " % (settings.ELECTION_ADMIN_ROLE))
     
     officer = ElectionOfficer.objects.filter(user__user_id=user_id,election=election)
     
     if not officer:
         return cleaned_data
     
     election_roles = list(cleaned_data.get('electionrole'))
     
     if super_p:
         election_roles.append(ElectionRole.objects.get(election=election, name=settings.ELECTION_ADMIN_ROLE))
         
     deleted_roles = []
     for role in officer[0].electionrole.all():
         if role not in election_roles:
             deleted_roles.append(role)
             
     if deleted_roles:
         v_policies = ValidationPolicy.get_by_election_and_officer(election, officer[0], delete_from_roles=deleted_roles, delete=True)
         if v_policies:
             print v_policies
             msg = []
             msg1 = "You are not allowed to remove officer %s from the roles " % (officer[0].user.name)
             print msg1
             for i, role in enumerate(deleted_roles):
                 msg1 = msg1 + " " + role.name
                 if i < (len(deleted_roles) - 1):
                     msg1 = msg1 + ", "
             msg1 = msg1 + "  because he is needed on deciding the following validation policies:"
             msg.append(msg1)
             print msg
             for policy in v_policies:
                 msg.append(policy.description)
             print msg
             raise forms.ValidationError(msg)    
     
     return cleaned_data
Пример #2
0
    def clean(self):
        super(OfficerEditForm, self).clean()
        if any(self.errors):
            print self.errors

        election = self.election
        cleaned_data = self.cleaned_data
        super_p = cleaned_data.get('super_p')
        user_id = cleaned_data.get('user_id')

        print super_p

        admins = None
        if not super_p:
            admins = ElectionOfficer.objects.filter(
                election=election, super_p=True).exclude(user__user_id=user_id)
            print admins
            if not admins:
                raise forms.ValidationError(
                    "You need to have at least one (1) %s  for this election. "
                    % (settings.ELECTION_ADMIN_ROLE))

        officer = ElectionOfficer.objects.filter(user__user_id=user_id,
                                                 election=election)

        if not officer:
            return cleaned_data

        election_roles = list(cleaned_data.get('electionrole'))

        if super_p:
            election_roles.append(
                ElectionRole.objects.get(election=election,
                                         name=settings.ELECTION_ADMIN_ROLE))

        deleted_roles = []
        for role in officer[0].electionrole.all():
            if role not in election_roles:
                deleted_roles.append(role)

        if deleted_roles:
            v_policies = ValidationPolicy.get_by_election_and_officer(
                election,
                officer[0],
                delete_from_roles=deleted_roles,
                delete=True)
            if v_policies:
                print v_policies
                msg = []
                msg1 = "You are not allowed to remove officer %s from the roles " % (
                    officer[0].user.name)
                print msg1
                for i, role in enumerate(deleted_roles):
                    msg1 = msg1 + " " + role.name
                    if i < (len(deleted_roles) - 1):
                        msg1 = msg1 + ", "
                msg1 = msg1 + "  because he is needed on deciding the following validation policies:"
                msg.append(msg1)
                print msg
                for policy in v_policies:
                    msg.append(policy.description)
                print msg
                raise forms.ValidationError(msg)

        return cleaned_data