villafleurie/rental/bookings.py
2019-12-06 17:04:18 +01:00

138 lines
5.3 KiB
Python

from __future__ import print_function
from googleapiclient import sample_tools
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import os.path
import pickle
import datetime
from oauth2client import client
import sys
# from rental.models import Reservation
def get_bookings(place):
"""
returns a list of all related place reservations
"""
booked_dates = Reservation.objects.all()
return [booking for booking in booked_dates if booking.place.name == f"{place.name}"]
def check_availability(place, start_date, end_date):
"""
check if the related place is available during a given period
"""
bookings = get_bookings(place)
for booking in bookings:
if (booking.start <= start_date <= booking.end) or (booking.start <= end_date <= booking.end):
return False
return True
"""Simple command-line sample for the Calendar API.
Command-line application that retrieves the list of the user's calendars."""
def main1(argv):
# Authenticate and construct service.
service, _ = sample_tools.init(
argv, 'calendar', 'v3', __doc__, __file__,
scope='https://www.googleapis.com/auth/calendar.readonly')
try:
page_token = None
while True:
calendar_list = service.calendarList().list(
pageToken=page_token).execute()
for calendar_list_entry in calendar_list['items']:
print(calendar_list_entry['summary'])
page_token = calendar_list.get('nextPageToken')
if not page_token:
break
# Call the Calendar API
calendar_list = service.calendarList().list(
pageToken=page_token).execute()
# print(calendar_list)
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
print('Getting the upcoming 10 T2 arrivals')
events_result = service.events().list(calendarId='burik7aclvhc7vsboh06c179uo@group.calendar.google.com', timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
print('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
end = event['end'].get('dateTime', event['end'].get('date'))
print(start, end, event['summary'])
print('Getting the upcoming 10 T3 arrivals')
events_result = service.events().list(calendarId='fu7h30p0gk4a2p4nvo7nsbgpok@group.calendar.google.com', timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
print('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
end = event['end'].get('dateTime', event['end'].get('date'))
print(start, end, event['summary'])
except client.AccessTokenRefreshError:
print('The credentials have been revoked or expired, please re-run'
'the application to re-authorize.')
# If modifying these scopes, delete the file token.pickle.
# SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
# def main():
# """Shows basic usage of the Google Calendar API.
# Prints the start and name of the next 10 events on the user's calendar.
# """
# creds = None
# # The file token.pickle stores the user's access and refresh tokens, and is
# # created automatically when the authorization flow completes for the first
# # time.
# if os.path.exists('token.pickle'):
# with open('token.pickle', 'rb') as token:
# creds = pickle.load(token)
# # If there are no (valid) credentials available, let the user log in.
# if not creds or not creds.valid:
# if creds and creds.expired and creds.refresh_token:
# creds.refresh(Request())
# else:
# flow = InstalledAppFlow.from_client_secrets_file(
# 'credentials.json', SCOPES)
# creds = flow.run_local_server(port=0)
# # Save the credentials for the next run
# with open('token.pickle', 'wb') as token:
# pickle.dump(creds, token)
# service = build('calendar', 'v3', credentials=creds)
# # Call the Calendar API
# now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
# print('Getting the upcoming 10 events')
# events_result = service.events().list(calendarId='primary', timeMin=now,
# maxResults=10, singleEvents=True,
# orderBy='startTime').execute()
# events = events_result.get('items', [])
# if not events:
# print('No upcoming events found.')
# for event in events:
# start = event['start'].get('dateTime', event['start'].get('date'))
# print(start, event['summary'])
if __name__ == '__main__':
main1(sys.argv)
# main()