mirror of
https://github.com/rjNemo/villafleurie
synced 2026-06-12 13:26:47 +00:00
get bookings from calende ok …
This commit is contained in:
parent
8695c988a6
commit
6788b71211
7 changed files with 278 additions and 36 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -9,4 +9,6 @@ rental/static/
|
||||||
Procfile
|
Procfile
|
||||||
heroku.yml
|
heroku.yml
|
||||||
rental/client_secrets.json
|
rental/client_secrets.json
|
||||||
|
calendar.dat
|
||||||
|
token.pickle
|
||||||
|
villafleurie.pgsql
|
||||||
|
|
@ -96,3 +96,5 @@ Le visiteur doit pouvoir :
|
||||||
\_ nettoyer les statics files. Garder que les définitions utiles
|
\_ nettoyer les statics files. Garder que les définitions utiles
|
||||||
|
|
||||||
## BUGS
|
## BUGS
|
||||||
|
|
||||||
|
- La synchro ne gère pas les heures dans le calendriers.
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
import datetime
|
import datetime
|
||||||
from rental.pricing import get_reservation_price
|
from rental.pricing import get_reservation_price
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from rental.models import Reservation, Place
|
from rental.models import Reservation, Place, Guest
|
||||||
import datetime
|
import datetime
|
||||||
from google.auth.transport.requests import Request
|
from google.auth.transport.requests import Request
|
||||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||||
from googleapiclient.discovery import build
|
from googleapiclient.discovery import build
|
||||||
import os.path
|
import os.path
|
||||||
import pickle
|
import pickle
|
||||||
|
from villafleurie.settings import BASE_DIR
|
||||||
|
|
||||||
|
|
||||||
def get_bookings(place):
|
def get_bookings(place):
|
||||||
|
|
@ -49,8 +50,9 @@ def synchronize_calendars():
|
||||||
if creds and creds.expired and creds.refresh_token:
|
if creds and creds.expired and creds.refresh_token:
|
||||||
creds.refresh(Request())
|
creds.refresh(Request())
|
||||||
else:
|
else:
|
||||||
flow = InstalledAppFlow.from_client_secrets_file(
|
SECRETS = os.path.join(BASE_DIR, 'rental/client_secrets.json')
|
||||||
'client_secrets.json', scopes=SCOPES, redirect_uri="http://localhost:8080/")
|
flow = InstalledAppFlow.from_client_secrets_file(SECRETS, scopes=SCOPES,
|
||||||
|
redirect_uri="http://localhost:8080/")
|
||||||
creds = flow.run_local_server()
|
creds = flow.run_local_server()
|
||||||
# Save the credentials for the next run
|
# Save the credentials for the next run
|
||||||
with open('token.pickle', 'wb') as token:
|
with open('token.pickle', 'wb') as token:
|
||||||
|
|
@ -74,32 +76,38 @@ def synchronize_calendars():
|
||||||
if not events:
|
if not events:
|
||||||
print('No upcoming events found.')
|
print('No upcoming events found.')
|
||||||
else:
|
else:
|
||||||
reservation = {}
|
# reservation = {}
|
||||||
for index, event in enumerate(events):
|
for event in events:
|
||||||
reservation[index] = {
|
reservation = {
|
||||||
'place': calendar,
|
'place': calendar,
|
||||||
'guest': event['summary'],
|
'guest': event['summary'],
|
||||||
'start': event['start'].get('dateTime', event['start'].get('date')),
|
'start': event['start'].get('dateTime', event['start'].get('date')),
|
||||||
'end': event['end'].get('dateTime', event['end'].get('date'))
|
'end': event['end'].get('dateTime', event['end'].get('date'))
|
||||||
}
|
}
|
||||||
print(reservation[index])
|
print(reservation)
|
||||||
|
|
||||||
# if booking not in db -> create
|
# if booking not in db -> create
|
||||||
# if booking in db and modification_date_cal > update_date_db -> update
|
# if booking in db and modification_date_cal > update_date_db -> update
|
||||||
|
# try : create/update
|
||||||
place = get_object_or_404(Place, name=calendar)
|
# except : send a log message
|
||||||
price = get_reservation_price(
|
try:
|
||||||
place, reservation['start'], reservation['end'])
|
place = get_object_or_404(Place, name=calendar)
|
||||||
# trouver si guest existe déjà, créer sinon
|
price = get_reservation_price(
|
||||||
guest = Guest.objects.create(name=reservation['guest'])
|
place, reservation['start'], reservation['end'])
|
||||||
|
# trouver si guest existe déjà, créer sinon
|
||||||
Reservation.objects.create(
|
guest = Guest.objects.create(name=reservation['guest'])
|
||||||
place=place,
|
start = reservation['start']
|
||||||
guest=guest,
|
end = reservation['end']
|
||||||
start=start,
|
Reservation.objects.create(
|
||||||
end=end,
|
place=place,
|
||||||
price=price
|
guest=guest,
|
||||||
)
|
start=start,
|
||||||
|
end=end,
|
||||||
|
price=price
|
||||||
|
)
|
||||||
|
except:
|
||||||
|
print(
|
||||||
|
f"######## ERROR ! Can't create {guest} reservation ########")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ class Guest(models.Model):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
name = models.CharField(max_length=100)
|
name = models.CharField(max_length=100)
|
||||||
email = models.EmailField(unique=True)
|
email = models.EmailField(unique=False)
|
||||||
phone = models.CharField(max_length=30, blank=True)
|
phone = models.CharField(max_length=30, blank=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,20 @@
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
def get_reservation_price(place, start, end):
|
def get_reservation_price(place, start, end):
|
||||||
"""
|
"""
|
||||||
Compute booking price as a function of place and dates
|
Compute booking price as a function of place and dates
|
||||||
"""
|
"""
|
||||||
|
start = datetime.strptime(start, '%Y-%m-%d')
|
||||||
|
end = datetime.strptime(end, '%Y-%m-%d')
|
||||||
|
|
||||||
nights = (end - start).days
|
nights = (end - start).days
|
||||||
return place.price * nights
|
return place.price * nights
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from rental.models import Place
|
from rental.models import Place
|
||||||
from datetime import *
|
from datetime import datetime
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
place_name = 'T2'
|
place_name = 'T2'
|
||||||
place = get_object_or_404(Place, name=place_name)
|
place = get_object_or_404(Place, name=place_name)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ from django.views.generic.base import TemplateView
|
||||||
from .forms import ReservationForm
|
from .forms import ReservationForm
|
||||||
from django.db import IntegrityError
|
from django.db import IntegrityError
|
||||||
from rental.pricing import get_reservation_price
|
from rental.pricing import get_reservation_price
|
||||||
from rental.bookings import check_availability # , synchronize_calendars
|
from rental.bookings import check_availability, synchronize_calendars
|
||||||
# import sys
|
# import sys
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -143,6 +143,7 @@ def calendar(request, place_name):
|
||||||
"""
|
"""
|
||||||
returns a list of all related place reservations
|
returns a list of all related place reservations
|
||||||
"""
|
"""
|
||||||
|
synchronize_calendars()
|
||||||
booked_dates = Reservation.objects.all()
|
booked_dates = Reservation.objects.all()
|
||||||
bookings = [
|
bookings = [
|
||||||
booking for booking in booked_dates if booking.place.name == place_name]
|
booking for booking in booked_dates if booking.place.name == place_name]
|
||||||
|
|
|
||||||
|
|
@ -121,10 +121,10 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"model": "sessions.session",
|
"model": "sessions.session",
|
||||||
"pk": "pe2dvr7dfkqagrcs2trec5aj7ep57rvz",
|
"pk": "v9w10z63mcilrzwa1asqd6fq4sdhoula",
|
||||||
"fields": {
|
"fields": {
|
||||||
"session_data": "OTc5Y2U3Zjc1YWQzOTAxNjA0MjQ1NTI3MDk0NTkxYjlmM2YyNDMxNDp7Il9hdXRoX3VzZXJfaWQiOiIyIiwiX2F1dGhfdXNlcl9iYWNrZW5kIjoiZGphbmdvLmNvbnRyaWIuYXV0aC5iYWNrZW5kcy5Nb2RlbEJhY2tlbmQiLCJfYXV0aF91c2VyX2hhc2giOiJjMjE1MTliMGU3ODIwYTkwMjIzNjIwZjE3MWU0MmM1ZGFhZGE3YjBiIn0=",
|
"session_data": "NzhmMTE3YWExMjExY2FjZDI4NDI4MmQxNDI1MjQwYzViZjczM2Q3Mjp7Il9hdXRoX3VzZXJfaWQiOiIyIiwiX2F1dGhfdXNlcl9iYWNrZW5kIjoiZGphbmdvLmNvbnRyaWIuYXV0aC5iYWNrZW5kcy5Nb2RlbEJhY2tlbmQiLCJfYXV0aF91c2VyX2hhc2giOiI1NTkwY2NlZjM1YzFhNDhiNjE4ZGJiNWI3YjZkZWUwZjFmYjU0NjYyIn0=",
|
||||||
"expire_date": "2019-12-18T13:11:01.964Z"
|
"expire_date": "2019-12-23T17:27:03.312Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -221,6 +221,7 @@
|
||||||
"beds": 1,
|
"beds": 1,
|
||||||
"max_occupation": 2,
|
"max_occupation": 2,
|
||||||
"thumbnail": 1,
|
"thumbnail": 1,
|
||||||
|
"calendar": "https://calendar.google.com/calendar/embed?height=600&wkst=2&bgcolor=%23ffffff&ctz=America%2FMartinique&src=YnVyaWs3YWNsdmhjN3ZzYm9oMDZjMTc5dW9AZ3JvdXAuY2FsZW5kYXIuZ29vZ2xlLmNvbQ&color=%23DD4477&showTitle=0&showCalendars=0&showTabs=1&showPrint=0&showDate=1&showNav=1",
|
||||||
"images": [
|
"images": [
|
||||||
7,
|
7,
|
||||||
6,
|
6,
|
||||||
|
|
@ -246,6 +247,7 @@
|
||||||
"beds": 2,
|
"beds": 2,
|
||||||
"max_occupation": 4,
|
"max_occupation": 4,
|
||||||
"thumbnail": 10,
|
"thumbnail": 10,
|
||||||
|
"calendar": "https://calendar.google.com/calendar/embed?height=600&wkst=2&bgcolor=%23ffffff&ctz=America%2FMartinique&showTitle=0&showCalendars=0&showTabs=1&showPrint=0&showDate=1&showNav=1&src=ZnU3aDMwcDBnazRhMnA0bnZvN25zYmdwb2tAZ3JvdXAuY2FsZW5kYXIuZ29vZ2xlLmNvbQ&color=%233366CC",
|
||||||
"images": [
|
"images": [
|
||||||
7,
|
7,
|
||||||
6,
|
6,
|
||||||
|
|
@ -760,15 +762,23 @@
|
||||||
"codename": "view_image"
|
"codename": "view_image"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"model": "auth.group",
|
||||||
|
"pk": 1,
|
||||||
|
"fields": {
|
||||||
|
"name": "Clients",
|
||||||
|
"permissions": []
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"model": "auth.user",
|
"model": "auth.user",
|
||||||
"pk": 1,
|
"pk": 1,
|
||||||
"fields": {
|
"fields": {
|
||||||
"password": "pbkdf2_sha256$150000$FUvMBlxIZy6y$HmwSAxQAdPaOve3XwCYfDx/3Oklh2WtG2QJewPcrO+s=",
|
"password": "pbkdf2_sha256$150000$FUvMBlxIZy6y$HmwSAxQAdPaOve3XwCYfDx/3Oklh2WtG2QJewPcrO+s=",
|
||||||
"last_login": "2019-12-03T12:22:52.850Z",
|
"last_login": "2019-12-03T12:22:52Z",
|
||||||
"is_superuser": true,
|
"is_superuser": true,
|
||||||
"username": "admin",
|
"username": "admin",
|
||||||
"first_name": "Ruidy",
|
"first_name": "Nilka",
|
||||||
"last_name": "Nemausat",
|
"last_name": "Nemausat",
|
||||||
"email": "location.villafleurie@gmail.com",
|
"email": "location.villafleurie@gmail.com",
|
||||||
"is_staff": true,
|
"is_staff": true,
|
||||||
|
|
@ -780,6 +790,10 @@
|
||||||
26,
|
26,
|
||||||
27,
|
27,
|
||||||
28,
|
28,
|
||||||
|
49,
|
||||||
|
50,
|
||||||
|
51,
|
||||||
|
52,
|
||||||
29,
|
29,
|
||||||
30,
|
30,
|
||||||
31,
|
31,
|
||||||
|
|
@ -799,18 +813,71 @@
|
||||||
"model": "auth.user",
|
"model": "auth.user",
|
||||||
"pk": 2,
|
"pk": 2,
|
||||||
"fields": {
|
"fields": {
|
||||||
"password": "pbkdf2_sha256$150000$Bm05RbDiL5KK$ok0/kUpbommwOCOo6TeY4De7mVSrOnONdmdN2fvWFV4=",
|
"password": "pbkdf2_sha256$180000$0vN9vhsYeNft$6ema2p7l0l6muwDnPcpNO6dxJQc4qG7hIoyvzcBBAXc=",
|
||||||
"last_login": "2019-12-04T13:11:01.941Z",
|
"last_login": "2019-12-09T17:27:30Z",
|
||||||
"is_superuser": true,
|
"is_superuser": true,
|
||||||
"username": "ruidy",
|
"username": "ruidy",
|
||||||
"first_name": "",
|
"first_name": "Ruidy",
|
||||||
"last_name": "",
|
"last_name": "NEMAUSAT",
|
||||||
"email": "ruidy.nemausat@gmail.com",
|
"email": "ruidy.nemausat@gmail.com",
|
||||||
"is_staff": true,
|
"is_staff": true,
|
||||||
"is_active": true,
|
"is_active": true,
|
||||||
"date_joined": "2019-12-04T13:10:55.470Z",
|
"date_joined": "2019-12-04T13:10:55Z",
|
||||||
"groups": [],
|
"groups": [],
|
||||||
"user_permissions": []
|
"user_permissions": [
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
3,
|
||||||
|
4,
|
||||||
|
9,
|
||||||
|
10,
|
||||||
|
11,
|
||||||
|
12,
|
||||||
|
5,
|
||||||
|
6,
|
||||||
|
7,
|
||||||
|
8,
|
||||||
|
13,
|
||||||
|
14,
|
||||||
|
15,
|
||||||
|
16,
|
||||||
|
17,
|
||||||
|
18,
|
||||||
|
19,
|
||||||
|
20,
|
||||||
|
45,
|
||||||
|
46,
|
||||||
|
47,
|
||||||
|
48,
|
||||||
|
25,
|
||||||
|
26,
|
||||||
|
27,
|
||||||
|
28,
|
||||||
|
49,
|
||||||
|
50,
|
||||||
|
51,
|
||||||
|
52,
|
||||||
|
29,
|
||||||
|
30,
|
||||||
|
31,
|
||||||
|
32,
|
||||||
|
33,
|
||||||
|
34,
|
||||||
|
35,
|
||||||
|
36,
|
||||||
|
37,
|
||||||
|
38,
|
||||||
|
39,
|
||||||
|
40,
|
||||||
|
21,
|
||||||
|
22,
|
||||||
|
23,
|
||||||
|
24,
|
||||||
|
41,
|
||||||
|
42,
|
||||||
|
43,
|
||||||
|
44
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -2112,5 +2179,161 @@
|
||||||
"action_flag": 3,
|
"action_flag": 3,
|
||||||
"change_message": ""
|
"change_message": ""
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "admin.logentry",
|
||||||
|
"pk": 101,
|
||||||
|
"fields": {
|
||||||
|
"action_time": "2019-12-06T12:17:40.493Z",
|
||||||
|
"user": 2,
|
||||||
|
"content_type": 8,
|
||||||
|
"object_id": "1",
|
||||||
|
"object_repr": "T2",
|
||||||
|
"action_flag": 2,
|
||||||
|
"change_message": "[{\"changed\": {\"fields\": [\"Calendar\"]}}]"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "admin.logentry",
|
||||||
|
"pk": 102,
|
||||||
|
"fields": {
|
||||||
|
"action_time": "2019-12-06T12:18:15.553Z",
|
||||||
|
"user": 2,
|
||||||
|
"content_type": 8,
|
||||||
|
"object_id": "2",
|
||||||
|
"object_repr": "T3",
|
||||||
|
"action_flag": 2,
|
||||||
|
"change_message": "[{\"changed\": {\"fields\": [\"Calendar\"]}}]"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "admin.logentry",
|
||||||
|
"pk": 103,
|
||||||
|
"fields": {
|
||||||
|
"action_time": "2019-12-06T12:19:44.349Z",
|
||||||
|
"user": 2,
|
||||||
|
"content_type": 8,
|
||||||
|
"object_id": "2",
|
||||||
|
"object_repr": "T3",
|
||||||
|
"action_flag": 2,
|
||||||
|
"change_message": "[]"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "admin.logentry",
|
||||||
|
"pk": 104,
|
||||||
|
"fields": {
|
||||||
|
"action_time": "2019-12-06T12:27:10.747Z",
|
||||||
|
"user": 2,
|
||||||
|
"content_type": 8,
|
||||||
|
"object_id": "1",
|
||||||
|
"object_repr": "T2",
|
||||||
|
"action_flag": 2,
|
||||||
|
"change_message": "[]"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "admin.logentry",
|
||||||
|
"pk": 105,
|
||||||
|
"fields": {
|
||||||
|
"action_time": "2019-12-06T12:27:35.355Z",
|
||||||
|
"user": 2,
|
||||||
|
"content_type": 8,
|
||||||
|
"object_id": "2",
|
||||||
|
"object_repr": "T3",
|
||||||
|
"action_flag": 2,
|
||||||
|
"change_message": "[]"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "admin.logentry",
|
||||||
|
"pk": 106,
|
||||||
|
"fields": {
|
||||||
|
"action_time": "2019-12-09T17:27:42.217Z",
|
||||||
|
"user": 2,
|
||||||
|
"content_type": 4,
|
||||||
|
"object_id": "2",
|
||||||
|
"object_repr": "ruidy",
|
||||||
|
"action_flag": 2,
|
||||||
|
"change_message": "[{\"changed\": {\"fields\": [\"First name\", \"Last name\", \"User permissions\", \"Last login\"]}}]"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "admin.logentry",
|
||||||
|
"pk": 107,
|
||||||
|
"fields": {
|
||||||
|
"action_time": "2019-12-09T17:28:38.006Z",
|
||||||
|
"user": 2,
|
||||||
|
"content_type": 4,
|
||||||
|
"object_id": "1",
|
||||||
|
"object_repr": "admin",
|
||||||
|
"action_flag": 2,
|
||||||
|
"change_message": "[{\"changed\": {\"fields\": [\"First name\", \"User permissions\"]}}]"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "admin.logentry",
|
||||||
|
"pk": 108,
|
||||||
|
"fields": {
|
||||||
|
"action_time": "2019-12-09T17:29:31.434Z",
|
||||||
|
"user": 2,
|
||||||
|
"content_type": 3,
|
||||||
|
"object_id": "1",
|
||||||
|
"object_repr": "Clients",
|
||||||
|
"action_flag": 1,
|
||||||
|
"change_message": "[{\"added\": {}}]"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "admin.logentry",
|
||||||
|
"pk": 109,
|
||||||
|
"fields": {
|
||||||
|
"action_time": "2019-12-09T17:30:49.765Z",
|
||||||
|
"user": 2,
|
||||||
|
"content_type": 7,
|
||||||
|
"object_id": "40",
|
||||||
|
"object_repr": "Thomas Price",
|
||||||
|
"action_flag": 3,
|
||||||
|
"change_message": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "admin.logentry",
|
||||||
|
"pk": 110,
|
||||||
|
"fields": {
|
||||||
|
"action_time": "2019-12-09T17:30:49.768Z",
|
||||||
|
"user": 2,
|
||||||
|
"content_type": 7,
|
||||||
|
"object_id": "39",
|
||||||
|
"object_repr": "John Doe",
|
||||||
|
"action_flag": 3,
|
||||||
|
"change_message": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "admin.logentry",
|
||||||
|
"pk": 111,
|
||||||
|
"fields": {
|
||||||
|
"action_time": "2019-12-09T17:30:49.769Z",
|
||||||
|
"user": 2,
|
||||||
|
"content_type": 7,
|
||||||
|
"object_id": "38",
|
||||||
|
"object_repr": "Nilka",
|
||||||
|
"action_flag": 3,
|
||||||
|
"change_message": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "admin.logentry",
|
||||||
|
"pk": 112,
|
||||||
|
"fields": {
|
||||||
|
"action_time": "2019-12-09T17:30:49.771Z",
|
||||||
|
"user": 2,
|
||||||
|
"content_type": 7,
|
||||||
|
"object_id": "37",
|
||||||
|
"object_repr": "NEMAUSAT Ruidy",
|
||||||
|
"action_flag": 3,
|
||||||
|
"change_message": ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
Loading…
Reference in a new issue