Add categories for offers

This commit is contained in:
Jannis Portmann 2023-07-07 13:47:56 +02:00
parent 9d2bfc4371
commit 8370050f2c
6 changed files with 52 additions and 3 deletions

View file

@ -10,7 +10,7 @@ from .models import Offer, PflaenzliUser, Wish
class CreateOfferForm(forms.ModelForm):
class Meta:
model = Offer
fields = ['title', 'description', 'zipcode', 'image']
fields = ['title', 'description', 'category', 'zipcode', 'image']
class RegistrationForm(UserCreationForm):
@ -22,6 +22,7 @@ class RegistrationForm(UserCreationForm):
class FilterForm(forms.Form):
category = forms.ChoiceField(choices=Offer.CATEGORIES)
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 +31,10 @@ class FilterForm(forms.Form):
super().__init__(*args, **kwargs)
# 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}'
)
self.fields['text'].label = mark_safe(
f'<i class="fa-solid fa-magnifying-glass"></i> {self.fields["text"].label}'
)

View file

@ -0,0 +1,28 @@
# Generated by Django 4.1.7 on 2023-05-19 10:10
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("pflaenzli", "0003_alter_offer_description_alter_offer_image_and_more"),
]
operations = [
migrations.AddField(
model_name="offer",
name="category",
field=models.CharField(
choices=[
("PLNT", "Plant"),
("SEED", "Seedling"),
("POT", "Pot"),
("TOOL", "Tools"),
("OTHR", "Other"),
],
default="PLNT",
max_length=4,
),
),
]

View file

@ -14,12 +14,21 @@ class PflaenzliUser(AbstractUser):
class Offer(models.Model):
CATEGORIES = [
('PLNT', _('Plant')),
('SEED', _('Seedling')),
('POT', _('Pots')),
('TOOL', _('Tools')),
('OTHR', _('Other')),
]
created = models.DateTimeField(default=timezone.now)
user = models.ForeignKey(PflaenzliUser, on_delete=models.CASCADE)
title = models.CharField(max_length=50, verbose_name=_('Title'))
description = models.TextField(max_length=5000, verbose_name=_('Description'))
zipcode = models.IntegerField(blank=True, default=0, verbose_name=_('ZIP code'))
image = models.ImageField(upload_to="uploads/", verbose_name=_('Image'))
category = models.CharField(choices=CATEGORIES, max_length=4, default='PLNT', verbose_name=_('Category'))
class Wish(models.Model):

View file

@ -17,7 +17,9 @@
src="{{ offer.image.url }}">
</div>
<div class="show-offer-info col-12 col-md-6">
<h1 class="mb-3">{{ offer.title }}</h1>
<h1 class="mb-3">
{{ offer.title }}<span class="ms-2 badge text-bg-secondary">{{ offer.get_category_display }}</span>
</h1>
<div class="mb-3 d-flex column-gap-2">
<p class="mr-3">
<i class="fas fa-user"></i>

View file

@ -40,7 +40,9 @@
<img class="card-img-top offer-img" src="{% static 'placeholder.jpg' %}" />
{% endif %}
<div class="card-body">
<h5 class="h5">{{ offer.title }}</h5>
<h5 class="h5">
{{ offer.title }}<span class="ms-2 badge pfl-{{ offer.category|lower }}">{{ offer.get_category_display }}</span>
</h5>
</div>
</a>
<div class="card-footer d-flex justify-content-between">

View file

@ -199,6 +199,9 @@ def error_500(request):
def filter_offers(offers, form):
if form.cleaned_data['category']:
offers = offers.filter(category=form.cleaned_data['category'])
if form.cleaned_data['text']:
offers = offers.filter(title__icontains=form.cleaned_data['text'])