JourneyJoker/partners/models.py

102 lines
3.7 KiB
Python
Raw Normal View History

from django.contrib.gis.db import models
2021-10-28 07:26:17 +00:00
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
2021-03-01 09:56:10 +00:00
2021-04-18 06:12:15 +00:00
from .features import *
from localauth.models import User, Profile, LocationMixin, ImageMixin, PhoneMixin
from gallery.mixins import GalleryMixin
2021-03-22 17:42:07 +00:00
from django_countries.fields import CountryField
class PartnerProfile(Profile):
2021-04-18 06:12:15 +00:00
@property
def roomcategory_set(self):
return RoomCategory.objects.filter(establishment__in=self.establishment_set.all())
2021-03-22 17:42:07 +00:00
class Establishment(LocationMixin, ImageMixin, PhoneMixin, GalleryMixin):
2021-03-22 17:42:07 +00:00
owner = models.ForeignKey(PartnerProfile, models.CASCADE)
2021-10-28 07:26:17 +00:00
name = models.CharField(max_length=64)
stars = models.IntegerField("Sterne", null=True, blank=True)
2021-10-28 07:26:17 +00:00
superior = models.BooleanField(default=False)
2021-04-09 05:18:21 +00:00
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
2021-04-18 06:12:15 +00:00
@property
def booking_set(self):
return self.offer_set.filter(accepted=True)
2021-03-22 17:42:07 +00:00
@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):
2021-03-22 17:42:07 +00:00
establishment = models.ForeignKey(Establishment, models.CASCADE)
2021-10-28 07:26:17 +00:00
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"])