|
2 | 2 |
|
3 | 3 | from dojo.celery import app |
4 | 4 | from dojo.decorators import dojo_async_task |
5 | | -from dojo.models import Finding, Product, SLA_Configuration |
6 | | -from dojo.utils import calculate_grade, mass_model_updater |
| 5 | +from dojo.models import Finding, Product, SLA_Configuration, System_Settings |
| 6 | +from dojo.utils import get_custom_method, mass_model_updater |
7 | 7 |
|
8 | 8 | logger = logging.getLogger(__name__) |
9 | 9 |
|
10 | 10 |
|
11 | 11 | @dojo_async_task |
12 | 12 | @app.task |
13 | | -def update_sla_expiration_dates_sla_config_async(sla_config, products, severities, *args, **kwargs): |
14 | | - update_sla_expiration_dates_sla_config_sync(sla_config, products, severities) |
| 13 | +def async_update_sla_expiration_dates_sla_config_sync(sla_config: SLA_Configuration, products: list[Product], *args, severities: list[str] | None = None, **kwargs): |
| 14 | + if method := get_custom_method("FINDING_SLA_EXPIRATION_CALCULATION_METHOD"): |
| 15 | + method(sla_config, products, severities=severities) |
| 16 | + else: |
| 17 | + update_sla_expiration_dates_sla_config_sync(sla_config, products, severities=severities) |
15 | 18 |
|
16 | 19 |
|
17 | | -@dojo_async_task |
18 | | -@app.task |
19 | | -def update_sla_expiration_dates_product_async(product, sla_config, *args, **kwargs): |
20 | | - update_sla_expiration_dates_sla_config_sync(sla_config, [product]) |
21 | | - |
22 | | - |
23 | | -def update_sla_expiration_dates_sla_config_sync(sla_config, products, severities=None): |
| 20 | +def update_sla_expiration_dates_sla_config_sync(sla_config: SLA_Configuration, products: list[Product], severities: list[str] | None = None): |
24 | 21 | logger.info("Updating finding SLA expiration dates within the %s SLA configuration", sla_config) |
| 22 | + # First check if SLA is enabled globally |
| 23 | + system_settings = System_Settings.objects.get() |
| 24 | + if not system_settings.enable_finding_sla: |
| 25 | + return |
25 | 26 | # update each finding that is within the SLA configuration that was saved |
26 | 27 | findings = Finding.objects.filter(test__engagement__product__sla_configuration_id=sla_config.id) |
27 | 28 | if products: |
28 | 29 | findings = findings.filter(test__engagement__product__in=products) |
29 | 30 | if severities: |
30 | 31 | findings = findings.filter(severity__in=severities) |
31 | 32 |
|
32 | | - findings = findings.prefetch_related( |
| 33 | + findings = ( |
| 34 | + findings.prefetch_related( |
33 | 35 | "test", |
34 | 36 | "test__engagement", |
35 | 37 | "test__engagement__product", |
36 | 38 | "test__engagement__product__sla_configuration", |
| 39 | + ) |
| 40 | + .order_by("id") |
| 41 | + .only("id", "sla_start_date", "date", "severity", "test") |
37 | 42 | ) |
38 | | - |
39 | | - findings = findings.order_by("id").only("id", "sla_start_date", "date", "severity", "test") |
40 | | - |
| 43 | + # Call the internal method so that we are not checking system settings for each finding |
41 | 44 | mass_model_updater(Finding, findings, lambda f: f.set_sla_expiration_date(), fields=["sla_expiration_date"]) |
42 | 45 |
|
43 | 46 | # reset the async updating flag to false for all products using this sla config |
44 | | - for product in products: |
45 | | - product.async_updating = False |
46 | | - super(Product, product).save() |
47 | | - calculate_grade(product) |
| 47 | + # use update as we don't want save() and signals to be triggered |
| 48 | + products.update(async_updating=False) |
48 | 49 |
|
49 | 50 | # reset the async updating flag to false for this sla config |
50 | 51 | sla_config.async_updating = False |
|
0 commit comments