-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(admin): améliorer le filtrage des stats de forum #699
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,16 +1,44 @@ | ||||||
from dateutil.relativedelta import relativedelta | ||||||
from django.contrib import admin | ||||||
|
||||||
from lacommunaute.forum.models import Forum | ||||||
from lacommunaute.stats.models import ForumStat, Stat | ||||||
|
||||||
|
||||||
class ForumWithStatsFilter(admin.SimpleListFilter): | ||||||
title = "Forum" | ||||||
parameter_name = "forum" | ||||||
|
||||||
def lookups(self, request, model_admin): | ||||||
forums_with_stats = model_admin.model.objects.values_list("forum", flat=True).distinct() | ||||||
return [(forum.pk, forum.name) for forum in Forum.objects.filter(pk__in=forums_with_stats)] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Je préfère le style ci-dessous ?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tant qu’à faire : There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. merci 👍🏿 |
||||||
|
||||||
def queryset(self, request, queryset): | ||||||
if self.value(): | ||||||
return queryset.filter(forum=self.value()) | ||||||
return queryset | ||||||
|
||||||
|
||||||
class BaseStatAdmin(admin.ModelAdmin): | ||||||
list_display = ("explicit_period",) | ||||||
list_filter = ("date", "period") | ||||||
|
||||||
def explicit_period(self, obj): | ||||||
if obj.period == "month": | ||||||
return f"{obj.date} au {obj.date + relativedelta(months=1) - relativedelta(days=1)}" | ||||||
if obj.period == "week": | ||||||
return f"{obj.date} au {obj.date + relativedelta(days=6)}" | ||||||
return f"{obj.date} (jour unique)" | ||||||
|
||||||
|
||||||
@admin.register(Stat) | ||||||
class StatAdmin(admin.ModelAdmin): | ||||||
list_display = ("name", "date", "value", "period") | ||||||
list_filter = ("name", "date", "period") | ||||||
class StatAdmin(BaseStatAdmin): | ||||||
list_display = BaseStatAdmin.list_display + ("name", "value") | ||||||
list_filter = BaseStatAdmin.list_filter + ("name",) | ||||||
|
||||||
|
||||||
@admin.register(ForumStat) | ||||||
class ForumStatAdmin(admin.ModelAdmin): | ||||||
list_display = ("date", "period", "forum", "visits", "entry_visits", "time_spent") | ||||||
list_filter = ("date", "period", "forum") | ||||||
class ForumStatAdmin(BaseStatAdmin): | ||||||
list_display = BaseStatAdmin.list_display + ("forum", "visits", "entry_visits", "time_spent") | ||||||
list_filter = BaseStatAdmin.list_filter + (ForumWithStatsFilter,) | ||||||
raw_id_fields = ("forum",) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pas grave s’il y a des doublons dans le
__in
.