from django.db import models from django.utils.timezone import now from .data import TYPES class Day(models.Model): date = models.DateField(null=True, blank=True) def __str__(self): return str(self.date) class Location(models.Model): 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 self.name class Menu(models.Model): art = models.CharField(max_length=144, null=True, blank=True) name = models.CharField(max_length=144, null=True, blank=True) price = models.CharField(max_length=144, null=True, blank=True) allergens = models.JSONField(null=True, blank=True) types = models.JSONField(null=True, blank=True) likes = models.IntegerField(default=0) locations = models.ManyToManyField(Location) days = models.ManyToManyField(Day) def __str__(self): return self.name @property def get_price(self): return { 'Student': self.price.split(' / ')[0] or 0, 'Employee': self.price.split(' / ')[1] or 0, 'Guest': self.price.split(' / ')[2] or 0, } @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(related_menu=self)] class Comment(models.Model): related_menu = models.ForeignKey(Menu, on_delete=models.CASCADE) timestamp = models.DateTimeField(default=now, blank=True) username = models.CharField(max_length=144, default='anon') comment = models.CharField(max_length=720)