Skip to content

Commit

Permalink
Store privacy policy acceptance when sending a grant (#4108)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoacierno authored Oct 6, 2024
1 parent d14cf01 commit 24c00f8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
37 changes: 23 additions & 14 deletions backend/api/grants/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from enum import Enum
from typing import Annotated, Union, Optional

from privacy_policy.record import record_privacy_policy_acceptance
import strawberry
from strawberry.types import Info

from django.db import transaction
from api.grants.types import (
AgeGroup,
Grant,
Expand Down Expand Up @@ -121,13 +122,13 @@ class SendGrantInput(BaseGrantInput):
linkedin_url: str
mastodon_handle: str

def validate(self, conference: Conference, user: User) -> GrantErrors:
def validate(self, conference: Conference, user: User) -> GrantErrors | None:
errors = super().validate(conference=conference, user=user)

if GrantModel.objects.of_user(user).for_conference(conference).exists():
errors.add_error("non_field_errors", "Grant already submitted!")

return errors
return errors.if_has_errors


@strawberry.input
Expand Down Expand Up @@ -156,6 +157,9 @@ class UpdateGrantInput(BaseGrantInput):
linkedin_url: str
mastodon_handle: str

def validate(self, conference: Conference, user: User) -> GrantErrors | None:
return super().validate(conference=conference, user=user).if_has_errors


SendGrantResult = Annotated[
Union[Grant, GrantErrors], strawberry.union(name="SendGrantResult")
Expand Down Expand Up @@ -199,17 +203,23 @@ def send_grant(self, info: Info, input: SendGrantInput) -> SendGrantResult:

conference = Conference.objects.filter(code=input.conference).first()

errors = input.validate(conference=conference, user=request.user)
if errors.has_errors:
if errors := input.validate(conference=conference, user=request.user):
return errors

instance = GrantModel.objects.create(
**{
**asdict(input),
"user_id": request.user.id,
"conference": conference,
}
)
with transaction.atomic():
instance = GrantModel.objects.create(
**{
**asdict(input),
"user_id": request.user.id,
"conference": conference,
}
)

record_privacy_policy_acceptance(
info.context.request,
conference,
"grant",
)

# hack because we return django models
instance.__strawberry_definition__ = Grant.__strawberry_definition__
Expand All @@ -226,8 +236,7 @@ def update_grant(self, info: Info, input: UpdateGrantInput) -> UpdateGrantResult
)

input.conference = instance.conference
errors = input.validate(conference=input.conference, user=request.user)
if errors.has_errors:
if errors := input.validate(conference=input.conference, user=request.user):
return errors

for attr, value in asdict(input).items():
Expand Down
5 changes: 5 additions & 0 deletions backend/api/grants/tests/test_send_grant.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from privacy_policy.models import PrivacyPolicyAcceptanceRecord
from conferences.tests.factories import ConferenceFactory
from grants.tests.factories import GrantFactory
import pytest
Expand Down Expand Up @@ -88,6 +89,10 @@ def test_send_grant(graphql_client, user):
assert response["data"]["sendGrant"]["__typename"] == "Grant"
assert response["data"]["sendGrant"]["id"]

assert PrivacyPolicyAcceptanceRecord.objects.filter(
user=user, conference=conference, privacy_policy="grant"
).exists()


def test_cannot_send_a_grant_if_grants_are_closed(graphql_client, user):
graphql_client.force_login(user)
Expand Down

0 comments on commit 24c00f8

Please sign in to comment.