diff --git a/TODO.md b/TODO.md index e2997f4..9f15ccf 100644 --- a/TODO.md +++ b/TODO.md @@ -35,3 +35,4 @@ - [ ] Fuseau horaire - [ ] Calendar API: [docs](https://github.com/googleapis/google-api-python-client/blob/master/docs/start.md) - [ ] Blog +- [ ] Call update_calendar from BookingManager. SOlve circular dependencies diff --git a/rental/models/booking.py b/rental/models/booking.py index c489413..e274a19 100644 --- a/rental/models/booking.py +++ b/rental/models/booking.py @@ -2,6 +2,7 @@ from datetime import datetime from django.db import models from rental.models.guest import Guest from rental.models.place import Place +# from rental.services.calendar import update_calendar class BookingManager(models.Manager): @@ -10,10 +11,11 @@ class BookingManager(models.Manager): """ def create_booking(self, **kwargs): - """ create_booking creates a Booking instance. """ + """ create_booking creates a Booking instance, computes the price and + updates the remote calendar. """ booking = self.create(**kwargs) booking.price = booking.get_price() - + # update_calendar(booking) return booking diff --git a/rental/bookings.py b/rental/services/calendar.py similarity index 93% rename from rental/bookings.py rename to rental/services/calendar.py index 977f0d8..3f07c13 100644 --- a/rental/bookings.py +++ b/rental/services/calendar.py @@ -1,17 +1,17 @@ -from django.shortcuts import get_object_or_404 -from rental.models.booking import Booking -from rental.models.contact import Contact -from rental.models.guest import Guest -from rental.models.picture import Picture -from rental.models.place import Place -from rental.models.testimonial import Testimonial -from villafleurie.settings import BASE_DIR import datetime from google.auth.transport.requests import Request from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build import os.path import pickle +from villafleurie.settings import BASE_DIR +from django.shortcuts import get_object_or_404 +from rental.models.contact import Contact +from rental.models.guest import Guest +from rental.models.picture import Picture +from rental.models.place import Place +from rental.models.testimonial import Testimonial +from rental.models.booking import Booking def build_calendar_api_service(): @@ -151,4 +151,20 @@ def update_calendar(reservation): if __name__ == '__main__': + s, c = build_calendar_api_service() + + from google_auth_oauthlib.flow import Flow + + SCOPES = [ + 'https://www.googleapis.com/auth/calendar', + 'https://www.googleapis.com/auth/calendar.events' + ] + + SECRETS = '/client_secrets.json' + + flow = Flow.from_client_secrets_file( + SECRETS, + scopes=SCOPES, + redirect_uri='http://localhost:8080/' + ) diff --git a/rental/views.py b/rental/views.py index ad7bb8a..bf2d22b 100644 --- a/rental/views.py +++ b/rental/views.py @@ -10,8 +10,7 @@ from rental.models.guest import Guest from rental.models.picture import Picture from rental.models.place import Place from rental.models.testimonial import Testimonial - -from rental.bookings import check_availability, synchronize_calendars, update_calendar +from rental.services.calendar import check_availability, synchronize_calendars, update_calendar from rental.tasks.apiMailer import * # or gMailer @@ -81,15 +80,14 @@ def handle_reservation_form(request, context={}, init_template='rental/reservati place = get_object_or_404(Place, name=place_name) available = check_availability(place, start, end) - price = get_reservation_price(place, start, end) + if available: reservation = Booking.objects.create_booking( guest=guest, place=place, message=message, start=start, - end=end, - price=price + end=end ) send_quotation.delay(name, email) update_calendar(reservation)