22from enum import Enum
33
44import strawberry
5+ import strawberry_django
56
67from api .submissions .types import Submission
7- from schedule . models import ScheduleItem as ScheduleItemModel
8+ from schedule import models
89
910
10- @strawberry .type
11+ @strawberry_django .type ( models . ScheduleItem )
1112class ScheduleInvitationDate :
12- id : strawberry .ID
13+ id : strawberry .auto
1314 start : datetime
1415 end : datetime
15- duration : int
1616
17- @classmethod
18- def from_django (cls , schedule_item ):
19- duration = schedule_item .duration or schedule_item .slot .duration
20- return cls (
21- id = schedule_item .id ,
22- start = schedule_item .start ,
23- end = schedule_item .end ,
24- duration = duration ,
25- )
17+ @strawberry_django .field
18+ def duration (self ) -> int :
19+ return self .duration or self .slot .duration
2620
2721
2822@strawberry .enum
@@ -33,43 +27,40 @@ class ScheduleInvitationOption(Enum):
3327 REJECT = "reject"
3428 CANT_ATTEND = "cant_attend"
3529
36- def to_schedule_item_status (self ):
37- return MAP_OPTION_TO_ITEM_STATUS .get (self )
38-
39- @staticmethod
40- def from_schedule_item_status (schedule_item_status : str ):
41- return MAP_ITEM_STATUS_TO_OPTION .get (schedule_item_status )
30+ def to_schedule_item_status (self ) -> str :
31+ return MAP_OPTION_TO_ITEM_STATUS [self ]
4232
4333
4434MAP_OPTION_TO_ITEM_STATUS = {
45- ScheduleInvitationOption .CONFIRM : ScheduleItemModel .STATUS .confirmed ,
46- ScheduleInvitationOption .MAYBE : ScheduleItemModel .STATUS .maybe ,
47- ScheduleInvitationOption .REJECT : ScheduleItemModel .STATUS .rejected ,
48- ScheduleInvitationOption .CANT_ATTEND : ScheduleItemModel .STATUS .cant_attend ,
49- ScheduleInvitationOption .NO_ANSWER : ScheduleItemModel .STATUS .waiting_confirmation ,
35+ ScheduleInvitationOption .CONFIRM : models . ScheduleItem .STATUS .confirmed ,
36+ ScheduleInvitationOption .MAYBE : models . ScheduleItem .STATUS .maybe ,
37+ ScheduleInvitationOption .REJECT : models . ScheduleItem .STATUS .rejected ,
38+ ScheduleInvitationOption .CANT_ATTEND : models . ScheduleItem .STATUS .cant_attend ,
39+ ScheduleInvitationOption .NO_ANSWER : models . ScheduleItem .STATUS .waiting_confirmation ,
5040}
5141
5242MAP_ITEM_STATUS_TO_OPTION = {
5343 item : option for option , item in MAP_OPTION_TO_ITEM_STATUS .items ()
5444}
5545
5646
57- @strawberry .type
47+ @strawberry_django .type ( models . ScheduleItem )
5848class ScheduleInvitation :
59- id : strawberry .ID
60- option : ScheduleInvitationOption
61- notes : str
62- title : str
49+ id : strawberry .ID = strawberry_django .field (
50+ resolver = lambda self : self .submission .hashid ,
51+ select_related = ["submission" ],
52+ )
53+ option : ScheduleInvitationOption = strawberry_django .field (
54+ resolver = lambda self : MAP_ITEM_STATUS_TO_OPTION [self .status ],
55+ only = ["status" ],
56+ )
57+ notes : str = strawberry_django .field (field_name = "speaker_invitation_notes" )
58+ title : strawberry .auto
6359 submission : Submission
64- dates : list [ScheduleInvitationDate ]
6560
66- @classmethod
67- def from_django_model (cls , instance ):
68- return cls (
69- id = instance .submission .hashid ,
70- title = instance .title ,
71- option = ScheduleInvitationOption .from_schedule_item_status (instance .status ),
72- notes = instance .speaker_invitation_notes ,
73- submission = instance .submission ,
74- dates = [ScheduleInvitationDate .from_django (instance )],
75- )
61+ @strawberry_django .field (
62+ only = ["duration" , "slot__duration" , "slot__hour" , "slot__day__day" ],
63+ select_related = ["slot__day" ],
64+ )
65+ def dates (self ) -> list [ScheduleInvitationDate ]:
66+ return [self ]
0 commit comments