diff --git a/members/admin/activity_admin.py b/members/admin/activity_admin.py index 723fe6f9..c92ef91c 100644 --- a/members/admin/activity_admin.py +++ b/members/admin/activity_admin.py @@ -10,6 +10,8 @@ Union, ) +from members.admin.admin_actions import AdminActions + class ActivityParticipantInline(admin.TabularInline): class Media: @@ -115,6 +117,9 @@ class ActivityAdmin(admin.ModelAdmin): "open_invite", "activitytype", ) + actions = [ + AdminActions.export_participants_csv, + ] save_as = True class Media: diff --git a/members/admin/activityparticipant_admin.py b/members/admin/activityparticipant_admin.py index 772b96b3..7fd0dea4 100644 --- a/members/admin/activityparticipant_admin.py +++ b/members/admin/activityparticipant_admin.py @@ -1,6 +1,4 @@ -import codecs from django.contrib import admin -from django.http import HttpResponse from django.urls import reverse from django.utils import timezone from django.utils.safestring import mark_safe @@ -221,7 +219,7 @@ class ActivityParticipantAdmin(admin.ModelAdmin): actions = [ AdminActions.invite_many_to_activity_action, - "export_csv_full", + AdminActions.export_participants_csv, ] def activity_activitytype(self, item): @@ -298,59 +296,3 @@ def activity_payment_info_html(self, item): return item.payment_info(True) activity_payment_info_html.short_description = "Betalingsinfo" - - def export_csv_full(self, request, queryset): - result_string = '"Forening"; "Afdeling"; "Aktivitet"; "Navn"; "Alder"; "Køn"; "Post-nr"; "Betalingsinfo"; "forældre navn"; "forældre email"; "forældre tlf"; "Note til arrangørerne"\n' - for p in queryset: - gender = p.person.gender_text() - - parent = p.person.family.get_first_parent() - if parent: - parent_name = parent.name - parent_phone = parent.phone - if not p.person.family.dont_send_mails: - parent_email = parent.email - else: - parent_email = "" - else: - parent_name = "" - parent_phone = "" - parent_email = "" - - result_string = ( - result_string - + p.activity.department.union.name - + ";" - + p.activity.department.name - + ";" - + p.activity.name - + ";" - + p.person.name - + ";" - + str(p.person.age_years()) - + ";" - + gender - + ";" - + p.person.zipcode - + ";" - + self.activity_payment_info_txt(p) - + ";" - + parent_name - + ";" - + parent_email - + ";" - + parent_phone - + ";" - + '"' - + p.note.replace('"', '""') - + '"' - + "\n" - ) - response = HttpResponse( - f'{codecs.BOM_UTF8.decode("utf-8")}{result_string}', - content_type="text/csv; charset=utf-8", - ) - response["Content-Disposition"] = 'attachment; filename="deltagere.csv"' - return response - - export_csv_full.short_description = "CSV Export" diff --git a/members/admin/admin_actions.py b/members/admin/admin_actions.py index 12813d08..961a3219 100644 --- a/members/admin/admin_actions.py +++ b/members/admin/admin_actions.py @@ -1,3 +1,4 @@ +import codecs from datetime import timedelta from dateutil.relativedelta import relativedelta from django import forms @@ -6,6 +7,8 @@ from django.contrib.admin.widgets import AdminDateWidget from django.db import transaction +from django.http import HttpResponse + from django.utils import timezone from django.utils.safestring import mark_safe from django.utils.html import escape @@ -341,3 +344,80 @@ class MassInvitationForm(forms.Form): invite_many_to_activity_action.short_description = ( "Inviter valgte personer til en aktivitet" ) + + def export_participants_csv(self, request, queryset): + print(f"queryset:[{queryset}]") + if queryset.model is Activity: + activities = [a.pk for a in queryset] + participants = ActivityParticipant.objects.filter(activity__in=activities) + elif queryset.model is ActivityParticipant: + participants = queryset + + context = admin.site.each_context(request) + context["participants"] = participants + context["queryset"] = queryset + + result_string = """"Forening"; "Afdeling"; "Aktivitet"; "Navn";\ + "Alder"; "Køn"; "Post-nr"; "Betalingsinfo"; "forældre navn";\ + "forældre email"; "forældre tlf"; "Foto-tilladelse";\ + "Note til arrangørerne"\n""" + for p in participants: + gender = p.person.gender_text() + + parent = p.person.family.get_first_parent() + if parent: + parent_name = parent.name + parent_phone = parent.phone + if not p.person.family.dont_send_mails: + parent_email = parent.email + else: + parent_email = "" + else: + parent_name = "" + parent_phone = "" + parent_email = "" + + if p.photo_permission == "OK": + photo = "Tilladelse givet" + else: + photo = "Ikke tilladt" + + result_string = ( + result_string + + p.activity.department.union.name + + ";" + + p.activity.department.name + + ";" + + p.activity.name + + ";" + + p.person.name + + ";" + + str(p.person.age_years()) + + ";" + + gender + + ";" + + p.person.zipcode + + ";" + + str(p.payment_info(False)) + + ";" + + parent_name + + ";" + + parent_email + + ";" + + parent_phone + + ";" + + photo + + ";" + + '"' + + p.note.replace('"', '""') + + '"' + + "\n" + ) + response = HttpResponse( + f'{codecs.BOM_UTF8.decode("utf-8")}{result_string}', + content_type="text/csv; charset=utf-8", + ) + response["Content-Disposition"] = 'attachment; filename="deltagere.csv"' + return response + + export_participants_csv.short_description = "Eksporter deltagerliste (CSV)"