Skip to content

Commit

Permalink
Admin: add Tender HasAmountFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Aug 31, 2023
1 parent 0d14ef1 commit a8515a8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ class SourceFilter(MultiChoice):
BUTTON_LABEL = "Appliquer"


class HasAmountFilter(admin.SimpleListFilter):
title = "Montant renseigné ?"
parameter_name = "has_amount"

def lookups(self, request, model_admin):
return (("Yes", "Oui"), ("No", "Non"))

def queryset(self, request, queryset):
value = self.value()
if value == "Yes":
return queryset.has_amount()
elif value == "No":
return queryset.filter(amount__isnull=True, amount_exact__isnull=True)
return queryset


class AmountFilter(MultiChoice):
FILTER_LABEL = Tender._meta.get_field("amount").verbose_name
BUTTON_LABEL = "Appliquer"
Expand Down Expand Up @@ -128,6 +144,7 @@ class TenderAdmin(FieldsetsInlineMixin, admin.ModelAdmin):
"status",
("scale_marche_useless", ScaleMarcheUselessFilter),
("source", SourceFilter),
HasAmountFilter,
("amount", AmountFilter),
"deadline_date",
"start_working_date",
Expand Down
3 changes: 3 additions & 0 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def is_incremental(self):
def is_live(self):
return self.validated().filter(deadline_date__gte=datetime.today())

def has_amount(self):
return self.filter(Q(amount__isnull=False) | Q(amount_exact__isnull=False))

def in_perimeters(self, post_code, department, region):
filters = (
Q(perimeters__post_codes__contains=[post_code])
Expand Down
9 changes: 9 additions & 0 deletions lemarche/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ def test_is_live_queryset(self):
# TenderFactory(deadline_date=None) # cannot be None
self.assertEqual(Tender.objects.is_live().count(), 1)

def test_has_amount_queryset(self):
TenderFactory()
TenderFactory(amount=tender_constants.AMOUNT_RANGE_0_1)
TenderFactory(amount_exact=1000)
TenderFactory(amount=tender_constants.AMOUNT_RANGE_0_1, amount_exact=1000)
self.assertEqual(Tender.objects.count(), 4)
self.assertEqual(Tender.objects.has_amount().count(), 3)
self.assertEqual(Tender.objects.filter(amount__isnull=True, amount_exact__isnull=True).count(), 1)

def test_in_sectors_queryset(self):
sector_1 = SectorFactory(name="Un secteur")
sector_2 = SectorFactory(name="Un deuxieme secteur")
Expand Down

0 comments on commit a8515a8

Please sign in to comment.