Skip to content

Commit 7302e19

Browse files
committed
add tasks for rules evaluation
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent d468a54 commit 7302e19

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

policy/engine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ def evaluate_rule(policy_rule, product):
5050
return None
5151

5252

53-
def evaluate_rules(dataspace, product):
53+
def evaluate_rules(product):
5454
"""
5555
Evaluate all active PolicyRules for the given product.
5656
Returns the list of active ProductPolicyViolation instances.
5757
"""
5858
violations = []
59-
for policy_rule in PolicyRule.objects.scope(dataspace).active():
59+
for policy_rule in PolicyRule.objects.scope(product.dataspace).active():
6060
violation = evaluate_rule(policy_rule, product)
6161
if violation:
6262
violations.append(violation)

policy/tasks.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# DejaCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: AGPL-3.0-only
5+
# See https://github.com/aboutcode-org/dejacode for support or download.
6+
# See https://aboutcode.org for more information about AboutCode FOSS projects.
7+
#
8+
9+
from django.apps import apps
10+
11+
from django_rq import job
12+
13+
from policy.engine import evaluate_rules
14+
15+
16+
@job
17+
def evaluate_product_rules_task(product_uuid):
18+
"""Evaluate all active PolicyRules for the given product UUID."""
19+
Product = apps.get_model("product_portfolio", "product")
20+
21+
try:
22+
product = Product.objects.select_related("dataspace").get(uuid=product_uuid)
23+
except Product.DoesNotExist:
24+
return
25+
26+
evaluate_rules(product)
27+
28+
29+
@job
30+
def evaluate_all_products_rules_task():
31+
"""Enqueue evaluate_product_rules_task for every product."""
32+
Product = apps.get_model("product_portfolio", "product")
33+
34+
for product in Product.objects.select_related("dataspace").all():
35+
evaluate_product_rules_task.delay(product_uuid=product.uuid)

0 commit comments

Comments
 (0)