2
2
import logging
3
3
import typing
4
4
5
- from django .conf import settings
6
5
from django .core .management .base import BaseCommand
7
6
8
7
from apps .common .models import Event
9
8
from 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
+ )
11
14
from apps .project .models import Deadline
12
15
from apps .project .tasks import sync_deadline_with_google_calendar
16
+ from main import config
13
17
14
18
CommandActionType = typing .Literal [
15
19
"list-calendars" ,
@@ -51,10 +55,18 @@ def list_all_events(service: GoogleServiceAccount, calendar_id, time_min: str |
51
55
class Command (BaseCommand ):
52
56
help = "Initialize google calendar"
53
57
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
+
54
65
def confirm (self , message : str ) -> bool :
55
66
prompt = self .style .NOTICE (f"{ message } (y/n): " )
56
67
return input (prompt ).strip ().lower () == "y"
57
68
69
+ @typing .override
58
70
def add_arguments (self , parser ):
59
71
subparsers = parser .add_subparsers (dest = "action" , help = "Actions" , required = True )
60
72
@@ -101,7 +113,7 @@ def add_arguments(self, parser):
101
113
subparsers .add_parser ("reset-timur-data" , help = "List calendars" )
102
114
103
115
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 )
105
117
for cal in service .list_calendar ():
106
118
self .stdout .write (json .dumps (cal , indent = 2 ))
107
119
@@ -118,7 +130,7 @@ def share_calendar(
118
130
role = typing .cast ("GoogleCalendarShareRoleType" , options ["role" ])
119
131
calendar_id = typing .cast (
120
132
"str" ,
121
- options .get ("calendar_id" ) or settings . GOOGLE_CALENDAR_ID ,
133
+ options .get ("calendar_id" ) or self . calendar_id ,
122
134
)
123
135
124
136
if role == "owner" :
@@ -150,7 +162,7 @@ def list_events(self, service: GoogleServiceAccount, **options):
150
162
time_min = options ["time_min" ]
151
163
google_calendar_events = list_all_events (
152
164
service ,
153
- calendar_id = settings . GOOGLE_CALENDAR_ID ,
165
+ calendar_id = self . calendar_id ,
154
166
time_min = time_min ,
155
167
)
156
168
for result in google_calendar_events :
@@ -161,7 +173,7 @@ def list_colors(self, service: GoogleServiceAccount):
161
173
self .stdout .write (json .dumps (list (results .items ()), indent = 2 ))
162
174
163
175
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
165
177
compact = options .get ("compact" , False )
166
178
167
179
results = service .service_account .acl ().list (calendarId = calendar_id ).execute ()
@@ -183,7 +195,7 @@ def delete_event(self, service: GoogleServiceAccount, **options):
183
195
184
196
if self .confirm ("Are you sure?" ):
185
197
service .service_account .events ().delete (
186
- calendarId = settings . GOOGLE_CALENDAR_ID ,
198
+ calendarId = self . calendar_id ,
187
199
eventId = event_id ,
188
200
).execute ()
189
201
@@ -252,7 +264,7 @@ def reset_timur_data(self, service: GoogleServiceAccount):
252
264
"deadlines" : to_process_deadline_qs .count (),
253
265
}
254
266
255
- calendar_id = settings . GOOGLE_CALENDAR_ID
267
+ calendar_id = self . calendar_id
256
268
if not self .confirm (f"Are you sure? This will remove { summary } " ):
257
269
return
258
270
@@ -288,6 +300,7 @@ def reset_timur_data(self, service: GoogleServiceAccount):
288
300
self .stdout .write (f" - Success { events_resp } " )
289
301
self .stdout .write (f" - Success { deadline_resp } " )
290
302
303
+ @typing .override
291
304
def handle (self , action : CommandActionType , ** options ):
292
305
gsc = GoogleServiceAccount ()
293
306
0 commit comments