core: fix rule engine not working
This commit is contained in:
parent
b5ed371575
commit
0ce242e956
|
@ -165,7 +165,7 @@ class FieldMatcherRule(Rule):
|
|||
passes = self.value in user_field_value
|
||||
if self.match_action == FieldMatcherRule.MATCH_REGEXP:
|
||||
pattern = re.compile(self.value)
|
||||
passes = pattern.match(user_field_value)
|
||||
passes = bool(pattern.match(user_field_value))
|
||||
if self.negate:
|
||||
passes = not passes
|
||||
LOGGER.debug("User got '%r'", passes)
|
||||
|
|
|
@ -1,9 +1,20 @@
|
|||
"""passbook core rule engine"""
|
||||
from logging import getLogger
|
||||
|
||||
from celery import group
|
||||
|
||||
from passbook.core.celery import CELERY_APP
|
||||
from passbook.core.models import Rule, User
|
||||
|
||||
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)
|
||||
|
||||
class RuleEngine:
|
||||
"""Orchestrate rule checking, launch tasks and return result"""
|
||||
|
@ -14,28 +25,18 @@ class RuleEngine:
|
|||
def __init__(self, rule_model):
|
||||
self._rule_model = rule_model
|
||||
|
||||
@CELERY_APP.task(bind=True)
|
||||
def _rule_engine_task(self, 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(user_pk)
|
||||
return rule_obj.passes(user_obj)
|
||||
|
||||
def for_user(self, user):
|
||||
"""Check rules for user"""
|
||||
signatures = []
|
||||
for rule in self._rule_model.rules.all():
|
||||
# pylint: disable=no-member
|
||||
signatures.append(self._rule_engine_task.s(user.pk, rule.pk))
|
||||
self._group = group(signatures).apply_async()
|
||||
signatures.append(_rule_engine_task.s(user.pk, rule.pk.hex))
|
||||
self._group = group(signatures)()
|
||||
return self
|
||||
|
||||
def wait(self):
|
||||
"""Wait for result, blocking this request"""
|
||||
# return self._group.wait()
|
||||
|
||||
@property
|
||||
def result(self):
|
||||
"""Get rule-checking result"""
|
||||
print(self._group.get())
|
||||
for rule_result in self._group.get():
|
||||
if rule_result is False:
|
||||
return False
|
||||
return True
|
||||
|
|
Reference in New Issue