Skip to content

Commit

Permalink
Run 'make valid' #522
Browse files Browse the repository at this point in the history
Reference: #522
Signed-off-by: John M. Horan <[email protected]>
  • Loading branch information
johnmhoran committed Oct 1, 2024
1 parent e680f5e commit 7cbcc75
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 46 deletions.
34 changes: 24 additions & 10 deletions packagedb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
from collections import OrderedDict
from urllib.parse import urlencode

import natsort
from dateutil.parser import parse as dateutil_parse
from django.conf import settings
from django.contrib.auth.models import UserManager
from django.contrib.postgres.fields import ArrayField
Expand All @@ -28,6 +26,9 @@
from django.dispatch import receiver
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

import natsort
from dateutil.parser import parse as dateutil_parse
from licensedcode.cache import build_spdx_license_expression
from packagedcode.models import normalize_qualifiers
from packageurl import PackageURL
Expand Down Expand Up @@ -86,7 +87,6 @@ def paginated(self, per_page=5000):
page = paginator.page(page_number)
yield from page.object_list


# Based on class PurlValidateResponseSerializer(Serializer). Added here because when added to serializers.py and imported raises a circular import error.
class PurlValidateErrorSerializer(Serializer):
valid = BooleanField()
Expand Down Expand Up @@ -123,8 +123,12 @@ def search(self, query: str = None):
return serializer.data

qs = qs.filter(
models.Q(namespace=package_url.namespace) if package_url.namespace else (models.Q(namespace="")),
models.Q(subpath=package_url.subpath) if package_url.subpath else (models.Q(subpath="")),
models.Q(namespace=package_url.namespace)
if package_url.namespace
else (models.Q(namespace="")),
models.Q(subpath=package_url.subpath)
if package_url.subpath
else (models.Q(subpath="")),
type=package_url.type,
name=package_url.name,
version=package_url.version,
Expand Down Expand Up @@ -156,13 +160,23 @@ def get_packageurl_from_string(self, query: str = None):
try:
packageurl_from_string = PackageURL.from_string(query)
if print_to_console:
print(f"\n- models.py PackageQuerySet get_packageurl_from_string() query = {packageurl_from_string}")
print(
f"\n- models.py PackageQuerySet get_packageurl_from_string() query = {packageurl_from_string}"
)
print(f"\npackageurl_from_string.type = {packageurl_from_string.type}")
print(f"- packageurl_from_string.namespace = {packageurl_from_string.namespace}")
print(
f"- packageurl_from_string.namespace = {packageurl_from_string.namespace}"
)
print(f"- packageurl_from_string.name = {packageurl_from_string.name}")
print(f"- packageurl_from_string.version = {packageurl_from_string.version}")
print(f"- packageurl_from_string.qualifiers = {packageurl_from_string.qualifiers}")
print(f"- packageurl_from_string.subpath = {packageurl_from_string.subpath}")
print(
f"- packageurl_from_string.version = {packageurl_from_string.version}"
)
print(
f"- packageurl_from_string.qualifiers = {packageurl_from_string.qualifiers}"
)
print(
f"- packageurl_from_string.subpath = {packageurl_from_string.subpath}"
)

return packageurl_from_string

Expand Down
83 changes: 47 additions & 36 deletions packagedb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,67 +48,69 @@ def get_context_data(self, **kwargs):
context["search"] = request_query.get("search")

if print_to_console:
print(f"\nviews.py ValidatePurl() get_context_data context['search'] = {context['search']}")
print(
f"\nviews.py ValidatePurl() get_context_data context['search'] = {context['search']}"
)

context["packageurl_from_string"] = self.check_packageurl_from_string(request_query.get("search"))
context["packageurl_from_string"] = self.check_packageurl_from_string(
request_query.get("search")
)

purl_components_tooltips = [
{
"text": "scheme",
"attribute": "scheme",
"tooltip_class": "tooltip-content",
"required": True,
"data_tooltip": "The scheme is a constant with the value 'pkg', and is followed by a ':' separator."
"data_tooltip": "The scheme is a constant with the value 'pkg', and is followed by a ':' separator.",
},
{
"text": "type",
"attribute": "type",
"tooltip_class": "tooltip-content",
"required": True,
"data_tooltip": "A short code to identify the type of this package. For example: gem for a Rubygem, docker for a container, pypi for a Python Wheel or Egg, maven for a Maven Jar, deb for a Debian package etc."
"data_tooltip": "A short code to identify the type of this package. For example: gem for a Rubygem, docker for a container, pypi for a Python Wheel or Egg, maven for a Maven Jar, deb for a Debian package etc.",
},
{
"text": "namespace",
"attribute": "namespace",
"tooltip_class": "tooltip-content",
"required": False,
"data_tooltip": "Package name prefix, such as Maven groupid, Docker image owner, GitHub user or organization, etc."
"data_tooltip": "Package name prefix, such as Maven groupid, Docker image owner, GitHub user or organization, etc.",
},
{
"text": "name",
"attribute": "name",
"tooltip_class": "tooltip-content",
"required": True,
"data_tooltip": "Name of the package."
"data_tooltip": "Name of the package.",
},
{
"text": "version",
"attribute": "version",
"tooltip_class": "tooltip-content",
"required": False,
"data_tooltip": "Version of the package."
"data_tooltip": "Version of the package.",
},
{
"text": "qualifiers",
"attribute": "qualifiers",
"tooltip_class": "tooltip-content",
"required": False,
"data_tooltip": "Extra qualifying data for a package, such as the name of an OS, architecture, distro, etc."
"data_tooltip": "Extra qualifying data for a package, such as the name of an OS, architecture, distro, etc.",
},
{
"text": "subpath",
"attribute": "subpath",
"tooltip_class": "tooltip-content",
"required": False,
"data_tooltip": "Extra subpath within a package, relative to the package root."
"data_tooltip": "Extra subpath within a package, relative to the package root.",
},
]
context["purl_components_tooltips"] = purl_components_tooltips

if self.validation_errors:
context["validation_errors"] = (
self.validation_errors
)
context["validation_errors"] = self.validation_errors
context["parse_purl_attributes"] = parse_purl(request_query.get("search"))
if print_to_console:
print(f"\ncontext = \n{context}\n")
Expand All @@ -128,11 +130,12 @@ def get_queryset(self, query=None):

return result.prefetch_related().order_by("version")


def check_packageurl_from_string(self, query=None):
query = query or self.request.GET.get("search") or ""
if print_to_console:
print(f"\nviews.py ValidatePurl check_packageurl_from_string() query = {query}")
print(
f"\nviews.py ValidatePurl check_packageurl_from_string() query = {query}"
)
result = self.model.objects.get_packageurl_from_string(query)

return result
Expand All @@ -152,9 +155,7 @@ def get_context_data(self, **kwargs):
context["search"] = request_query.get("search")

if self.validation_errors:
context["validation_errors"] = (
self.validation_errors
)
context["validation_errors"] = self.validation_errors

context["parse_purl_attributes"] = parse_purl(request_query.get("search"))

Expand All @@ -163,69 +164,67 @@ def get_context_data(self, **kwargs):
"text": "Package URL",
"attribute": "package_url",
"tooltip_class": "tooltip-content",
"data_tooltip": "A Package URL (&quot;purl&quot;) is a URL string used to identify and locate a software package in a mostly universal and uniform way across programming languages, package managers, packaging conventions, tools, APIs and databases."
"data_tooltip": "A Package URL (&quot;purl&quot;) is a URL string used to identify and locate a software package in a mostly universal and uniform way across programming languages, package managers, packaging conventions, tools, APIs and databases.",
},
{
"text": "Filename",
"attribute": "filename",
"tooltip_class": "tooltip-content",
"data_tooltip": "The exact file name (typically an archive of some type) of the package. This is usually the name of the file as downloaded from a website."
"data_tooltip": "The exact file name (typically an archive of some type) of the package. This is usually the name of the file as downloaded from a website.",
},
{
"text": "Download URL",
"attribute": "download_url",
"tooltip_class": "tooltip-content",
"data_tooltip": "The download URL for obtaining the package."
"data_tooltip": "The download URL for obtaining the package.",
},
{
"text": "Homepage URL",
"attribute": "homepage_url",
"tooltip_class": "tooltip-content",
"data_tooltip": "Homepage URL"
"data_tooltip": "Homepage URL",
},
{
"text": "Primary language",
"attribute": "primary_language",
"tooltip_class": "tooltip-content",
"data_tooltip": "The primary programming language associated with the package."
"data_tooltip": "The primary programming language associated with the package.",
},
{
"text": "Description",
"attribute": "description",
"tooltip_class": "tooltip-content",
"data_tooltip": "Freeform description, preferably as provided by the author(s)."
"data_tooltip": "Freeform description, preferably as provided by the author(s).",
},

{
"text": "Type",
"attribute": "type",
"tooltip_class": "tooltip-content",
"data_tooltip": "A short code to identify the type of this package. For example: gem for a Rubygem, docker for a container, pypi for a Python Wheel or Egg, maven for a Maven Jar, deb for a Debian package, etc."
"data_tooltip": "A short code to identify the type of this package. For example: gem for a Rubygem, docker for a container, pypi for a Python Wheel or Egg, maven for a Maven Jar, deb for a Debian package, etc.",
},

{
"text": "Name",
"attribute": "name",
"tooltip_class": "tooltip-content",
"data_tooltip": "Name of the package."
"data_tooltip": "Name of the package.",
},
{
"text": "Version",
"attribute": "version",
"tooltip_class": "tooltip-content",
"data_tooltip": "Version of the package."
"data_tooltip": "Version of the package.",
},
{
"text": "Release date",
"attribute": "release_date",
"tooltip_class": "tooltip-content",
"data_tooltip": "The date that the package file was created, or when it was posted to its original download source."
"data_tooltip": "The date that the package file was created, or when it was posted to its original download source.",
},
{
"text": "Declared license expression",
"attribute": "declared_license_expression",
"tooltip_class": "tooltip-content",
"data_tooltip": "A license expression derived from statements in the manifest or key files of a software project, such as the NOTICE, COPYING, README and LICENSE files."
"data_tooltip": "A license expression derived from statements in the manifest or key files of a software project, such as the NOTICE, COPYING, README and LICENSE files.",
},
]
context["purl_details_tooltips"] = purl_details_tooltips
Expand All @@ -243,7 +242,9 @@ def get_queryset(self, query=None):
result = self.model.objects.search(query)
if isinstance(result, dict): # If result is a validation error dictionary
self.validation_errors = result # Store errors in the instance attribute
print(f"\nValidatedPurlDetails.get_queryset() result = {json.dumps(result, indent=4)}")
print(
f"\nValidatedPurlDetails.get_queryset() result = {json.dumps(result, indent=4)}"
)
return self.model.objects.none() # Return an empty queryset

return result.prefetch_related().order_by("version")
Expand Down Expand Up @@ -298,7 +299,9 @@ def parse_purl(query):
purl_type = type
else:
if not type or not sep:
purl_error_message = "The input purl is missing the required 'scheme' and 'type' components."
purl_error_message = (
"The input purl is missing the required 'scheme' and 'type' components."
)
else:
purl_type = type

Expand Down Expand Up @@ -367,7 +370,9 @@ def parse_purl(query):
purl_name = name
elif purl_pkg_scheme_component == "MISSING" and purl_type != "MISSING":
if not name:
purl_error_message = "The input purl is missing the required 'scheme' and 'name' components."
purl_error_message = (
"The input purl is missing the required 'scheme' and 'name' components."
)
else:
purl_name = name
elif purl_pkg_scheme_component == "MISSING" and purl_type == "MISSING":
Expand All @@ -377,7 +382,9 @@ def parse_purl(query):
purl_name = name
elif purl_pkg_scheme_component == "pkg:" and purl_type == "MISSING":
if not name:
purl_error_message = "The input purl is missing the required 'type' and 'name' components."
purl_error_message = (
"The input purl is missing the required 'type' and 'name' components."
)
else:
purl_name = name

Expand Down Expand Up @@ -405,10 +412,14 @@ def parse_purl(query):
print(f"\nparse_purl_attributes['status'] = {parse_purl_attributes['status']}")
print(f"\nparse_purl_attributes['scheme'] = {parse_purl_attributes['scheme']}")
print(f"parse_purl_attributes['type'] = {parse_purl_attributes['type']}")
print(f"parse_purl_attributes['namespace'] = {parse_purl_attributes['namespace']}")
print(
f"parse_purl_attributes['namespace'] = {parse_purl_attributes['namespace']}"
)
print(f"parse_purl_attributes['name'] = {parse_purl_attributes['name']}")
print(f"parse_purl_attributes['version'] = {parse_purl_attributes['version']}")
print(f"parse_purl_attributes['qualifiers'] = {parse_purl_attributes['qualifiers']}")
print(
f"parse_purl_attributes['qualifiers'] = {parse_purl_attributes['qualifiers']}"
)
print(f"parse_purl_attributes['subpath'] = {parse_purl_attributes['subpath']}")
print("")

Expand Down
1 change: 1 addition & 0 deletions purldb_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.conf.urls import include
from django.urls import path
from django.views.generic.base import TemplateView

from drf_spectacular.views import SpectacularAPIView
from drf_spectacular.views import SpectacularSwaggerView
from rest_framework import routers
Expand Down

0 comments on commit 7cbcc75

Please sign in to comment.