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

debug excel #35

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
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
30 changes: 28 additions & 2 deletions core/admin.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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):
Expand All @@ -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": (
Expand All @@ -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'
Expand All @@ -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": (
Expand All @@ -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
Expand Down
28 changes: 10 additions & 18 deletions user/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.http import JsonResponse
from excel_response import ExcelResponse
from django.contrib import admin
from .models import SiteUser, Team
Expand All @@ -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 = []
Expand All @@ -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
Expand Down
Loading