diff --git a/.gitignore b/.gitignore index 2f1ef30..cf78c5b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ rental/static/ .vscode/ Procfile heroku.yml +rental/client_secrets.json diff --git a/calendar.dat b/calendar.dat new file mode 100644 index 0000000..e5cd4ce --- /dev/null +++ b/calendar.dat @@ -0,0 +1 @@ +{"access_token": "ya29.Il-0B4bh7Yq32B8mvoTT0EiAbYtzr1snXN_AwfcJzzaXu_L74OpPkkH7UgxQIzh3ZkiF_7fQq5VwZ5lIPE8ag5iei_D58rDeFdvT6x3V0MC9uOAyTBsejAlh2aJl9hY1Lg", "client_id": "1068382961407-1dudhnc4e9d7ham53nr6b7ak3c4dlvme.apps.googleusercontent.com", "client_secret": "F9zmMyRRiTUEAMfRiv8A2l1d", "refresh_token": "1//09MUSHKoeusLDCgYIARAAGAkSNwF-L9IrNOqe0TCFB9n1MZZwuAQNtfkdXAv7x4bUs2AxMwfmTdccxSMhCMEwjaXRUD2WR_EXiss", "token_expiry": "2019-12-06T16:13:57Z", "token_uri": "https://accounts.google.com/o/oauth2/token", "user_agent": null, "revoke_uri": "https://oauth2.googleapis.com/revoke", "id_token": null, "id_token_jwt": null, "token_response": {"access_token": "ya29.Il-0B4bh7Yq32B8mvoTT0EiAbYtzr1snXN_AwfcJzzaXu_L74OpPkkH7UgxQIzh3ZkiF_7fQq5VwZ5lIPE8ag5iei_D58rDeFdvT6x3V0MC9uOAyTBsejAlh2aJl9hY1Lg", "expires_in": 3600, "scope": "https://www.googleapis.com/auth/calendar.readonly", "token_type": "Bearer"}, "scopes": ["https://www.googleapis.com/auth/calendar.readonly"], "token_info_uri": "https://oauth2.googleapis.com/tokeninfo", "invalid": false, "_class": "OAuth2Credentials", "_module": "oauth2client.client"} \ No newline at end of file diff --git a/rental/bookings.py b/rental/bookings.py new file mode 100644 index 0000000..811bfe9 --- /dev/null +++ b/rental/bookings.py @@ -0,0 +1,138 @@ +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() diff --git a/rental/calendar.py b/rental/calendar.py deleted file mode 100644 index 666a7dc..0000000 --- a/rental/calendar.py +++ /dev/null @@ -1,19 +0,0 @@ -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 diff --git a/rental/models.py b/rental/models.py index c14bf83..29dc4b2 100644 --- a/rental/models.py +++ b/rental/models.py @@ -28,6 +28,7 @@ class Place(models.Model): thumbnail = models.ForeignKey( Image, on_delete=models.CASCADE, blank=True, null=True) images = models.ManyToManyField(Image, related_name="places", blank=True) + calendar = models.CharField(max_length=350, blank=True, null=True) class Guest(models.Model): diff --git a/rental/templates/rental/detail_place.html b/rental/templates/rental/detail_place.html index 8a6b3b3..980fc7f 100644 --- a/rental/templates/rental/detail_place.html +++ b/rental/templates/rental/detail_place.html @@ -1,154 +1,180 @@ -{% extends 'rental/base.html'%} -{% load static %} +{% extends 'rental/base.html'%} {% load static %} {% block content %} -{% block content %} - -
-
-
-
-
-

{{ place.subname }} {{ place.name }}

- VillaFleurie -
-
-
- +
+
+
+
+
+

{{ place.subname }} {{ place.name }}

+ VillaFleurie
+
+ +
-
- +
+
- - {% endblock %} diff --git a/rental/views.py b/rental/views.py index e1fdc70..03815fd 100644 --- a/rental/views.py +++ b/rental/views.py @@ -6,7 +6,7 @@ from django.views.generic.base import TemplateView from .forms import ReservationForm from django.db import IntegrityError from rental.pricing import get_reservation_price -from rental.calendar import check_availability +from rental.bookings import check_availability def index(request):