@classmethod @db_session definstance(cls, name, weight=None): """ Return an PermissionGroup instance.
If a PermissionGroup with the name exists, it will directly return the group, with new weight value. It is noticeable that, if weight is not given, it won't be changed.
Or, it will create a new PermissionGroup with the given name and weight value. Notice that if the weight is not given here, an ValueError will be thrown.
""" result = cls.get(name=name) if result isNone: if weight isNone: raise ValueError return cls(name=name, weight=weight) if weight isnotNone: result.weight = weight return result
models/permission_node.py
1 2 3 4 5 6 7 8 9 10 11
from pony.orm import Required, db_session
from .db import db from .permission_group import PermissionGroup
classPermissionNode(db.Entity): permission = Required(str) value = Required(bool) owner = Required(PermissionGroup)
from models import User from pony.orm import select
defcheck_permission(user: User, permission): queue = Queue() weight = float("-inf") value = False for group in user.permission_groups: queue.put(group) whilenot queue.empty(): group = queue.get() for g in group.parents: queue.put(g) if group.weight < weight: continue values = select(node.value for node in group.nodes if node.permission == permission) for v in values: value = v weight = group.weight return value