Skip to content

Commit

Permalink
Update actions
Browse files Browse the repository at this point in the history
  • Loading branch information
estyxx committed Feb 8, 2024
1 parent 3a82662 commit c9ad3f7
Showing 1 changed file with 47 additions and 36 deletions.
83 changes: 47 additions & 36 deletions backend/grants/admin/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,67 @@
from pretix import create_voucher
from grants.models import Grant

from functools import wraps


def _check_amounts_are_not_empty(grant: Grant, request):
if grant.total_amount is None:
messages.error(

Check warning on line 20 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L20

Added line #L20 was not covered by tests
request,
f"Grant for {grant.name} is missing 'Total Amount'!",
)
return
return False

Check warning on line 24 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L24

Added line #L24 was not covered by tests

if (
grant.grant_type
in (
Grant.ApprovedType.ticket_accommodation,
Grant.ApprovedType.ticket_travel_accommodation,
)
and grant.accommodation_amount is None
):
if grant.has_approved_accommodation() and grant.accommodation_amount is None:
messages.error(

Check warning on line 27 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L27

Added line #L27 was not covered by tests
request,
f"Grant for {grant.name} is missing 'Accommodation Amount'!",
)
return
return False

Check warning on line 31 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L31

Added line #L31 was not covered by tests

if (
grant.grant_type
in (
Grant.ApprovedType.ticket_travel,
Grant.ApprovedType.ticket_travel_accommodation,
)
and grant.travel_amount is None
):
if grant.has_approved_travel() and grant.travel_amount is None:
messages.error(

Check warning on line 34 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L34

Added line #L34 was not covered by tests
request,
f"Grant for {grant.name} is missing 'Travel Amount'!",
)
return
return False

Check warning on line 38 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L38

Added line #L38 was not covered by tests

return True

Check warning on line 40 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L40

Added line #L40 was not covered by tests


def validate_single_conference_selection(func):
"""
Ensure all selected grants in the queryset belong to the same conference.
"""

@wraps(func)
def wrapper(modeladmin, request, queryset):
is_filtered_by_conference = (

Check warning on line 50 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L50

Added line #L50 was not covered by tests
queryset.values_list("conference_id").distinct().count() == 1
)

if not is_filtered_by_conference:
messages.error(request, "Please select only one conference")
return

Check warning on line 56 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L55-L56

Added lines #L55 - L56 were not covered by tests

return func(modeladmin, request, queryset)

Check warning on line 58 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L58

Added line #L58 was not covered by tests

return wrapper


@admin.action(description="Send Approved/Waiting List/Rejected reply emails")
@validate_single_conference_selection
def send_reply_emails(modeladmin, request, queryset):
conference = queryset.first().conference

Check warning on line 66 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L66

Added line #L66 was not covered by tests

if not conference.visa_application_form_link:
messages.error(

Check warning on line 69 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L69

Added line #L69 was not covered by tests
request,
"Visa Application Form Link Missing: Please ensure the link to the Visa "
"Application Form is set in the Conference admin settings.",
)
return

Check warning on line 74 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L74

Added line #L74 was not covered by tests

queryset = queryset.filter(

Check warning on line 76 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L76

Added line #L76 was not covered by tests
status__in=(
Grant.Status.approved,
Expand All @@ -68,6 +89,7 @@ def send_reply_emails(modeladmin, request, queryset):
return

Check warning on line 89 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L89

Added line #L89 was not covered by tests

for grant in queryset:

if grant.status in (Grant.Status.approved,):
if grant.approved_type is None:
messages.error(

Check warning on line 95 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L95

Added line #L95 was not covered by tests
Expand All @@ -76,7 +98,8 @@ def send_reply_emails(modeladmin, request, queryset):
)
return

Check warning on line 99 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L99

Added line #L99 was not covered by tests

_check_amounts_are_not_empty(grant, request)
if not _check_amounts_are_not_empty(grant, request):
return

Check warning on line 102 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L102

Added line #L102 was not covered by tests

now = timezone.now()
grant.applicant_reply_deadline = timezone.datetime(

Check warning on line 105 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L104-L105

Added lines #L104 - L105 were not covered by tests
Expand All @@ -100,6 +123,7 @@ def send_reply_emails(modeladmin, request, queryset):


@admin.action(description="Send reminder to waiting confirmation grants")
@validate_single_conference_selection
def send_grant_reminder_to_waiting_for_confirmation(modeladmin, request, queryset):
queryset = queryset.filter(

Check warning on line 128 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L128

Added line #L128 was not covered by tests
status__in=(Grant.Status.waiting_for_confirmation,),
Expand All @@ -122,6 +146,7 @@ def send_grant_reminder_to_waiting_for_confirmation(modeladmin, request, queryse


@admin.action(description="Send Waiting List update email")
@validate_single_conference_selection
def send_reply_email_waiting_list_update(modeladmin, request, queryset):
queryset = queryset.filter(

Check warning on line 151 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L151

Added line #L151 was not covered by tests
status__in=(
Expand All @@ -136,15 +161,8 @@ def send_reply_email_waiting_list_update(modeladmin, request, queryset):


@admin.action(description="Send voucher via email")
@validate_single_conference_selection
def send_voucher_via_email(modeladmin, request, queryset):
is_filtered_by_conference = (
queryset.values_list("conference_id").distinct().count() == 1
)

if not is_filtered_by_conference:
messages.error(request, "Please select only one conference")
return

count = 0

Check warning on line 166 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L166

Added line #L166 was not covered by tests
for grant in queryset.filter(pretix_voucher_id__isnull=False):
send_grant_voucher_email.delay(grant_id=grant.id)
Expand All @@ -160,15 +178,8 @@ def _generate_voucher_code(prefix: str) -> str:


@admin.action(description="Create grant vouchers on Pretix")
@validate_single_conference_selection
def create_grant_vouchers_on_pretix(modeladmin, request, queryset):
is_filtered_by_conference = (
queryset.values_list("conference_id").distinct().count() == 1
)

if not is_filtered_by_conference:
messages.error(request, "Please select only one conference")
return

conference = queryset.first().conference

Check warning on line 183 in backend/grants/admin/actions.py

View check run for this annotation

Codecov / codecov/patch

backend/grants/admin/actions.py#L183

Added line #L183 was not covered by tests

if not conference.pretix_speaker_voucher_quota_id:
Expand Down

0 comments on commit c9ad3f7

Please sign in to comment.