Skip to content

Commit

Permalink
Merge pull request #1015 from CodingPirates/1009-feature-export-af-de…
Browse files Browse the repository at this point in the history
…ltagerliste

[#1009] Eksport af Deltagerliste. Indeholder nu også fototilladelse. Og kan bruges fra Admin > Aktiviteter
  • Loading branch information
lakridserne authored Apr 7, 2024
2 parents c3e50af + 01a096e commit 9d9a472
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 59 deletions.
5 changes: 5 additions & 0 deletions members/admin/activity_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
Union,
)

from members.admin.admin_actions import AdminActions


class ActivityParticipantInline(admin.TabularInline):
class Media:
Expand Down Expand Up @@ -115,6 +117,9 @@ class ActivityAdmin(admin.ModelAdmin):
"open_invite",
"activitytype",
)
actions = [
AdminActions.export_participants_csv,
]
save_as = True

class Media:
Expand Down
60 changes: 1 addition & 59 deletions members/admin/activityparticipant_admin.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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"
80 changes: 80 additions & 0 deletions members/admin/admin_actions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import codecs
from datetime import timedelta
from dateutil.relativedelta import relativedelta
from django import forms
Expand All @@ -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
Expand Down Expand Up @@ -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)"

0 comments on commit 9d9a472

Please sign in to comment.