Skip to content

Commit

Permalink
clean from review
Browse files Browse the repository at this point in the history
  • Loading branch information
madjid-asa committed Feb 22, 2024
1 parent 444e312 commit f3201e4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
10 changes: 5 additions & 5 deletions lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ def queryset(self, request, queryset):

class AmountCustomFilter(admin.SimpleListFilter):
title = "Montant du besoin"
# FILTER_LABEL = Tender._meta.get_field("amount").verbose_name
BUTTON_LABEL = "Appliquer"
parameter_name = "amount"

def lookups(self, request, model_admin):
Expand All @@ -71,11 +69,13 @@ def queryset(self, request, queryset):
value = self.value()
amount_10k = 10 * 10**3 # 10k
if value == "<10k":
return queryset.filter_by_amount(amount_10k, operation="lt")
return queryset.filter_by_amount_exact(amount_10k, operation="lt")
elif value == "5k-10k":
return queryset.filter_by_amount(amount_10k, operation="lte").filter_by_amount(amount_10k, operation="gte")
return queryset.filter_by_amount_exact(amount_10k, operation="lte").filter_by_amount_exact(
amount_10k, operation="gte"
)
elif value == ">=10k":
return queryset.filter_by_amount(amount_10k, operation="gte")
return queryset.filter_by_amount_exact(amount_10k, operation="gte")


class ResponseKindFilter(admin.SimpleListFilter):
Expand Down
8 changes: 4 additions & 4 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def has_amount(self):
)
)

def filter_by_amount(self, amount: int, operation: str = "gte"):
def filter_by_amount_exact(self, amount: int, operation: str = "gte"):
"""
Filters records based on a monetary amount with a specified comparison operation.
It dynamically selects between filtering on an exact amount (`amount_exact`)
Expand All @@ -131,7 +131,7 @@ def filter_by_amount(self, amount: int, operation: str = "gte"):
QuerySet: Filtered queryset based on the amount and operation.
Example:
>>> filtered_queryset = MyModel.objects.all().filter_by_amount(5000, 'gte')
>>> filtered_queryset = MyModel.objects.all().filter_by_amount_exact(5000, 'gte')
Filters for records with `amount_exact` >= 5000 or in the matching amount range.
"""
amounts_keys = find_amount_ranges(amount=amount, operation=operation)
Expand Down Expand Up @@ -1017,7 +1017,7 @@ class PartnerShareTenderQuerySet(models.QuerySet):
def is_active(self):
return self.filter(is_active=True)

def filter_by_amount(self, tender: Tender):
def filter_by_amount_exact(self, tender: Tender):
"""
Return partners with:
- an empty 'amount_in'
Expand Down Expand Up @@ -1056,7 +1056,7 @@ def filter_by_perimeter(self, tender: Tender):
return self.filter(conditions)

def filter_by_tender(self, tender: Tender):
return self.is_active().filter_by_amount(tender).filter_by_perimeter(tender).distinct()
return self.is_active().filter_by_amount_exact(tender).filter_by_perimeter(tender).distinct()


class PartnerShareTender(models.Model):
Expand Down
3 changes: 2 additions & 1 deletion lemarche/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,4 +1032,5 @@ def test_edge_case(self):

def test_no_matching_ranges(self):
"""Test when no ranges match the criteria."""
self.assertListEqual(find_amount_ranges(100, "lte"), [tender_constants.AMOUNT_RANGE_0_1])
expected_keys = [tender_constants.AMOUNT_RANGE_0_1]
self.assertListEqual(find_amount_ranges(100, "lte"), expected_keys)

0 comments on commit f3201e4

Please sign in to comment.