You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.8 KiB
Python
85 lines
2.8 KiB
Python
from django.db import models
|
|
from django.utils.timezone import now
|
|
|
|
from .data import TYPES, ART_CHOICES
|
|
|
|
|
|
class Location(models.Model):
|
|
city = models.CharField(max_length=144, null=True, blank=True)
|
|
name = models.CharField(max_length=144, null=True, blank=True)
|
|
mensa_id = models.CharField(max_length=2, null=True, blank=True)
|
|
|
|
def __str__(self):
|
|
return f"{self.city} - {self.name}"
|
|
|
|
@property
|
|
def menus(self):
|
|
return [menu for menu in Menu.objects.filter(location=self)]
|
|
|
|
@property
|
|
def news_articles(self):
|
|
return [news_article for news_article in NewsArticle.objects.filter(location=self)]
|
|
|
|
|
|
class Menu(models.Model):
|
|
art = models.CharField(max_length=24, choices=ART_CHOICES, default='M')
|
|
name = models.CharField(max_length=144, default='---', blank=True)
|
|
price = models.CharField(max_length=144, default='---', blank=True)
|
|
allergens = models.JSONField(default=list, blank=True)
|
|
types = models.JSONField(default=list, blank=True)
|
|
day = models.DateField(null=True, blank=True)
|
|
likes = models.IntegerField(default=0)
|
|
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True,
|
|
blank=True)
|
|
|
|
def __str__(self):
|
|
return f"{self.name} - {self.location} | {self.day}"
|
|
|
|
@property
|
|
def get_price(self):
|
|
try:
|
|
return {
|
|
'Student': self.price.split(' / ')[0],
|
|
'Employee': self.price.split(' / ')[1],
|
|
'Guest': self.price.split(' / ')[2],
|
|
}
|
|
except IndexError:
|
|
return {
|
|
'Student': "0,00 €",
|
|
'Employee': "0,00 €",
|
|
'Guest': "0,00 €",
|
|
}
|
|
|
|
@property
|
|
def get_types(self):
|
|
return ', '.join([TYPES[type] for type in self.types])
|
|
|
|
@property
|
|
def get_allergens(self):
|
|
return ', '.join([allergen for allergen in self.allergens])
|
|
|
|
@property
|
|
def comments(self):
|
|
return [comment for comment in Comment.objects.filter(menu=self)]
|
|
|
|
|
|
class Comment(models.Model):
|
|
menu = models.ForeignKey(Menu, on_delete=models.CASCADE)
|
|
timestamp = models.DateTimeField(default=now, blank=True)
|
|
author = models.CharField(max_length=144, default='anon')
|
|
text = models.TextField(null=True, blank=True)
|
|
|
|
def __str__(self):
|
|
return f"{self.menu} - {self.author} | {self.timestamp}"
|
|
|
|
|
|
class NewsArticle(models.Model):
|
|
location = models.ForeignKey(Location, on_delete=models.CASCADE)
|
|
timestamp = models.DateTimeField(default=now, blank=True)
|
|
author = models.CharField(max_length=144, default='anon')
|
|
title = models.CharField(max_length=144, blank=True)
|
|
text = models.TextField(null=True, blank=True)
|
|
|
|
def __str__(self):
|
|
return f"{self.title} | {self.title} - {self.author} | {self.timestamp}"
|