From fed5732204eca86c793490828803ce523f7bbf10 Mon Sep 17 00:00:00 2001 From: Ruidy Nemausat Date: Wed, 8 Apr 2020 10:48:52 +0200 Subject: [PATCH] add send_confirmation and send_notificaiton methods to contact; add send_quotation method to booking; inject services in models and decoupled views --- rental/models/booking.py | 4 ++++ rental/models/contact.py | 14 +++++++++++++- rental/templates/rental/base.html | 18 +++++++++--------- rental/views.py | 17 ++++------------- 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/rental/models/booking.py b/rental/models/booking.py index e8c7451..b935846 100644 --- a/rental/models/booking.py +++ b/rental/models/booking.py @@ -3,6 +3,7 @@ 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): @@ -45,3 +46,6 @@ class Booking(models.Model): 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) diff --git a/rental/models/contact.py b/rental/models/contact.py index fb8ca79..0184300 100644 --- a/rental/models/contact.py +++ b/rental/models/contact.py @@ -1,5 +1,5 @@ from django.db import models - +import rental.tasks.apiMailer as mailer # or gMailer class Contact(models.Model): @@ -12,3 +12,15 @@ class Contact(models.Model): subject = models.EmailField(max_length=50) message = models.TextField(max_length=50) date = models.DateTimeField(auto_now_add=True) + + def send_confirmation(self): + mailer.send_confirmation.delay(self.name, self.email) + + def send_notification(self): + mailer.send_notification.delay( + self.name, + self.email, + self.subject, + self.message, + self.date + ) diff --git a/rental/templates/rental/base.html b/rental/templates/rental/base.html index 3b4c5a6..781818f 100644 --- a/rental/templates/rental/base.html +++ b/rental/templates/rental/base.html @@ -23,7 +23,7 @@ - VillaFleurie + VillaFleurie – Locations de vacances diff --git a/rental/views.py b/rental/views.py index dbdfef3..42d30d9 100644 --- a/rental/views.py +++ b/rental/views.py @@ -7,11 +7,8 @@ from rental.forms import ReservationForm, ContactForm 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 rental.services.calendar import check_availability, synchronize_calendars, update -from rental.tasks.apiMailer import * # or gMailer def index(request): @@ -79,7 +76,6 @@ def handle_reservation_form(request, context={}, init_template='rental/reservati guest = guest.first() place = get_object_or_404(Place, name=place_name) - # available = check_availability(place, ) if place.is_available(start, end): reservation = Booking.objects.create_booking( @@ -89,7 +85,8 @@ def handle_reservation_form(request, context={}, init_template='rental/reservati start=start, end=end ) - send_quotation.delay(name, email) + + reservation.send_quotation() context = { 'reservation': reservation @@ -144,14 +141,8 @@ def contact(request): message=form.cleaned_data['message'] ) - send_confirmation.delay(contact.name, contact.email) - send_notification.delay( - contact.name, - contact.email, - contact.subject, - contact.message, - contact.date - ) + contact.send_confirmation() + contact.send_notification() return render(request, 'rental/contact_merci.html', {})