Advertisement
barabar4e

Untitled

Nov 20th, 2023
886
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. class Hotel(models.Model):
  2.     name = models.CharField(
  3.         max_length=100,
  4.     )
  5.  
  6.     address = models.CharField(
  7.         max_length=200,
  8.     )
  9.  
  10.  
  11. class Room(models.Model):
  12.     hotel = models.ForeignKey(
  13.         to=Hotel,
  14.         on_delete=models.CASCADE,
  15.     )
  16.  
  17.     number = models.CharField(
  18.         max_length=100,
  19.         unique=True,
  20.     )
  21.  
  22.     capacity = models.PositiveIntegerField()
  23.  
  24.     total_guests = models.PositiveIntegerField()
  25.  
  26.     price_per_night = models.DecimalField(
  27.         max_digits=10,
  28.         decimal_places=2,
  29.     )
  30.  
  31.     def clean(self):
  32.         if self.total_guests > self.capacity:
  33.             raise ValidationError("Total guests are more than the capacity of the room")
  34.  
  35.     def save(self, *args, **kwargs):
  36.         self.clean()
  37.  
  38.         super().save(*args, **kwargs)
  39.  
  40.         return f"Room {self.number} created successfully"
  41.  
  42.  
  43. class BaseReservation(models.Model):
  44.     room = models.ForeignKey(
  45.         to=Room,
  46.         on_delete=models.CASCADE,
  47.     )
  48.  
  49.     start_date = models.DateField()
  50.  
  51.     end_date = models.DateField()
  52.  
  53.     class Meta:
  54.         abstract = True
  55.  
  56.     def reservation_period(self):
  57.         return (self.end_date - self.start_date).days
  58.  
  59.     def calculate_total_cost(self):
  60.         total_cost = self.reservation_period() * Room.price_per_night
  61.  
  62.         return f"{total_cost:.2f}"
  63.  
  64.     @property
  65.     def is_available(self):
  66.         reservations = self.__class__.objects.filter(
  67.             room=self.room,
  68.             end_date__gte=self.start_date,
  69.             start_date__lte=self.end_date,
  70.         )
  71.  
  72.         return not reservations.exists()
  73.  
  74.     def clean(self):
  75.         if self.start_date >= self.end_date:
  76.             raise ValidationError("Start date cannot be after or in the same end date")
  77.  
  78.         if not self.is_available:
  79.             raise ValidationError(f"Room {self.room.number} cannot be reserved")
  80.  
  81.  
  82. class RegularReservation(BaseReservation):
  83.     def save(self, *args, **kwargs):
  84.         super().clean()
  85.  
  86.         super().save(*args, **kwargs)
  87.  
  88.         return f"Regular reservation for room {self.room.number}"
  89.  
  90.  
  91. class SpecialReservation(BaseReservation):
  92.  
  93.     def save(self, *args, **kwargs):
  94.         super().clean()
  95.  
  96.         super().save(*args, **kwargs)
  97.  
  98.         return f"Special reservation for room {self.room.number}"
  99.  
  100.     def extend_reservation(self, days: int):
  101.         reservations = SpecialReservation.objects.filter(
  102.             room=self.room,
  103.             end_date__gte=self.start_date,
  104.             start_date__lte=self.end_date + timedelta(days=days),
  105.         )
  106.  
  107.         if reservations:
  108.             raise ValidationError("Error during extending reservation")
  109.  
  110.         self.end_date += timedelta(days=days)
  111.  
  112.         self.save()
  113.  
  114.         return f"Extended reservation for room {self.room.number} with {days} days"
  115.  
Advertisement
Comments
  • mikace
    1 year
    Comment was deleted
Add Comment
Please, Sign In to add comment
Advertisement