-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment.py
84 lines (72 loc) · 3.37 KB
/
assignment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
from datetime import datetime, timedelta
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import Flow
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect
from rest_framework.views import APIView
# Google Calendar API scopes
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
class GoogleCalendarInitView(APIView):
def get(self, request):
flow = Flow.from_client_secrets_file(
os.path.join(settings.BASE_DIR, 'client_secrets.json'),
scopes=SCOPES,
redirect_uri=request.build_absolute_uri('/rest/v1/calendar/redirect/')
)
authorization_url, state = flow.authorization_url(
access_type='offline',
include_granted_scopes='true'
)
request.session['google_auth_state'] = state
return HttpResponseRedirect(authorization_url)
class GoogleCalendarRedirectView(APIView):
def get(self, request):
state = request.session.get('google_auth_state')
flow = Flow.from_client_secrets_file(
os.path.join(settings.BASE_DIR, 'client_secrets.json'),
scopes=SCOPES,
redirect_uri=request.build_absolute_uri('/rest/v1/calendar/redirect/'),
state=state
)
authorization_response = request.build_absolute_uri()
flow.fetch_token(authorization_response=authorization_response)
if not request.user.is_authenticated:
return HttpResponse("User not authenticated")
# Save the credentials for future API calls
credentials = flow.credentials
request.user.google_calendar_credentials = credentials.to_json()
request.user.save()
# Redirect to a view that lists the events
return HttpResponseRedirect('/rest/v1/calendar/events/')
class GoogleCalendarEventsView(APIView):
def get(self, request):
if not request.user.is_authenticated:
return HttpResponse("User not authenticated")
# Load saved credentials for the user
credentials = Credentials.from_authorized_user_info(
request.user.google_calendar_credentials
)
if not credentials.valid:
if credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
return HttpResponse("Invalid credentials")
# Create a Google Calendar API service
service = build('calendar', 'v3', credentials=credentials)
# Get the list of events from the user's calendar
now = datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
events_result = service.events().list(calendarId='primary', timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
# Process and return the events
event_list = []
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
event_list.append({
'summary': event['summary'],
'start': start
})
return JsonResponse(event_list, safe=False)