Skip to content

Commit

Permalink
Add filter by vulnerable in hierarchy tab #94
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Aug 13, 2024
1 parent 2c5d621 commit 5aa6ff9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<button type="button" data-bs-toggle="tooltip" title="Edit" class="btn btn-link p-0" aria-label="Edit object"><i class="far fa-edit fa-sm"></i></button>
</span>
{% endif %}
{% if relation.vulnerability_count %}
{% include 'component_catalog/includes/vulnerability_icon_link.html' with url=instance.get_absolute_url count=relation.vulnerability_count %}
{% endif %}
{% elif not instance %}
<span data-bs-toggle="tooltip" title="Custom Component">
<i class="fa fa-puzzle-piece me-1 text-muted"></i>
Expand Down
4 changes: 2 additions & 2 deletions dje/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,8 @@ class HasCountFilter(HasRelationFilter):
"""

def filter(self, qs, value):
if value == "with":
if value in ["with", "yes"]:
return qs.filter(**{f"{self.field_name}_count__gt": 0}).distinct()
elif value == "without":
elif value in ["without", "no"]:
return qs.filter(**{f"{self.field_name}_count__exact": 0}).distinct()
return qs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
<a class="nav-link{% if values.is_deployed != 'yes' and values.is_deployed != 'no' %} active{% endif %}" href="?hierarchy-is_deployed=all#hierarchy">Inventory <small>(Deployed=All)</small></a>
</li>
<li class="nav-item">
<a class="nav-link{% if values.is_deployed == 'yes' %} active{% endif %}" href="?hierarchy-is_deployed=yes#hierarchy">BOM <small>(Deployed=Yes)</small></a>
<a class="nav-link{% if values.is_deployed == 'yes' %} active{% endif %}" href="?hierarchy-is_deployed=yes#hierarchy">BOM <small>(Deployed=Yes)</small></a>
</li>
<li class="nav-item">
<a class="nav-link{% if values.is_deployed == 'no' %} active{% endif %}" href="?hierarchy-is_deployed=no#hierarchy">Internal Use Only <small>(Deployed=No)</small></a>
<a class="nav-link{% if values.is_deployed == 'no' %} active{% endif %}" href="?hierarchy-is_deployed=no#hierarchy">Internal Use Only <small>(Deployed=No)</small></a>
</li>
<li class="nav-item">
<a class="nav-link{% if values.is_vulnerable == 'yes' %} active{% endif %}" href="?hierarchy-is_vulnerable=yes#hierarchy"><i class="fas fa-bug me-1"></i>Affected by vulnerabilities</a>
</li>
</ul>
</div>
Expand Down
8 changes: 8 additions & 0 deletions product_portfolio/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
from dje import tasks
from dje.client_data import add_client_data
from dje.filters import BooleanChoiceFilter
from dje.filters import HasCountFilter
from dje.models import DejacodeUser
from dje.models import History
from dje.templatetags.dje_tags import urlize_target_blank
Expand Down Expand Up @@ -441,6 +442,12 @@ def tab_hierarchy(self):
productcomponent_qs = is_deployed_filter.filter(productcomponent_qs, is_deployed)
productpackage_qs = is_deployed_filter.filter(productpackage_qs, is_deployed)

is_vulnerable = self.request.GET.get("hierarchy-is_vulnerable")
if is_vulnerable:
is_vulnerable_filter = HasCountFilter(field_name="vulnerability")
productcomponent_qs = is_vulnerable_filter.filter(productcomponent_qs, is_vulnerable)
productpackage_qs = is_vulnerable_filter.filter(productpackage_qs, is_vulnerable)

if not (productcomponent_qs or productpackage_qs or is_deployed):
return

Expand All @@ -454,6 +461,7 @@ def tab_hierarchy(self):
"verbose_name_plural": self.model._meta.verbose_name_plural,
"relations_feature_grouped": dict(sorted(relations_feature_grouped.items())),
"is_deployed": is_deployed,
"is_vulnerable": is_vulnerable,
}

return {"fields": [(None, context, None, template)]}
Expand Down

0 comments on commit 5aa6ff9

Please sign in to comment.