Coverage for apps/practitioners/services.py: 21%
27 statements
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-30 13:30 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-30 13:30 -0600
1from services.google import GoogleService
3from .models import Practitioner
6class PractitionerGoogleHandler(GoogleService):
7 def __init__(self, practitioner: Practitioner):
8 print("***** en el init de practitioner service")
9 self.practitioner = practitioner
10 creds = self._get_credentials_for_user(user=practitioner.user)
11 if creds:
12 self._build_service()
14 #######
15 # High Level methods
16 #######
18 def setup_calendars(self):
19 """Gets calendars from google for a user"""
20 response = self._get_calendars_response()
21 if response:
22 calendars = response.get("items")
23 for item in calendars:
24 calendar, new = self.practitioner.calendars.update_or_create(
25 calendar_id=item.get("id"),
26 defaults={
27 "name": item.get("summary", None),
28 "json": item,
29 "timezone": item.get("timeZone", "America/Mexico_City"),
30 },
31 )
32 if (
33 new
34 and calendar.calendar_id == self.practitioner.user.email
35 and not self.practitioner.google_calendar
36 ):
37 calendar.for_sync = True
38 calendar.save()
39 self.service.close()
40 return response
41 else:
42 self.service.close()
43 return None
45 #######
46 # LOW Level methods
47 #######
49 def _get_calendars_response(self):
50 """Gets calendars from google for a user"""
51 if hasattr(self, "credentials"):
52 response = self.service.calendarList().list().execute()
53 return response
54 else:
55 return None