villafleurie/rental/models/booking.py
2020-06-13 18:12:10 +02:00

52 lines
1.7 KiB
Python

from datetime import datetime
from django.db import models
from rental.models.guest import Guest
from rental.models.place import Place
import rental.services.calendar as calendar
import rental.tasks.apiMailer as mailer # or gMailer
class BookingManager(models.Manager):
""" BookingManager is the interface through which database query operations
are provided to Django models.
"""
def create_booking(self, **kwargs):
""" create_booking creates a Booking instance, computes the price and
updates the remote calendar. """
booking = self.create(**kwargs)
booking.price = booking.get_price()
calendar.update(booking)
return booking
class Booking(models.Model):
class Meta:
verbose_name = 'Réservation'
def __str__(self):
return "Réservation du {} par {}".format(self.place, self.guest)
objects = BookingManager()
place = models.ForeignKey(Place, on_delete=models.CASCADE)
guest = models.ForeignKey(Guest, on_delete=models.CASCADE)
message = models.TextField(blank=True)
start = models.DateField()
end = models.DateField()
price = models.DecimalField(max_digits=6, decimal_places=2, null=True)
def get_price(self):
""" Compute booking price as a function of place and dates """
if type(self.start) == str:
self.start = datetime.strptime(self.start, '%Y-%m-%d')
if type(self.end) == str:
self.end = datetime.strptime(self.end, '%Y-%m-%d')
nights = (self.end - self.start).days
return self.place.price * nights
def send_quotation(self):
mailer.send_quotation.delay(self.guest.name, self.guest.email)