Пример #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
def test_1():
    v_entries = [[{
        'election_role': 4,
        'needed_signatures': 1
    }, {
        'election_role': 2,
        'needed_signatures': 2
    }, {
        'election_role': 3,
        'needed_signatures': 2
    }],
                 [{
                     'election_role': 1,
                     'needed_signatures': 1
                 }, {
                     'election_role': 2,
                     'needed_signatures': 1
                 }, {
                     'election_role': 3,
                     'needed_signatures': 1
                 }],
                 [{
                     'election_role': 1,
                     'needed_signatures': 2
                 }, {
                     'election_role': 2,
                     'needed_signatures': 2
                 }, {
                     'election_role': 3,
                     'needed_signatures': 2
                 }],
                 [{
                     'election_role': 1,
                     'needed_signatures': 1
                 }, {
                     'election_role': 2,
                     'needed_signatures': 0
                 }, {
                     'election_role': 3,
                     'needed_signatures': 2
                 }],
                 [{
                     'election_role': 2,
                     'needed_signatures': 1
                 }, {
                     'election_role': 3,
                     'needed_signatures': 2
                 }]]

    test_results = [False, False, False, True, True]

    for i, e in enumerate(v_entries):
        print "Test Case ", i + 1, " = ", ValidationPolicy.validate(
            e), " == ", test_results[i]
Пример #3
0
    def clean(self):
        super(BaseEntryFormSet, self).clean()
        if any(self.errors):
            return

        msg = []
        all_instances = []
        election = self.forms[0].election
        for form in self.forms:
            instance = form.cleaned_data

            if instance:
                if not instance['DELETE']:
                    all_instances.append(instance)
        #print all_instances

        #validate its satisfiability, returns [Bool, errors],
        #errors list the [needed_signatures, num_users] for each validation entry

        if not all_instances:
            msg.append(
                "You must define at least one validation entry for this validation policy"
            )

        results = ValidationPolicy.validate(
            all_instances,
            permission_id=self.policy_permission_id,
            election=election)
        if not results[0]:
            for i, er in enumerate(results[1]):
                if er[0] == 'perm':
                    msg.append(
                        "With this validation policy definition, no election officer would be able to execute this permission."
                    )
                elif er[0] != er[1]:
                    msg.append(u"" + all_instances[i]['description'] +
                               " needed signatures should be less than " +
                               str(er[0]))

        #validate the order
        ran = list(range(1, len(all_instances) + 1))

        for e in all_instances:
            if not e['order'] in ran:
                msg.append(
                    "There is an error in the ordering of the validation entries"
                )
                break
            else:
                ran.remove(e['order'])

        raise forms.ValidationError(msg)
Пример #4
0
    def clean(self):
        super(BaseEntryFormSet, self).clean()
        if any(self.errors):
            return
        
        msg = []
        all_instances = []
        election = self.forms[0].election
        for form in self.forms:
            instance = form.cleaned_data
            
            if instance:
                if not instance['DELETE']:
                    all_instances.append(instance)
        #print all_instances
        
        #validate its satisfiability, returns [Bool, errors], 
        #errors list the [needed_signatures, num_users] for each validation entry
        
        if not all_instances:
            msg.append("You must define at least one validation entry for this validation policy")
            
        results = ValidationPolicy.validate(all_instances, permission_id = self.policy_permission_id, election=election)
        if not results[0]:
            for i, er in enumerate(results[1]):
                if er[0] == 'perm':
                    msg.append("With this validation policy definition, no election officer would be able to execute this permission.")
                elif er[0] != er[1]:
                    msg.append(u""+all_instances[i]['description'] + " needed signatures should be less than " + str(er[0]))

        #validate the order
        ran = list(range(1, len(all_instances)+1))
        
        for e in all_instances:
            if not e['order'] in ran:
                msg.append("There is an error in the ordering of the validation entries")
                break
            else:
                ran.remove(e['order'])
                
        raise forms.ValidationError(msg)
Пример #5
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