Skip to content

Commit

Permalink
fix: Organize revenue by year in admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
hampfh committed Aug 5, 2024
1 parent 6113ccd commit 20d7584
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions accounting/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,39 @@
from django.utils.translation import gettext_lazy as _


class RevenueDescendingFilter(admin.SimpleListFilter):
title = _("Year")
parameter_name = "fair__year"

def lookups(self, request, model_admin):
# Retrieve all unique years from the related model and sort them in descending order
years = (
model_admin.model.objects.order_by("-fair__year")
.values_list("fair__year", flat=True)
.distinct()
)
return [(year, year) for year in years]

# Default to current year selected
def value(self):
# Default to the current year if no specific value is selected
if not self.used_parameters.get(self.parameter_name):
return str(datetime.now().year)
return super().value()

def queryset(self, request, queryset):
if self.value():
return queryset.filter(fair__year=self.value())
return queryset


@admin.register(Revenue)
class RevenueAdmin(ModelAdminImproved):
ordering = ["name"]
ordering = ["-fair__year"]
list_display = ["name", "description", "fair"]
list_filter = ["fair__year"]
list_filter = [RevenueDescendingFilter]


class CategoryDescendingFilter(admin.SimpleListFilter):
title = _("Year")
parameter_name = "fair__year"
Expand Down

0 comments on commit 20d7584

Please sign in to comment.