Fix category filtering
This commit is contained in:
parent
27db76cdeb
commit
258f24f70b
|
@ -22,7 +22,7 @@ class RegistrationForm(UserCreationForm):
|
|||
|
||||
|
||||
class FilterForm(forms.Form):
|
||||
category = forms.ChoiceField(choices=Offer.CATEGORIES)
|
||||
category = forms.ChoiceField(choices=Offer.FILTER_CATEGORIES, label=_("Category"))
|
||||
text = forms.CharField(max_length=128, required=False, label=_("Search"))
|
||||
zipcode = forms.CharField(max_length=4, required=False, label=_("ZIP code"))
|
||||
distance = forms.IntegerField(required=False, label=_("Distance"))
|
||||
|
@ -30,6 +30,9 @@ class FilterForm(forms.Form):
|
|||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
# Overwrite default
|
||||
self.fields['category'].initial = 'ALL'
|
||||
|
||||
# Set the translated labels with the selected language and icons for each form field
|
||||
self.fields['category'].label = mark_safe(
|
||||
f'<i class="fa-solid fa-tag"></i> {self.fields["category"].label}'
|
||||
|
|
|
@ -22,6 +22,10 @@ class Offer(models.Model):
|
|||
('OTHR', _('Other')),
|
||||
]
|
||||
|
||||
FILTER_CATEGORIES = [
|
||||
('ALL', _('All')),
|
||||
] + CATEGORIES
|
||||
|
||||
created = models.DateTimeField(default=timezone.now)
|
||||
user = models.ForeignKey(PflaenzliUser, on_delete=models.CASCADE)
|
||||
title = models.CharField(max_length=50, verbose_name=_('Title'))
|
||||
|
|
|
@ -199,16 +199,17 @@ def error_500(request):
|
|||
|
||||
|
||||
def filter_offers(offers, form):
|
||||
if form.cleaned_data['category']:
|
||||
offers = offers.filter(category=form.cleaned_data['category'])
|
||||
if category := form.cleaned_data['category']:
|
||||
if category != 'ALL':
|
||||
offers = offers.filter(category=category)
|
||||
|
||||
if form.cleaned_data['text']:
|
||||
offers = offers.filter(title__icontains=form.cleaned_data['text'])
|
||||
if text := form.cleaned_data['text']:
|
||||
offers = offers.filter(title__icontains=text)
|
||||
|
||||
if form.cleaned_data['zipcode']:
|
||||
if zipcode := form.cleaned_data['zipcode']:
|
||||
if form.cleaned_data['distance']:
|
||||
offers = filter_by_distance(offers, form.cleaned_data['zipcode'], form.cleaned_data['distance'])
|
||||
offers = filter_by_distance(offers, zipcode, form.cleaned_data['distance'])
|
||||
else:
|
||||
offers = offers.filter(zipcode=int(form.cleaned_data['zipcode']))
|
||||
offers = offers.filter(zipcode=int(zipcode))
|
||||
|
||||
return offers
|
||||
|
|
Loading…
Reference in a new issue