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