Skip to content

Commit 34ad1db

Browse files
authored
Add import export to Volunteer Profile and Attendee Profile (#299)
* Add import export to Volunteer Profile and Attendee Profile * Linting
1 parent f460d9e commit 34ad1db

File tree

5 files changed

+115
-6
lines changed

5 files changed

+115
-6
lines changed

attendee/admin.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from django.contrib import admin
2+
from import_export import resources
3+
from import_export.admin import ImportExportModelAdmin
24

35
from .models import AttendeeProfile, PretixOrder
46

@@ -21,8 +23,23 @@ class PretixOrderAdmin(admin.ModelAdmin):
2123
search_fields = ("order_code", "email", "name")
2224

2325

26+
class AttendeeProfileResource(resources.ModelResource):
27+
class Meta:
28+
model = AttendeeProfile
29+
fields = (
30+
"order",
31+
"order__name",
32+
"order__email",
33+
"order__status",
34+
"may_share_email_with_sponsor",
35+
"chapter_description",
36+
"chapter_email",
37+
"chapter_website",
38+
)
39+
40+
2441
@admin.register(AttendeeProfile)
25-
class AttendeeProfileAdmin(admin.ModelAdmin):
42+
class AttendeeProfileAdmin(ImportExportModelAdmin):
2643
list_display = (
2744
"order",
2845
"city",
@@ -48,3 +65,4 @@ class AttendeeProfileAdmin(admin.ModelAdmin):
4865
"participated_in_previous_event",
4966
)
5067
readonly_fields = ("raw_answers",)
68+
resource_classes = [AttendeeProfileResource]

portal/common.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,6 @@ def get_first_time_attendee_count():
690690

691691
def get_first_time_attendee_percent():
692692
"""Returns the percent of first-time attendees."""
693-
cache.clear()
694693
first_time_attendee_percent = cache.get(CACHE_KEY_ATTENDEE_FIRST_TIME_PERCENT)
695694
if not first_time_attendee_percent:
696695
attendee_total_count = get_attendee_count_cache()

tests/portal/test_common.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ def test_attendee_breakdown_with_no_profiles(self):
360360
"""Test attendee breakdown returns empty when no profiles exist."""
361361
from portal.common import get_attendee_breakdown
362362

363-
cache.clear()
364363
breakdown = get_attendee_breakdown()
365364
for b in breakdown:
366365
assert b["data"] == []

tests/volunteer/test_admin.py

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
from django.core import mail
55
from django.core.files.uploadedfile import SimpleUploadedFile
66
from django.urls import reverse
7+
from tablib import Dataset
78

8-
from volunteer.admin import PyladiesChapterAdmin
9-
from volunteer.models import ApplicationStatus, PyladiesChapter, VolunteerProfile
9+
from volunteer.admin import PyladiesChapterAdmin, VolunteerProfileResource
10+
from volunteer.constants import ApplicationStatus
11+
from volunteer.models import PyladiesChapter, VolunteerProfile
1012

1113

1214
@pytest.mark.django_db
@@ -73,3 +75,68 @@ def test_pyladies_chapter_admin_view(self):
7375
has_logo_field = pyladies_chapter_admin.has_logo(chapter)
7476

7577
assert has_logo_field is True
78+
79+
80+
class TestVolunteerImportExport:
81+
def test_export_volunteer_does_not_trigger_email(self, portal_user):
82+
dataset = Dataset()
83+
dataset.headers = [
84+
"id",
85+
"user__first_name",
86+
"user__last_name",
87+
"user__email",
88+
"application_status",
89+
"github_username",
90+
"discord_username",
91+
"instagram_username",
92+
"bluesky_username",
93+
"mastodon_url",
94+
"x_username",
95+
"linkedin_url",
96+
"region",
97+
"chapter__chapter_name",
98+
]
99+
dataset.append(
100+
[
101+
portal_user.id,
102+
portal_user.first_name,
103+
portal_user.last_name,
104+
portal_user.email,
105+
ApplicationStatus.APPROVED.value,
106+
"",
107+
"",
108+
"",
109+
"",
110+
"",
111+
"",
112+
"",
113+
"North America",
114+
"",
115+
]
116+
)
117+
118+
dataset.append(
119+
[
120+
portal_user.id,
121+
portal_user.first_name,
122+
portal_user.last_name,
123+
portal_user.email,
124+
ApplicationStatus.CANCELLED.value,
125+
"",
126+
"",
127+
"",
128+
"",
129+
"",
130+
"",
131+
"",
132+
"USA",
133+
"",
134+
]
135+
)
136+
137+
resource = VolunteerProfileResource()
138+
mail.outbox.clear()
139+
resource.import_data(dataset, dry_run=False)
140+
assert len(mail.outbox) == 0 # no email
141+
resource.import_data(dataset, dry_run=True)
142+
assert len(mail.outbox) == 0 # no email

volunteer/admin.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,32 @@ def bulk_waitlist_volunteers(modeladmin, request, queryset):
1717
volunteer.save()
1818

1919

20-
class VolunteerProfileAdmin(admin.ModelAdmin):
20+
class VolunteerProfileResource(resources.ModelResource):
21+
def before_save_instance(self, instance, row, **kwargs):
22+
# during 'confirm' step, dry_run is True
23+
instance.from_import_export = True
24+
25+
class Meta:
26+
model = VolunteerProfile
27+
fields = (
28+
"id",
29+
"user__first_name",
30+
"user__last_name",
31+
"user__email",
32+
"application_status",
33+
"github_username",
34+
"discord_username",
35+
"instagram_username",
36+
"bluesky_username",
37+
"mastodon_url",
38+
"x_username",
39+
"linkedin_url",
40+
"region",
41+
"chapter__chapter_name",
42+
)
43+
44+
45+
class VolunteerProfileAdmin(ImportExportModelAdmin):
2146
list_display = (
2247
"user",
2348
"user__first_name",
@@ -38,6 +63,7 @@ class VolunteerProfileAdmin(admin.ModelAdmin):
3863
)
3964
list_filter = ("region", "application_status")
4065
actions = [bulk_waitlist_volunteers]
66+
resource_classes = [VolunteerProfileResource]
4167

4268

4369
class PyladiesChapterResource(resources.ModelResource):

0 commit comments

Comments
 (0)