Project_Camper/vagus/reservation/models.py
2021-02-06 12:31:59 +01:00

76 lines
3.4 KiB
Python
Executable File

from django.db import models
from django.utils.translation import gettext_lazy as _
from django.utils import timezone
from django.utils.translation import ugettext as _
from django.core.exceptions import ValidationError
from django.conf import settings
from django.core.exceptions import NON_FIELD_ERRORS
from django.db.models import Q
from django import forms
from django.contrib import messages
from django.template.defaultfilters import slugify
from django.urls import reverse
STATUS = (
('Confirmed',_('Rezerwacja potwierdzona')),
('Requsted',_('Oczekuję na potwierdzenie')),
('Cancelled',_('Anulowana')),
)
class Reservation(models.Model):
id = models.AutoField(primary_key= True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_("User"), on_delete=models.CASCADE)
offer = models.ForeignKey("camper.Offer", verbose_name=_("Offer"), on_delete=models.CASCADE)
created_date = models.DateTimeField(auto_now_add=True)
reservation_start_date = models.DateField(default = timezone.now,)
reservation_end_date = models.DateField()
reservation_cost = models.DecimalField(max_digits=12,default=0,decimal_places=2)
message_to_owner = models.TextField(max_length=500, blank = True, null = True)
status = models.CharField(choices = STATUS, max_length = 50)
slug = models.SlugField(max_length=255,editable = False, default='', unique = False)
def __str__(self):
return "%s (%s to %s)" % (self.user.email,
self.reservation_start_date.strftime(
"%Y/%m/%d"),
self.reservation_end_date.strftime(
"%Y/%m/%d"),)
@property
def get_total_cost(self):
diff_days = self.reservation_end_date - self.reservation_start_date
self.reservation_cost = diff_days.days * self.offer.price
return self.reservation_cost
def clean(self,*args,**kwargs):
errors = {}
today_date = timezone.now().date()
if self.created_date != None:
if self.reservation_start_date < today_date:
errors['reservation_start_date'] = "Rezerwacja nie moze rozpoczynać sie w przeszłości"
if self.reservation_end_date < self.reservation_start_date:
errors['reservation_end_date'] = "Data zakończenia wynajmu nie moze byc data wczesniejsza / równa od daty rozpoczecia."
if self.reservation_end_date == self.reservation_start_date:
errors['reservation_end_date'] = "Wynajem nie moze kończyc sie w dniu rozpoczecia wynajmu."
if errors:
raise forms.ValidationError(errors)
def get_absolute_url(self):
return reverse("reservation_detail", kwargs={"slug": self.slug})
def save(self,*args,**kwargs):
errors = {}
filter_params = dict(reservation_end_date__lte=self.reservation_end_date, reservation_start_date__gte=self.reservation_start_date,offer =self.offer)
if Reservation.objects.filter(**filter_params):
errors[NON_FIELD_ERRORS] = "Kamper w tym okresie jest juz zarezerwowany."
if errors:
raise forms.ValidationError(errors)
else:
self.slug = slugify(f'{self.offer.title}-{self.id}-/{self.reservation_start_date}-{self.reservation_end_date}')
return super(Reservation,self).save(*args,**kwargs)