From f8cdc0d01d940a1a13fe8ed3f61461332d4a77fd Mon Sep 17 00:00:00 2001 From: MJ Akbari Date: Sun, 22 Sep 2024 13:22:38 +0330 Subject: [PATCH] debug excel --- core/admin.py | 30 ++++++++++++++++++++++++++++-- user/admin.py | 28 ++++++++++------------------ 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/core/admin.py b/core/admin.py index 8bb7d7f..8399fdc 100644 --- a/core/admin.py +++ b/core/admin.py @@ -1,6 +1,7 @@ import jdatetime from django.contrib import admin from solo.admin import SingletonModelAdmin +from excel_response import ExcelResponse from core.models import ( Assistant, @@ -84,6 +85,23 @@ def send_reminder(services): reminder_email_task.delay(context) +def export_participants(queryset): + data = [] + headers = ['Email', 'Phone Number', 'Name', 'Presentation'] + + for event in queryset: + for service in event.services.all(): + if service and service.user and service.payment_state == "CM": + data.append([service.user.email, service.user.phone_number, service.user.first_name, event.title]) + + if not data: + return JsonResponse({"message": "Nothing Found"}) + else: + data.sort(key=lambda x: x[3]) + data.insert(0, headers) + return ExcelResponse(data=data, worksheet_name="Events", output_filename="events") + + @admin.register(Talk) class TalkAdmin(admin.ModelAdmin): def send_reminder_emails(self, request, queryset): @@ -97,6 +115,9 @@ def send_reminder_emails(self, request, queryset): return JsonResponse({"message": "Emails sent."}) + def export_selected_participants(self, request, queryset): + return export_participants(queryset) + fieldsets = ( ('Dates', { "fields": ( @@ -116,7 +137,8 @@ def send_reminder_emails(self, request, queryset): PresenterTalkInline ] - actions = ['send_reminder_emails'] + actions = ['send_reminder_emails', 'export_selected_participants'] + export_selected_participants.short_description = 'Export selected participants' send_reminder_emails.short_description = 'Send reminder emails' exclude = ['presenters'] date_hierarchy = 'start' @@ -139,6 +161,9 @@ def send_reminder_emails(self, request, queryset): return JsonResponse({"message": "Emails sent."}) + def export_selected_participants(self, request, queryset): + return export_participants(queryset) + fieldsets = ( ('Dates', { "fields": ( @@ -158,7 +183,8 @@ def send_reminder_emails(self, request, queryset): PresenterWorkshopInline, WorkshopAssistantInline, ] - actions = ['send_reminder_emails'] + actions = ['send_reminder_emails', 'export_selected_participants'] + export_selected_participants.short_description = 'Export selected participants' send_reminder_emails.short_description = 'Send reminder emails' date_hierarchy = 'start' actions_on_top = True diff --git a/user/admin.py b/user/admin.py index f579180..a9fe9b1 100644 --- a/user/admin.py +++ b/user/admin.py @@ -1,3 +1,4 @@ +from django.http import JsonResponse from excel_response import ExcelResponse from django.contrib import admin from .models import SiteUser, Team @@ -14,21 +15,10 @@ def export_selected_users(self, request, queryset): for user in queryset: data.append([user.first_name, user.email, user.phone_number, user.start_date]) - return ExcelResponse(data=data, worksheet_name="Users", output_filename="users") - - def export_selected_services(self, request, queryset): - data = [] - headers = ['Email', 'Phone Number', 'Name', 'Talk / workshop'] - - for user in queryset: - for service in user.services.all(): - if service.payment_state == "CM": - event = service.talk or service.workshop - data.append([service.user.email, service.user.phone_number, service.user.first_name, event.title]) - - data.sort(key=lambda x: x[3]) - data.insert(0, headers) - return ExcelResponse(data=data, worksheet_name="Services", output_filename="services") + if not data: + return JsonResponse({"message": "Nothing Found"}) + else: + return ExcelResponse(data=data, worksheet_name="Users", output_filename="users") def export_selected_online_participants(self, request, queryset): data = [] @@ -46,11 +36,13 @@ def export_selected_online_participants(self, request, queryset): if classes: data.append([user.phone_number, 'gamecraft2024', user.first_name, classes[1:], "normal"]) - return ExcelResponse(data=data, worksheet_name="Participants", output_filename="participants") + if not data: + return JsonResponse({"message": "Nothing Found"}) + else: + return ExcelResponse(data=data, worksheet_name="Participants", output_filename="participants") - actions = ['export_selected_users', 'export_selected_services', 'export_selected_online_participants'] + actions = ['export_selected_users', 'export_selected_online_participants'] export_selected_users.short_description = 'Export selected site users' - export_selected_services.short_description = "Export selected site users' services" export_selected_online_participants.short_description = "Export selected site users' online classes" # search by fields