Skip to content

Commit

Permalink
Add a few method to the PackageURLQuerySetMixin contrib class
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Jul 15, 2024
1 parent be73771 commit c64cdc7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
0.16.0 (unreleased)
-------------------

- Add ``with_package_url``, ``without_package_url``, and ``order_by_package_url`` to the
``PackageURLQuerySetMixin`` contrib class.

0.15.3 (2024-07-09)
-------------------

Expand Down
21 changes: 17 additions & 4 deletions src/packageurl/contrib/django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
from packageurl.contrib.django.utils import purl_to_lookups


PACKAGE_URL_FIELDS = ("type", "namespace", "name", "version", "qualifiers", "subpath")


class PackageURLQuerySetMixin:
"""
Add Package URL filtering methods to a django.db.models.QuerySet.
Expand All @@ -53,12 +56,22 @@ def for_package_url(self, purl_str, encode=True, exact_match=False):
return self.filter(**lookups)
return self.none()

def empty_package_url(self):
"""
Return objects with empty Package URL.
"""
def with_package_url(self):
"""Return objects with Package URL defined."""
return self.filter(~models.Q(type="") & ~models.Q(name=""))

def without_package_url(self):
"""Return objects with empty Package URL."""
return self.filter(models.Q(type="") | models.Q(name=""))

def empty_package_url(self):
"""Return objects with empty Package URL. Alias of without_package_url."""
return self.without_package_url()

def order_by_package_url(self):
"""Order by Package URL fields."""
return self.order_by(*PACKAGE_URL_FIELDS)


class PackageURLQuerySet(PackageURLQuerySetMixin, models.QuerySet):
pass
Expand Down

0 comments on commit c64cdc7

Please sign in to comment.