From f852b10c2c677e80d35bbcc75ada603094c8a10b Mon Sep 17 00:00:00 2001 From: Noel Rignon Date: Tue, 15 Sep 2020 11:27:59 -0400 Subject: [PATCH] enhance admin panel --- retirement/admin.py | 77 ++++++++++++++++++++++++++++++++++++++++++-- retirement/models.py | 12 +++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/retirement/admin.py b/retirement/admin.py index 55889e7b..e8dc39b0 100644 --- a/retirement/admin.py +++ b/retirement/admin.py @@ -1,5 +1,6 @@ from admin_auto_filters.filters import AutocompleteFilter from django.contrib import admin +from django.contrib.auth import get_user_model from django.utils.translation import ugettext_lazy as _ from import_export.admin import ExportActionModelAdmin from modeltranslation.admin import TranslationAdmin @@ -30,6 +31,8 @@ WaitQueueResource ) +User = get_user_model() + class RetreatFilter(AutocompleteFilter): title = 'Retreat' @@ -52,6 +55,34 @@ class PictureAdminInline(admin.TabularInline): readonly_fields = ('picture_tag', ) +class ReservationUserFilter(AutocompleteFilter): + title = 'User' + field_name = 'user' + rel_model = Reservation + + @property + def parameter_name(self): + return "reservation__user" + + @parameter_name.setter + def parameter_name(self, value): + pass + + +class ReservationRetreatFilter(AutocompleteFilter): + title = 'Retreat' + field_name = 'retreat' + rel_model = Reservation + + @property + def parameter_name(self): + return "reservation__retreat" + + @parameter_name.setter + def parameter_name(self, value): + pass + + def make_reservation_refundable(self, request, queryset): Reservation.objects.filter( @@ -268,13 +299,55 @@ class RetreatDateAdmin(admin.ModelAdmin): 'start_time', 'end_time', ) + list_filter = ( + RetreatFilter, + ) + + +class AutomaticEmailLogAdmin(admin.ModelAdmin): + list_display = ( + 'id', + 'user', + 'retreat', + 'template_id', + 'sent_at', + ) + list_filter = ( + ReservationUserFilter, + ReservationRetreatFilter, + 'sent_at' + ) + search_fields = ['reservation__user__email', 'email__template_id', + 'reservation__user__first_name', + 'reservation__user__last_name'] + + def lookup_allowed(self, lookup, value): + if lookup == "reservation__user": + return True + if lookup == "reservation__retreat": + return True + return super().lookup_allowed(lookup, value) + + +class AutomaticEmailAdmin(admin.ModelAdmin): + list_display = ( + 'id', + 'retreat_type', + 'time_base', + 'minutes_delta', + 'template_id', + ) + list_filter = ( + 'retreat_type', + 'template_id', + ) admin.site.register(Retreat, RetreatAdmin) admin.site.register(RetreatType) admin.site.register(RetreatDate, RetreatDateAdmin) -admin.site.register(AutomaticEmail) -admin.site.register(AutomaticEmailLog) +admin.site.register(AutomaticEmail, AutomaticEmailAdmin) +admin.site.register(AutomaticEmailLog, AutomaticEmailLogAdmin) admin.site.register(Picture, PictureAdmin) admin.site.register(Reservation, ReservationAdmin) admin.site.register(WaitQueue, WaitQueueAdmin) diff --git a/retirement/models.py b/retirement/models.py index ec9b1ae9..5d2fe0d2 100644 --- a/retirement/models.py +++ b/retirement/models.py @@ -858,6 +858,18 @@ class AutomaticEmailLog(models.Model): auto_now_add=True, ) + @property + def template_id(self): + return self.email.template_id + + @property + def retreat(self): + return self.reservation.retreat + + @property + def user(self): + return self.reservation.user + class Meta: verbose_name = _("Automatic email log") verbose_name_plural = _("Automatic email logs")