2018-11-28 13:01:59 +00:00
|
|
|
"""passbook core rule engine"""
|
2018-11-30 13:33:33 +00:00
|
|
|
from logging import getLogger
|
|
|
|
|
2018-11-28 13:01:59 +00:00
|
|
|
from celery import group
|
|
|
|
|
|
|
|
from passbook.core.celery import CELERY_APP
|
|
|
|
from passbook.core.models import Rule, User
|
|
|
|
|
2018-11-30 13:33:33 +00:00
|
|
|
LOGGER = getLogger(__name__)
|
|
|
|
|
|
|
|
@CELERY_APP.task()
|
|
|
|
def _rule_engine_task(user_pk, rule_pk):
|
|
|
|
"""Task wrapper to run rule checking"""
|
|
|
|
rule_obj = Rule.objects.filter(pk=rule_pk).select_subclasses().first()
|
|
|
|
user_obj = User.objects.get(pk=user_pk)
|
|
|
|
LOGGER.debug("Running rule `%s`#%s for user %s...", rule_obj.name, rule_obj.pk.hex, user_obj)
|
|
|
|
return rule_obj.passes(user_obj)
|
2018-11-28 13:01:59 +00:00
|
|
|
|
|
|
|
class RuleEngine:
|
|
|
|
"""Orchestrate rule checking, launch tasks and return result"""
|
|
|
|
|
|
|
|
_rule_model = None
|
|
|
|
_group = None
|
|
|
|
|
|
|
|
def __init__(self, rule_model):
|
|
|
|
self._rule_model = rule_model
|
|
|
|
|
|
|
|
def for_user(self, user):
|
|
|
|
"""Check rules for user"""
|
|
|
|
signatures = []
|
|
|
|
for rule in self._rule_model.rules.all():
|
2018-11-30 13:33:33 +00:00
|
|
|
signatures.append(_rule_engine_task.s(user.pk, rule.pk.hex))
|
|
|
|
self._group = group(signatures)()
|
2018-11-28 13:01:59 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
@property
|
|
|
|
def result(self):
|
|
|
|
"""Get rule-checking result"""
|
2018-11-30 13:33:33 +00:00
|
|
|
for rule_result in self._group.get():
|
|
|
|
if rule_result is False:
|
|
|
|
return False
|
2018-11-28 13:01:59 +00:00
|
|
|
return True
|