|
5 | 5 | from django.db.models.query import QuerySet |
6 | 6 |
|
7 | 7 | from application.access_control.services.current_user import get_current_user |
| 8 | +from application.commons.models import Settings |
8 | 9 | from application.core.models import ( |
9 | 10 | Observation, |
10 | 11 | Product, |
@@ -86,6 +87,12 @@ def _add_annotations(queryset: QuerySet, is_product_group: bool, with_annotation |
86 | 87 | if not with_annotations: |
87 | 88 | return queryset |
88 | 89 |
|
| 90 | + queryset = _add_observation_annotations(queryset, is_product_group) |
| 91 | + queryset = _add_license_annotations(queryset, is_product_group) |
| 92 | + return queryset |
| 93 | + |
| 94 | + |
| 95 | +def _add_observation_annotations(queryset: QuerySet, is_product_group: bool) -> QuerySet: |
89 | 96 | subquery_open_critical = ( |
90 | 97 | _get_product_group_observation_subquery(Severity.SEVERITY_CRITICAL) |
91 | 98 | if is_product_group |
@@ -117,49 +124,58 @@ def _add_annotations(queryset: QuerySet, is_product_group: bool, with_annotation |
117 | 124 | else _get_product_observation_subquery(Severity.SEVERITY_UNKNOWN) |
118 | 125 | ) |
119 | 126 |
|
120 | | - subquery_license_forbidden = ( |
121 | | - _get_product_group_license_subquery(License_Policy_Evaluation_Result.RESULT_FORBIDDEN) |
122 | | - if is_product_group |
123 | | - else _get_product_license_subquery(License_Policy_Evaluation_Result.RESULT_FORBIDDEN) |
124 | | - ) |
125 | | - subquery_license_review_required = ( |
126 | | - _get_product_group_license_subquery(License_Policy_Evaluation_Result.RESULT_REVIEW_REQUIRED) |
127 | | - if is_product_group |
128 | | - else _get_product_license_subquery(License_Policy_Evaluation_Result.RESULT_REVIEW_REQUIRED) |
129 | | - ) |
130 | | - subquery_license_unknown = ( |
131 | | - _get_product_group_license_subquery(License_Policy_Evaluation_Result.RESULT_UNKNOWN) |
132 | | - if is_product_group |
133 | | - else _get_product_license_subquery(License_Policy_Evaluation_Result.RESULT_UNKNOWN) |
134 | | - ) |
135 | | - subquery_license_allowed = ( |
136 | | - _get_product_group_license_subquery(License_Policy_Evaluation_Result.RESULT_ALLOWED) |
137 | | - if is_product_group |
138 | | - else _get_product_license_subquery(License_Policy_Evaluation_Result.RESULT_ALLOWED) |
139 | | - ) |
140 | | - subquery_license_ignored = ( |
141 | | - _get_product_group_license_subquery(License_Policy_Evaluation_Result.RESULT_IGNORED) |
142 | | - if is_product_group |
143 | | - else _get_product_license_subquery(License_Policy_Evaluation_Result.RESULT_IGNORED) |
144 | | - ) |
145 | | - |
146 | 127 | queryset = queryset.annotate( |
147 | 128 | open_critical_observation_count=Coalesce(subquery_open_critical, 0), |
148 | 129 | open_high_observation_count=Coalesce(subquery_open_high, 0), |
149 | 130 | open_medium_observation_count=Coalesce(subquery_open_medium, 0), |
150 | 131 | open_low_observation_count=Coalesce(subquery_open_low, 0), |
151 | 132 | open_none_observation_count=Coalesce(subquery_open_none, 0), |
152 | 133 | open_unknown_observation_count=Coalesce(subquery_open_unknown, 0), |
153 | | - forbidden_licenses_count=Coalesce(subquery_license_forbidden, 0), |
154 | | - review_required_licenses_count=Coalesce(subquery_license_review_required, 0), |
155 | | - unknown_licenses_count=Coalesce(subquery_license_unknown, 0), |
156 | | - allowed_licenses_count=Coalesce(subquery_license_allowed, 0), |
157 | | - ignored_licenses_count=Coalesce(subquery_license_ignored, 0), |
158 | 134 | ) |
159 | 135 |
|
160 | 136 | return queryset |
161 | 137 |
|
162 | 138 |
|
| 139 | +def _add_license_annotations(queryset: QuerySet, is_product_group: bool) -> QuerySet: |
| 140 | + settings = Settings.load() |
| 141 | + if settings.feature_license_management: |
| 142 | + subquery_license_forbidden = ( |
| 143 | + _get_product_group_license_subquery(License_Policy_Evaluation_Result.RESULT_FORBIDDEN) |
| 144 | + if is_product_group |
| 145 | + else _get_product_license_subquery(License_Policy_Evaluation_Result.RESULT_FORBIDDEN) |
| 146 | + ) |
| 147 | + subquery_license_review_required = ( |
| 148 | + _get_product_group_license_subquery(License_Policy_Evaluation_Result.RESULT_REVIEW_REQUIRED) |
| 149 | + if is_product_group |
| 150 | + else _get_product_license_subquery(License_Policy_Evaluation_Result.RESULT_REVIEW_REQUIRED) |
| 151 | + ) |
| 152 | + subquery_license_unknown = ( |
| 153 | + _get_product_group_license_subquery(License_Policy_Evaluation_Result.RESULT_UNKNOWN) |
| 154 | + if is_product_group |
| 155 | + else _get_product_license_subquery(License_Policy_Evaluation_Result.RESULT_UNKNOWN) |
| 156 | + ) |
| 157 | + subquery_license_allowed = ( |
| 158 | + _get_product_group_license_subquery(License_Policy_Evaluation_Result.RESULT_ALLOWED) |
| 159 | + if is_product_group |
| 160 | + else _get_product_license_subquery(License_Policy_Evaluation_Result.RESULT_ALLOWED) |
| 161 | + ) |
| 162 | + subquery_license_ignored = ( |
| 163 | + _get_product_group_license_subquery(License_Policy_Evaluation_Result.RESULT_IGNORED) |
| 164 | + if is_product_group |
| 165 | + else _get_product_license_subquery(License_Policy_Evaluation_Result.RESULT_IGNORED) |
| 166 | + ) |
| 167 | + |
| 168 | + queryset = queryset.annotate( |
| 169 | + forbidden_licenses_count=Coalesce(subquery_license_forbidden, 0), |
| 170 | + review_required_licenses_count=Coalesce(subquery_license_review_required, 0), |
| 171 | + unknown_licenses_count=Coalesce(subquery_license_unknown, 0), |
| 172 | + allowed_licenses_count=Coalesce(subquery_license_allowed, 0), |
| 173 | + ignored_licenses_count=Coalesce(subquery_license_ignored, 0), |
| 174 | + ) |
| 175 | + |
| 176 | + return queryset |
| 177 | + |
| 178 | + |
163 | 179 | def _get_product_observation_subquery(severity: str) -> Subquery: |
164 | 180 | branch_filter = Q(branch__is_default_branch=True) | ( |
165 | 181 | Q(branch__isnull=True) & Q(product__repository_default_branch__isnull=True) |
|
0 commit comments