22import logging
33import typing
44
5- from django .conf import settings
65from django .core .management .base import BaseCommand
76
87from apps .common .models import Event
98from apps .common .tasks import sync_event_with_google_calendar
10- from apps .common .utils .google_calendar import GoogleCalendarShareRoleType , GoogleServiceAccount
9+ from apps .common .utils .google_calendar import (
10+ GoogleCalendarInitialisationError ,
11+ GoogleCalendarShareRoleType ,
12+ GoogleServiceAccount ,
13+ )
1114from apps .project .models import Deadline
1215from apps .project .tasks import sync_deadline_with_google_calendar
16+ from main import config
1317
1418CommandActionType = typing .Literal [
1519 "list-calendars" ,
@@ -51,10 +55,18 @@ def list_all_events(service: GoogleServiceAccount, calendar_id, time_min: str |
5155class Command (BaseCommand ):
5256 help = "Initialize google calendar"
5357
58+ def __init__ (self , * args , ** kwargs ):
59+ if config .GOOGLE_CALENDAR_ID is None :
60+ raise GoogleCalendarInitialisationError ("GOOGLE_CALENDAR_ID is not defined" )
61+
62+ self .calendar_id = config .GOOGLE_CALENDAR_ID
63+ return super ().__init__ (* args , ** kwargs )
64+
5465 def confirm (self , message : str ) -> bool :
5566 prompt = self .style .NOTICE (f"{ message } (y/n): " )
5667 return input (prompt ).strip ().lower () == "y"
5768
69+ @typing .override
5870 def add_arguments (self , parser ):
5971 subparsers = parser .add_subparsers (dest = "action" , help = "Actions" , required = True )
6072
@@ -101,7 +113,7 @@ def add_arguments(self, parser):
101113 subparsers .add_parser ("reset-timur-data" , help = "List calendars" )
102114
103115 def list_calendars (self , service : GoogleServiceAccount ):
104- logger .info ("Action calendar ID: %s" , settings . GOOGLE_CALENDAR_ID )
116+ logger .info ("Action calendar ID: %s" , self . calendar_id )
105117 for cal in service .list_calendar ():
106118 self .stdout .write (json .dumps (cal , indent = 2 ))
107119
@@ -118,7 +130,7 @@ def share_calendar(
118130 role = typing .cast ("GoogleCalendarShareRoleType" , options ["role" ])
119131 calendar_id = typing .cast (
120132 "str" ,
121- options .get ("calendar_id" ) or settings . GOOGLE_CALENDAR_ID ,
133+ options .get ("calendar_id" ) or self . calendar_id ,
122134 )
123135
124136 if role == "owner" :
@@ -150,7 +162,7 @@ def list_events(self, service: GoogleServiceAccount, **options):
150162 time_min = options ["time_min" ]
151163 google_calendar_events = list_all_events (
152164 service ,
153- calendar_id = settings . GOOGLE_CALENDAR_ID ,
165+ calendar_id = self . calendar_id ,
154166 time_min = time_min ,
155167 )
156168 for result in google_calendar_events :
@@ -161,7 +173,7 @@ def list_colors(self, service: GoogleServiceAccount):
161173 self .stdout .write (json .dumps (list (results .items ()), indent = 2 ))
162174
163175 def list_calendar_access (self , service : GoogleServiceAccount , ** options ):
164- calendar_id = options .get ("calendar_id" ) or settings . GOOGLE_CALENDAR_ID
176+ calendar_id = options .get ("calendar_id" ) or self . calendar_id
165177 compact = options .get ("compact" , False )
166178
167179 results = service .service_account .acl ().list (calendarId = calendar_id ).execute ()
@@ -183,7 +195,7 @@ def delete_event(self, service: GoogleServiceAccount, **options):
183195
184196 if self .confirm ("Are you sure?" ):
185197 service .service_account .events ().delete (
186- calendarId = settings . GOOGLE_CALENDAR_ID ,
198+ calendarId = self . calendar_id ,
187199 eventId = event_id ,
188200 ).execute ()
189201
@@ -252,7 +264,7 @@ def reset_timur_data(self, service: GoogleServiceAccount):
252264 "deadlines" : to_process_deadline_qs .count (),
253265 }
254266
255- calendar_id = settings . GOOGLE_CALENDAR_ID
267+ calendar_id = self . calendar_id
256268 if not self .confirm (f"Are you sure? This will remove { summary } " ):
257269 return
258270
@@ -288,6 +300,7 @@ def reset_timur_data(self, service: GoogleServiceAccount):
288300 self .stdout .write (f" - Success { events_resp } " )
289301 self .stdout .write (f" - Success { deadline_resp } " )
290302
303+ @typing .override
291304 def handle (self , action : CommandActionType , ** options ):
292305 gsc = GoogleServiceAccount ()
293306
0 commit comments