from django.contrib.gis.db import models from django.utils import timezone from django.utils.translation import gettext_lazy as _ from .features import * from localauth.models import User, Profile, LocationMixin, ImageMixin, PhoneMixin from gallery.mixins import GalleryMixin from django_countries.fields import CountryField class PartnerProfile(Profile): @property def roomcategory_set(self): return RoomCategory.objects.filter(establishment__in=self.establishment_set.all()) class Establishment(LocationMixin, ImageMixin, PhoneMixin, GalleryMixin): owner = models.ForeignKey(PartnerProfile, models.CASCADE) name = models.CharField(max_length=64) stars = models.IntegerField("Sterne", null=True, blank=True) superior = models.BooleanField(default=False) verified = models.BooleanField(default=False) active = models.BooleanField(default=True) featureset = models.OneToOneField(FeatureSet, models.PROTECT, null=True, blank=True) @property def user(self): return self.owner.user @property def is_active(self): return self.verified and self.active @property def booking_set(self): return self.offer_set.filter(accepted=True) @property def offer_set(self): querysets = [] for roomcategory in self.roomcategory_set.all(): querysets.append(roomcategory.offer_set.all()) return querysets[0].union(*querysets[1:]) class RoomCategory(GalleryMixin): establishment = models.ForeignKey(Establishment, models.CASCADE) name = models.CharField(max_length=64) rooms = models.IntegerField(default=0) active = models.BooleanField(default=True) def average_price(self, date=None): dobj = timezone.datetime.strptime(date, "%Y-%m-%d") if date else timezone.now() return RoomCategoryPricing.for_date(self, dobj).average_price def minimum_price(self, date=None): dobj = timezone.datetime.strptime(date, "%Y-%m-%d") if date else timezone.now() return RoomCategoryPricing.for_date(self, dobj).minimum_price class RoomCategoryPricing(models.Model): roomcategory = models.ForeignKey(RoomCategory, models.CASCADE) date = models.DateField() average_price = models.DecimalField(max_digits=12, decimal_places=2) minimum_price = models.DecimalField(max_digits=12, decimal_places=2) class Meta: unique_together = [("roomcategory", "date")] @classmethod def for_date(cls, category: RoomCategory, date: timezone.datetime): try: pricing = cls.objects.get(roomcategory=category, date=date) return (pricing.average_price, pricing.minimum_price) except cls.DoesNotExist: return RoomCategoryDefaultPricing.for_date(category, date) class RoomCategoryDefaultPricing(models.Model): class WeekdayChoices(models.IntegerChoices): MONDAY = (0, _("Montag")) TUESDAY = (1, _("Dienstag")) WEDNESDAY = (2, _("Mittwoch")) THURSDAY = (3, _("Donnerstag")) FRIDAY = (4, _("Freitag")) SATURDAY = (5, _("Samstag")) SUNDAY = (6, _("Sonntag")) roomcategory = models.OneToOneField(RoomCategory, models.CASCADE) start_date = models.DateField() end_date = models.DateField(null=True, blank=True) weekday = models.IntegerField(choices=WeekdayChoices.choices) average_price = models.DecimalField(max_digits=12, decimal_places=2) minimum_price = models.DecimalField(max_digits=12, decimal_places=2) @classmethod def for_date(cls, category: RoomCategory, date: timezone.datetime): pricing = cls.objects.get(roomcategory=category) return (pricing[date.weekday()]["average"], pricing[date.weekday()]["minimum"])