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]
Пример #2
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)
Пример #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)