@@ -2508,6 +2508,16 @@ class Compliance(models.TextChoices):
2508
2508
ERROR = "error"
2509
2509
MISSING = "missing"
2510
2510
2511
+ # Map each compliance status to a severity level.
2512
+ # Higher numbers indicate more severe compliance issues.
2513
+ # This allows consistent comparison and sorting of compliance states.
2514
+ COMPLIANCE_SEVERITY_MAP = {
2515
+ Compliance .OK : 0 ,
2516
+ Compliance .MISSING : 1 ,
2517
+ Compliance .WARNING : 2 ,
2518
+ Compliance .ERROR : 3 ,
2519
+ }
2520
+
2511
2521
compliance_alert = models .CharField (
2512
2522
max_length = 10 ,
2513
2523
blank = True ,
@@ -2541,7 +2551,7 @@ def save(self, codebase=None, *args, **kwargs):
2541
2551
Injects policies, if the feature is enabled, when the
2542
2552
``license_expression_field`` field value has changed.
2543
2553
2544
- `codebase` is not used in this context but required for compatibility
2554
+ `` codebase` ` is not used in this context but required for compatibility
2545
2555
with the commoncode.resource.Codebase class API.
2546
2556
"""
2547
2557
if self .policies_enabled :
@@ -2563,7 +2573,10 @@ def policies_enabled(self):
2563
2573
return self .project .policies_enabled
2564
2574
2565
2575
def compute_compliance_alert (self ):
2566
- """Compute and return the compliance_alert value from the licenses policies."""
2576
+ """
2577
+ Compute and return the compliance_alert value from the license policies.
2578
+ Chooses the most severe compliance_alert found among licenses.
2579
+ """
2567
2580
license_expression = getattr (self , self .license_expression_field , "" )
2568
2581
if not license_expression :
2569
2582
return ""
@@ -2583,17 +2596,12 @@ def compute_compliance_alert(self):
2583
2596
else :
2584
2597
alerts .append (self .Compliance .MISSING )
2585
2598
2586
- compliance_ordered_by_severity = [
2587
- self .Compliance .ERROR ,
2588
- self .Compliance .WARNING ,
2589
- self .Compliance .MISSING ,
2590
- ]
2591
-
2592
- for compliance_severity in compliance_ordered_by_severity :
2593
- if compliance_severity in alerts :
2594
- return compliance_severity
2599
+ if not alerts :
2600
+ return self .Compliance .OK
2595
2601
2596
- return self .Compliance .OK
2602
+ # Return the most severe alert based on the defined severity
2603
+ severity = self .COMPLIANCE_SEVERITY_MAP .get
2604
+ return max (alerts , key = severity )
2597
2605
2598
2606
2599
2607
class FileClassifierFieldsModelMixin (models .Model ):
0 commit comments