diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a2ffa68..56a417d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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) ------------------- diff --git a/src/packageurl/contrib/django/models.py b/src/packageurl/contrib/django/models.py index 2d9c236..7ef895e 100644 --- a/src/packageurl/contrib/django/models.py +++ b/src/packageurl/contrib/django/models.py @@ -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. @@ -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