Skip to content

Commit

Permalink
Minor Changes to Get Workflow tests corrected
Browse files Browse the repository at this point in the history
Signed-off-by: Rishi Garg <[email protected]>
  • Loading branch information
Rishi-garg03 committed Nov 2, 2024
1 parent c465bbd commit 8d177c8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 63 deletions.
31 changes: 30 additions & 1 deletion vulnerabilities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,53 @@ class BaseSearchForm(forms.Form):
def clean_search(self):
return self.cleaned_data.get("search", "")

def get_queryset(self, query=None):
"""
Get queryset with search/filter/ordering applied.
Args:
query (str, optional): Direct query for testing
"""
if query is not None:
return self._search(query)

if not self.is_valid():
return self.model.objects.none()

return self._search(self.clean_search())


class PackageSearchForm(BaseSearchForm):
model = Package
search = forms.CharField(
required=True,
widget=forms.TextInput(
attrs={"placeholder": "Package name, purl or purl fragment"},
),
)

def _search(self, query):
"""Execute package-specific search logic."""
return (
self.model.objects.search(query)
.with_vulnerability_counts()
.prefetch_related()
.order_by("package_url")
)


class VulnerabilitySearchForm(BaseSearchForm):
model = Vulnerability
search = forms.CharField(
required=True,
widget=forms.TextInput(
attrs={"placeholder": "Vulnerability id or alias such as CVE or GHSA"}
),
)

def _search(self, query):
"""Execute vulnerability-specific search logic."""
return self.model.objects.search(query=query).with_package_counts()


class ApiUserCreationForm(forms.ModelForm):
"""
Expand Down Expand Up @@ -110,4 +139,4 @@ def clean_username(self):
return username

def save_m2m(self):
pass
pass
91 changes: 29 additions & 62 deletions vulnerabilities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
from django.contrib import messages
from django.core.exceptions import ValidationError
from django.core.mail import send_mail
from django.core.paginator import EmptyPage
from django.core.paginator import PageNotAnInteger
from django.core.paginator import Paginator
from django.http.response import Http404
from django.shortcuts import redirect
from django.shortcuts import render
Expand All @@ -34,7 +31,6 @@
from vulnerabilities.forms import PackageSearchForm
from vulnerabilities.forms import PaginationForm
from vulnerabilities.forms import VulnerabilitySearchForm
from vulnerabilities.models import VulnerabilityStatusType
from vulnerabilities.severity_systems import EPSS
from vulnerabilities.severity_systems import SCORING_SYSTEMS
from vulnerabilities.utils import get_severity_range
Expand Down Expand Up @@ -67,17 +63,17 @@ def get_purl_version_class(purl: models.Package):
return purl_version_class



class BaseSearchView(ListView):
"""Base view for implementing search functionality with pagination."""

paginate_by = PAGE_SIZE
max_page_size = MAX_PAGE_SIZE

def get_paginate_by(self):
def get_paginate_by(self, queryset=None):
"""
This function would get and validate the page size from request parameters.
It returns a page size between 1 and max_page_size, defaulting to
self.paginate_by for invalid inputs.
Get and validate the requested page size.
Required 2 positional_argument get_paginate_by(positional_argument1, positional_argument2)
"""
try:
page_size = int(self.request.GET.get("page_size", self.paginate_by))
Expand All @@ -88,6 +84,7 @@ def get_paginate_by(self):
return self.paginate_by

def get_context_data(self, **kwargs):
"""Add pagination form to the template context."""
context = super().get_context_data(**kwargs)
context.update(
{
Expand All @@ -104,47 +101,26 @@ class PackageSearch(BaseSearchView):
ordering = ["type", "namespace", "name", "version"]

def get_queryset(self, query=None):
"""
Return a Package queryset based on search parameters.
Args:
query (str, optional): Direct search query, mainly used for testing.
If not provided, uses form data from request.
Returns:
QuerySet: Filtered and ordered package queryset
"""
"""Get queryset from form's search method."""
if query is not None:
# Handle direct query (used in tests)
return (
self.model.objects.search(query)
.with_vulnerability_counts()
.prefetch_related()
.order_by("package_url")
)
form = self.form_class()
return form.get_queryset(query=query)

# Handle form submission
self.form = self.form_class(self.request.GET)
if not self.form.is_valid():
return self.model.objects.none()

search_query = self.form.cleaned_data.get("search", "")
return (
self.model.objects.search(search_query)
.with_vulnerability_counts()
.prefetch_related()
.order_by("package_url")
)
if hasattr(self, "request"):
self.form = self.form_class(self.request.GET)
return self.form.get_queryset()

return self.model.objects.none()

def get_context_data(self, **kwargs):
"""
Get the context data for template rendering.
Adds form and search parameters to context.
"""
"""Extends the template context with search form and search query for Packages."""
context = super().get_context_data(**kwargs)
if not hasattr(self, "form"):
self.form = self.form_class()
context.update(
{
"package_search_form": self.form,
"search": self.request.GET.get("search"),
"search": getattr(self.request, "GET", {}).get("search"),
}
)
return context
Expand All @@ -157,35 +133,26 @@ class VulnerabilitySearch(BaseSearchView):
ordering = ["vulnerability_id"]

def get_queryset(self, query=None):
"""
Return a Vulnerability queryset based on search parameters.
Args:
query (str, optional): Direct search query, mainly used for testing.
If not provided, uses form data from request.
Returns:
QuerySet: Filtered vulnerability queryset
"""
"""Get queryset from form's search method."""
if query is not None:
return self.model.objects.search(query=query).with_package_counts()
form = self.form_class()
return form.get_queryset(query=query)

self.form = self.form_class(self.request.GET)
if not self.form.is_valid():
return self.model.objects.none()
if hasattr(self, "request"):
self.form = self.form_class(self.request.GET)
return self.form.get_queryset()

search_query = self.form.cleaned_data.get("search", "")
return self.model.objects.search(query=search_query).with_package_counts()
return self.model.objects.none()

def get_context_data(self, **kwargs):
"""
Get the context data for template rendering.
Adds form and search parameters to context.
"""
"""Extends the template context with search form and search query for Vulnerability."""
context = super().get_context_data(**kwargs)
if not hasattr(self, "form"):
self.form = self.form_class()
context.update(
{
"vulnerability_search_form": self.form,
"search": self.request.GET.get("search"),
"search": getattr(self.request, "GET", {}).get("search"),
}
)
return context
Expand Down Expand Up @@ -383,4 +350,4 @@ def form_valid(self, form):
return response

def get_success_url(self):
return reverse_lazy("api_user_request")
return reverse_lazy("api_user_request")

0 comments on commit 8d177c8

Please sign in to comment.