Skip to content
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

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions lacommunaute/stats/admin.py
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()
Copy link
Contributor

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.

Suggested change
forums_with_stats = model_admin.model.objects.values_list("forum", flat=True).distinct()
forums_with_stats = model_admin.model.objects.values_list("forum", flat=True)

return [(forum.pk, forum.name) for forum in Forum.objects.filter(pk__in=forums_with_stats)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je préfère le style ci-dessous ?

Suggested change
return [(forum.pk, forum.name) for forum in Forum.objects.filter(pk__in=forums_with_stats)]
list(Forum.objects.filter(pk__in=forums_with_stats).values("pk", "name"))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tant qu’à faire : Forum.objects.filter(pk__in=forums_with_stats).values_list("pk", "name") ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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",)
Loading