2021-01-17 17:10:04 +01:00
|
|
|
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
|
2021-02-06 12:31:59 +01:00
|
|
|
from django import forms
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.template.defaultfilters import slugify
|
|
|
|
from django.urls import reverse
|
|
|
|
|
2021-01-17 17:10:04 +01:00
|
|
|
|
|
|
|
STATUS = (
|
|
|
|
('Confirmed',_('Rezerwacja potwierdzona')),
|
|
|
|
('Requsted',_('Oczekuję na potwierdzenie')),
|
|
|
|
('Cancelled',_('Anulowana')),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Reservation(models.Model):
|
2021-02-06 12:31:59 +01:00
|
|
|
id = models.AutoField(primary_key= True)
|
2021-01-17 17:10:04 +01:00
|
|
|
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)
|
2021-02-06 12:31:59 +01:00
|
|
|
created_date = models.DateTimeField(auto_now_add=True)
|
2021-01-17 17:10:04 +01:00
|
|
|
reservation_start_date = models.DateField(default = timezone.now,)
|
|
|
|
reservation_end_date = models.DateField()
|
2021-02-06 12:31:59 +01:00
|
|
|
reservation_cost = models.DecimalField(max_digits=12,default=0,decimal_places=2)
|
|
|
|
message_to_owner = models.TextField(max_length=500, blank = True, null = True)
|
2021-01-17 17:10:04 +01:00
|
|
|
status = models.CharField(choices = STATUS, max_length = 50)
|
2021-02-06 12:31:59 +01:00
|
|
|
slug = models.SlugField(max_length=255,editable = False, default='', unique = False)
|
2021-01-17 17:10:04 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s (%s to %s)" % (self.user.email,
|
|
|
|
self.reservation_start_date.strftime(
|
|
|
|
"%Y/%m/%d"),
|
|
|
|
self.reservation_end_date.strftime(
|
2021-02-06 12:31:59 +01:00
|
|
|
"%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})
|
|
|
|
|
2021-01-17 17:10:04 +01:00
|
|
|
|
2021-02-06 12:31:59 +01:00
|
|
|
def save(self,*args,**kwargs):
|
2021-01-17 17:10:04 +01:00
|
|
|
errors = {}
|
2021-02-06 12:31:59 +01:00
|
|
|
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):
|
2021-01-17 17:10:04 +01:00
|
|
|
errors[NON_FIELD_ERRORS] = "Kamper w tym okresie jest juz zarezerwowany."
|
2021-02-06 12:31:59 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
|