Coverage for rfpy/model/helpers.py: 100%
33 statements
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-31 16:00 +0000
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-31 16:00 +0000
1from functools import wraps
2from typing import Set
4from rfpy.auth import LacksPermission, AuthorizationFailure
7class ensure:
9 def __init__(self, perm, is_vendor=False, is_participant=False):
10 self.perm = perm
11 if not (is_vendor or is_participant):
12 raise ValueError('either is_vendor or is_participant must be True')
13 self.is_participant = is_participant
14 self.is_vendor = is_vendor
16 def __call__(self, func):
18 @wraps(func)
19 def wrapped(this, user):
20 # this == self == Issue instance
21 if not user.has_permission(self.perm):
22 raise LacksPermission(self.perm, user.id)
24 if self.is_participant and user.organisation not in this.project.participants:
25 m = ('%s does not belong to a participant ' +
26 'organisation so cannot perform this action')
27 raise AuthorizationFailure(message=m % user)
29 if self.is_vendor and user.organisation != this.respondent:
30 m = ('%s does not belong to respondent %s ' +
31 'so cannot perform this action')
32 raise AuthorizationFailure(message=m % (user, this.respondent))
34 return func(this, user)
36 return wrapped
39def validate_section_children(
40 session,
41 node_type: type,
42 provided_id_set: Set[int],
43 found_node_ids: Set[int],
44 current_child_ids: Set[int],
45 delete_orphans=False):
46 '''
47 Validate the set of provided ids. Ensure:
48 - that the set of provided_ids matches that of the found_ids
49 - that any missing ids (ie present in current_child_ids but missing in provided_id_set) are
50 only provided if delete_orphans is True
51 '''
52 if len(found_node_ids) != len(provided_id_set):
53 alien = provided_id_set - found_node_ids
54 m = f"Some provided {node_type.__name__} ids not found in this project: {alien}"
55 raise ValueError(m)
57 potential_orphans = current_child_ids - provided_id_set
58 if potential_orphans and not delete_orphans:
59 m = f"{node_type.__name__} ids {potential_orphans} exist in the current section but were "
60 m += "not provided in the provided ID list. Will not delete because delete_orphans is false"
61 raise ValueError(m)