|
29 | 29 |
|
30 | 30 | class PackageURLFilter(django_filters.CharFilter):
|
31 | 31 | """
|
32 |
| - Filter by a Package URL string. |
33 |
| - Empty values are not applied to the QuerySet. |
| 32 | + Filter by an exact Package URL string. |
| 33 | + The special "EMPTY" value allows to retrieve objects with empty |
| 34 | + Package URL. |
34 | 35 |
|
35 |
| - This filter depends on a `for_package_url` method available on the Model |
36 |
| - Manager, see for example `PackageURLQuerySetMixin`. |
| 36 | + This filter depends on a `for_package_url` and `empty_package_url` |
| 37 | + methods to be available on the Model Manager, |
| 38 | + see for example `PackageURLQuerySetMixin`. |
37 | 39 | """
|
38 | 40 |
|
| 41 | + is_empty = "EMPTY" |
| 42 | + help_text = ( |
| 43 | + "Match Package URL. " |
| 44 | + 'Use "EMPTY" as value to retrieve objects with empty Package URL.' |
| 45 | + ) |
| 46 | + |
| 47 | + def __init__(self, *args, **kwargs): |
| 48 | + kwargs.setdefault("help_text", self.help_text) |
| 49 | + super().__init__(*args, **kwargs) |
| 50 | + |
39 | 51 | def filter(self, qs, value):
|
40 |
| - empty_values = ([], (), {}, "", None) |
41 |
| - if value in empty_values: |
| 52 | + none_values = ([], (), {}, "", None) |
| 53 | + if value in none_values: |
42 | 54 | return qs
|
43 | 55 |
|
44 | 56 | if self.distinct:
|
45 | 57 | qs = qs.distinct()
|
| 58 | + |
| 59 | + if value == self.is_empty: |
| 60 | + return qs.empty_package_url() |
| 61 | + |
46 | 62 | return qs.for_package_url(value)
|
0 commit comments