11from dataclasses import asdict
22from enum import Enum
3- from typing import Annotated , Optional , Union
3+ from typing import Annotated
44
55import strawberry
66from django .db import transaction
1717)
1818from generic_forms .models import Form , FormAnswer
1919from generic_forms .services import validate_answers , wrap_answers
20- from grants . models import Grant as GrantModel
20+ from grants import models as grant_models
2121from grants .tasks import (
2222 create_and_send_voucher_to_grantee ,
2323 get_name ,
@@ -196,7 +196,7 @@ class SendGrantInput(BaseGrantInput):
196196 def validate (self , conference : Conference , user : User ) -> GrantErrors | None :
197197 errors = super ().validate (conference = conference , user = user )
198198
199- if GrantModel .objects .of_user (user ).for_conference (conference ).exists ():
199+ if grant_models . Grant .objects .of_user (user ).for_conference (conference ).exists ():
200200 errors .add_error ("non_field_errors" , "Grant already submitted!" )
201201
202202 return errors .if_has_errors
@@ -239,11 +239,11 @@ def validate(self, conference: Conference, user: User) -> GrantErrors | None:
239239
240240
241241SendGrantResult = Annotated [
242- Union [ Grant , GrantErrors ] , strawberry .union (name = "SendGrantResult" )
242+ Grant | GrantErrors , strawberry .union (name = "SendGrantResult" )
243243]
244244
245245UpdateGrantResult = Annotated [
246- Union [ Grant , GrantErrors ] , strawberry .union (name = "UpdateGrantResult" )
246+ Grant | GrantErrors , strawberry .union (name = "UpdateGrantResult" )
247247]
248248
249249
@@ -252,14 +252,14 @@ class StatusOption(Enum):
252252 confirmed = "confirmed"
253253 refused = "refused"
254254
255- def to_grant_status (self ) -> GrantModel .Status :
256- return GrantModel .Status (self .name )
255+ def to_grant_status (self ) -> grant_models . Grant .Status :
256+ return grant_models . Grant .Status (self .name )
257257
258258
259259@strawberry .input
260260class SendGrantReplyInput :
261261 instance : strawberry .ID
262- status : Optional [ StatusOption ]
262+ status : StatusOption | None
263263
264264
265265@strawberry .type
@@ -268,7 +268,7 @@ class SendGrantReplyError:
268268
269269
270270SendGrantReplyResult = Annotated [
271- Union [ Grant , SendGrantReplyError ] , strawberry .union (name = "SendGrantReplyResult" )
271+ Grant | SendGrantReplyError , strawberry .union (name = "SendGrantReplyResult" )
272272]
273273
274274
@@ -300,33 +300,29 @@ def send_grant(self, info: Info, input: SendGrantInput) -> SendGrantResult:
300300 if errors := input .validate (conference = conference , user = request .user ):
301301 return errors
302302
303- instance = GrantModel .objects .create (
304- ** {
305- "user_id" : request .user .id ,
306- "conference" : conference ,
307- "name" : input .name ,
308- "full_name" : input .full_name ,
309- # soft columns are NOT NULL; on the answers path they are
310- # omitted from the input and stored empty
311- "age_group" : input .age_group or "" ,
312- "gender" : input .gender or "" ,
313- "occupation" : input .occupation or "" ,
314- "grant_type" : input .grant_type ,
315- "python_usage" : input .python_usage or "" ,
316- "been_to_other_events" : input .been_to_other_events or "" ,
317- "community_contribution" : input .community_contribution or "" ,
318- "needs_funds_for_travel" : input .needs_funds_for_travel ,
319- "need_visa" : input .need_visa ,
320- "need_accommodation" : input .need_accommodation ,
321- "why" : input .why or "" ,
322- "notes" : input .notes or "" ,
323- "departure_country" : input .departure_country ,
324- "nationality" : input .nationality ,
325- "departure_city" : input .departure_city ,
326- "form_answer" : _persist_form_answer (
327- input .answers , conference , request .user
328- ),
329- }
303+ instance = grant_models .Grant .objects .create (
304+ user_id = request .user .id ,
305+ conference = conference ,
306+ name = input .name ,
307+ full_name = input .full_name ,
308+ # soft columns are NOT NULL; on the answers path they are
309+ # omitted from the input and stored empty
310+ age_group = input .age_group or "" ,
311+ gender = input .gender or "" ,
312+ occupation = input .occupation or "" ,
313+ grant_type = input .grant_type ,
314+ python_usage = input .python_usage or "" ,
315+ been_to_other_events = input .been_to_other_events or "" ,
316+ community_contribution = input .community_contribution or "" ,
317+ needs_funds_for_travel = input .needs_funds_for_travel ,
318+ need_visa = input .need_visa ,
319+ need_accommodation = input .need_accommodation ,
320+ why = input .why or "" ,
321+ notes = input .notes or "" ,
322+ departure_country = input .departure_country ,
323+ nationality = input .nationality ,
324+ departure_city = input .departure_city ,
325+ form_answer = _persist_form_answer (input .answers , conference , request .user ),
330326 )
331327
332328 record_privacy_policy_acceptance (
@@ -362,16 +358,14 @@ def send_grant(self, info: Info, input: SendGrantInput) -> SendGrantResult:
362358
363359 create_addition_admin_log_entry (request .user , instance , "Grant created." )
364360
365- # hack because we return django models
366- instance .__strawberry_definition__ = Grant .__strawberry_definition__
367361 return instance
368362
369363 @strawberry .mutation (permission_classes = [IsAuthenticated ])
370364 @transaction .atomic
371365 def update_grant (self , info : Info , input : UpdateGrantInput ) -> UpdateGrantResult :
372366 request = info .context .request
373367
374- instance = GrantModel .objects .get (id = input .instance )
368+ instance = grant_models . Grant .objects .get (id = input .instance )
375369 if not instance .can_edit (request .user ):
376370 return GrantErrors .with_error (
377371 "non_field_errors" , "You cannot edit this grant"
@@ -415,7 +409,6 @@ def update_grant(self, info: Info, input: UpdateGrantInput) -> UpdateGrantResult
415409 },
416410 )
417411
418- instance .__strawberry_definition__ = Grant .__strawberry_definition__
419412 return instance
420413
421414 @strawberry .mutation (permission_classes = [IsAuthenticated ])
@@ -424,19 +417,25 @@ def send_grant_reply(
424417 ) -> SendGrantReplyResult :
425418 request = info .context .request
426419
427- grant = GrantModel .objects .get (id = input .instance )
420+ grant = grant_models . Grant .objects .get (id = input .instance )
428421 if not grant .can_edit (request .user ):
429422 return SendGrantReplyError (message = "You cannot reply to this grant" )
430423
431424 # Can't modify the status if the grant is still pending or was already rejected
432- if grant .status in (GrantModel .Status .pending , GrantModel .Status .rejected ):
425+ if grant .status in (
426+ grant_models .Grant .Status .pending ,
427+ grant_models .Grant .Status .rejected ,
428+ ):
433429 return SendGrantReplyError (message = "You cannot reply to this grant" )
434430
435431 old_status = grant .status
436432 grant .status = input .status .to_grant_status ()
437433 grant .save ()
438434
439- if old_status != grant .status and grant .status == GrantModel .Status .confirmed :
435+ if (
436+ old_status != grant .status
437+ and grant .status == grant_models .Grant .Status .confirmed
438+ ):
440439 transaction .on_commit (
441440 lambda gid = grant .id : create_and_send_voucher_to_grantee .delay (
442441 grant_id = gid
@@ -450,4 +449,4 @@ def send_grant_reply(
450449 admin_url = request .build_absolute_uri (grant .get_admin_url ())
451450 notify_new_grant_reply_slack .delay (grant_id = grant .id , admin_url = admin_url )
452451
453- return Grant . from_model ( grant )
452+ return grant
0 commit comments