From 7893cbcb6f8f3874e7dd07ea346951b506af2be7 Mon Sep 17 00:00:00 2001 From: zb0xa1 Date: Sun, 8 Jan 2023 14:17:42 +0100 Subject: [PATCH] Added data, forms, and models. --- mensaviewer/data.py | 30 +++++++++++++++++++ mensaviewer/forms.py | 12 ++++++-- mensaviewer/models.py | 70 +++++++++++++++++++++++++++---------------- 3 files changed, 83 insertions(+), 29 deletions(-) diff --git a/mensaviewer/data.py b/mensaviewer/data.py index aefcf23..551b8d5 100644 --- a/mensaviewer/data.py +++ b/mensaviewer/data.py @@ -1,4 +1,31 @@ STATUS_VALUES = ['Student', 'Employee', 'Guest'] +ALLERGENS = { + 'Ei': 'Ei', + 'En': 'Erdnüsse', + 'Fi': 'Fisch', + 'Gl': 'Glutenhaltiges Getreide', + 'GlD': 'Dinkel', + 'GlG': 'Gerste', + 'GlH': 'Hafer', + 'GlR': 'Roggen', + 'GlW': 'Weizen', + 'Kr': 'Krebstiere', + 'Lac': 'Laktose', + 'Lu': 'Lupinen', + 'Mi': 'Milch', + 'Sc': 'Schalenfrüchte', + 'ScC': 'Cashews', + 'ScH': 'Haselnüsse', + 'ScM': 'Mandeln', + 'ScP': 'Pistazien', + 'ScW': 'Walnüsse', + 'Se': 'Sesam', + 'Sf': 'Senf', + 'Sl': 'Sellerie', + 'So': 'Soja', + 'Sw': 'Schwefeldioxid / Sulphite', + 'Wt': 'Weichtiere', +} TYPES = { 'vn': 'Vegan', 've': 'Vegetarisch', @@ -14,3 +41,6 @@ TYPES = { 'AGG': 'Artgerechtes Geflügel', 'AGL': 'Artgerechtes Lamm', } +ALLERGENS_CHOICES = ((key, ALLERGENS[key]) for key in ALLERGENS) +TYPES_CHOICES = ((key, TYPES[key]) for key in TYPES) +ART_CHOICES = (('Mensa', 'Mensa'), ('Cafeteria', 'Cafeteria')) diff --git a/mensaviewer/forms.py b/mensaviewer/forms.py index cab3579..ffdaedd 100644 --- a/mensaviewer/forms.py +++ b/mensaviewer/forms.py @@ -1,11 +1,17 @@ from django import forms from .models import * +from .data import TYPES_CHOICES, ALLERGENS_CHOICES from datetime import date -class CommentForm(forms.ModelForm): +class MenuForm(forms.ModelForm): class Meta: - model = Comment - fields = '__all__' + model = Menu + fields = ['art', 'name', 'price', 'allergens', 'types', 'day'] + widgets = { + 'types': forms.CheckboxSelectMultiple(choices=TYPES_CHOICES), + 'allergens': forms.CheckboxSelectMultiple(choices=ALLERGENS_CHOICES), + 'day': forms.DateInput(attrs={'type': 'date'}, format="%d.%m.%Y"), + } diff --git a/mensaviewer/models.py b/mensaviewer/models.py index a5679c1..ffe945b 100644 --- a/mensaviewer/models.py +++ b/mensaviewer/models.py @@ -1,44 +1,54 @@ 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) +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 self.name + return f"{self.city} - {self.name} [{self.mensa_id}]" + + @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=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) + 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) - locations = models.ManyToManyField(Location) - days = models.ManyToManyField(Day) + location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, + blank=True) def __str__(self): - return self.name + return f"{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, - } + 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): @@ -50,11 +60,19 @@ class Menu(models.Model): @property def comments(self): - return [comment for comment in Comment.objects.filter(related_menu=self)] + return [comment for comment in Comment.objects.filter(menu=self)] class Comment(models.Model): - related_menu = models.ForeignKey(Menu, on_delete=models.CASCADE) + 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) + + +class NewsArticle(models.Model): + location = models.ForeignKey(Location, 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) + author = models.CharField(max_length=144, default='anon') + title = models.CharField(max_length=144, blank=True) + text = models.TextField(null=True, blank=True)