diff --git a/mensaviewer/admin.py b/mensaviewer/admin.py index 4989254..38a0f6f 100644 --- a/mensaviewer/admin.py +++ b/mensaviewer/admin.py @@ -18,6 +18,6 @@ class LocationAdmin(admin.ModelAdmin): pass -@admin.register(Day) -class DayAdmin(admin.ModelAdmin): +@admin.register(NewsArticle) +class NewsArticleAdmin(admin.ModelAdmin): pass diff --git a/mensaviewer/forms.py b/mensaviewer/forms.py index ffdaedd..243ac0a 100644 --- a/mensaviewer/forms.py +++ b/mensaviewer/forms.py @@ -9,9 +9,7 @@ from datetime import date class MenuForm(forms.ModelForm): class Meta: model = Menu - fields = ['art', 'name', 'price', 'allergens', 'types', 'day'] + fields = ['location', '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"), + 'day': forms.DateInput(attrs={'type': 'date'}), } diff --git a/mensaviewer/helpers.py b/mensaviewer/helpers.py index f1e0569..4208655 100644 --- a/mensaviewer/helpers.py +++ b/mensaviewer/helpers.py @@ -20,46 +20,39 @@ example_json = [ def load_data(locations, checked_types, status): current_week = datetime.date.today().isocalendar().week - days = Day.objects.filter(date__week=current_week) - for location in locations: - menus = Menu.objects.filter(days__in=days, locations=location) + menus = Menu.objects.filter(day__week=current_week, location=location) if not menus.exists(): fetched_data = fetch(location.mensa_id) store(fetched_data, location) - data = formatted_menu_data(locations, checked_types, status) return data def formatted_menu_data(locations, checked_types, status): current_week = datetime.date.today().isocalendar().week - days = Day.objects.filter(date__week=current_week) + today = datetime.date.today() + days = [today + datetime.timedelta(days=i) + for i in range(0 - today.weekday(), 5 - today.weekday())] formatted_data = {} - - for day in days: - formatted_data[str(day.date)] = [] - for day in days: + if day not in formatted_data: + formatted_data[day] = [] for location in locations: - menus = Menu.objects.filter(days=day, locations=location) + menus = Menu.objects.filter(location=location, day=day) for menu in menus: - - # AND or OR filter ??? if set(checked_types).issubset(set(menu.types)): # <-- AND - - - - formatted_data[str(day.date)].append({ + formatted_data[day].append({ 'art': menu.art, 'name': menu.name, - 'price': menu.price if status is None else menu.get_price[status], + 'price': menu.price if status is None + else menu.get_price[status], 'allergens': menu.get_allergens, 'types': menu.get_types, 'likes': menu.likes, - 'location': location.name, + 'location': location, 'pk': menu.pk, }) return formatted_data @@ -67,32 +60,21 @@ def formatted_menu_data(locations, checked_types, status): def store(data, location): for day_json in data: - date_string= day_json['day'] + date_string = day_json['day'] menus_json = day_json['menus'] - - date = parse_date(date_string) - - if not Day.objects.filter(date=date).exists(): - Day(date=date).save() - - day = Day.objects.get(date=date) - + day = parse_date(date_string) for menu_json in menus_json: art = menu_json['art'] name = menu_json['name'] - price = menu_json['price'] or "0,00€ / 0,00€ / 0,00€" + price = menu_json['price'] or "0,00 € / 0,00 € / 0,00 €" allergens = menu_json['allergens'] types = menu_json['types'] - - menu_exists = Menu.objects.filter(art=art, name=name, price=price, allergens=allergens, types=types).exists() - + menu_exists = Menu.objects.filter(art=art, name=name, price=price, + allergens=allergens, types=types, + day=day).exists() if not menu_exists: - Menu(art=art, name=name, price=price, allergens=allergens, types=types).save() - - menu = Menu.objects.get(art=art, name=name, price=price, allergens=allergens, types=types) - menu.locations.add(location) - menu.days.add(day) - menu.save() + Menu(art=art, name=name, price=price, allergens=allergens, + types=types, day=day, location=location).save() def fetch(mensa_id: int): @@ -100,43 +82,3 @@ def fetch(mensa_id: int): if res.status_code != 200: raise Exception('request failed') return res.json() - - -def models_from_json(mensa_id: int): - for day_json in fetch_menus(mensa_id): - day_date = datetime.datetime.strptime(day_json['day'], '%Y-%m-%d').date() - - if not Day.objects.filter(mensa_id=mensa_id, date__contains=day_date): - # create new day model ONLY if it does not yet exist for the given - # day_date(s) and mensa_id. - - Day(mensa_id=mensa_id, date=day_date).save() - day = Day.objects.get(mensa_id=mensa_id, date=day_date) - - for menu_json in day_json['menus']: - # if meal already exists, simply add day to related menus. - # otherwise, build new meal model from json with the specified menu - # as the first related menu. - - if not Menu.objects.filter(name=menu_json['name']): - Menu.from_json(menu_json, day).save() - - day.menus.add(Menu.objects.get(name=menu_json['name'])) - - -def days_as_json(mensa_ids: list, types_filter: list, price_filter: str): - cw = datetime.date.today().isocalendar().week - dates = list(dict.fromkeys([day.date for day in Day.objects.filter(date__week=cw)])) - - days_list = [] - for date in dates: - day_json = { 'day': date, 'menus': [] } - for mensa_id in mensa_ids: - try: - day = Day.objects.get(mensa_id=mensa_id, date=date) - for menu in Menu.objects.filter(day=day, related_types__in=Type.objects.filter(type__in=types_filter) if types_filter else Type.objects.all()).distinct(): - day_json['menus'].append({ 'menu': menu, 'mensa_id': mensa_id }) - except ObjectDoesNotExist: - pass - days_list.append(day_json) - return days_list diff --git a/mensaviewer/migrations/0016_remove_menu_days_delete_day_menu_days.py b/mensaviewer/migrations/0016_remove_menu_days_delete_day_menu_days.py new file mode 100644 index 0000000..ba441b8 --- /dev/null +++ b/mensaviewer/migrations/0016_remove_menu_days_delete_day_menu_days.py @@ -0,0 +1,25 @@ +# Generated by Django 4.1.4 on 2023-01-06 00:06 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('mensaviewer', '0015_comment_timestamp'), + ] + + operations = [ + migrations.RemoveField( + model_name='menu', + name='days', + ), + migrations.DeleteModel( + name='Day', + ), + migrations.AddField( + model_name='menu', + name='days', + field=models.JSONField(blank=True, null=True), + ), + ] diff --git a/mensaviewer/migrations/0017_remove_menu_days_remove_menu_locations_location_city_and_more.py b/mensaviewer/migrations/0017_remove_menu_days_remove_menu_locations_location_city_and_more.py new file mode 100644 index 0000000..74db3bd --- /dev/null +++ b/mensaviewer/migrations/0017_remove_menu_days_remove_menu_locations_location_city_and_more.py @@ -0,0 +1,37 @@ +# Generated by Django 4.1.4 on 2023-01-06 22:11 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('mensaviewer', '0016_remove_menu_days_delete_day_menu_days'), + ] + + operations = [ + migrations.RemoveField( + model_name='menu', + name='days', + ), + migrations.RemoveField( + model_name='menu', + name='locations', + ), + migrations.AddField( + model_name='location', + name='city', + field=models.CharField(blank=True, max_length=144, null=True), + ), + migrations.AddField( + model_name='menu', + name='day', + field=models.DateField(blank=True, null=True), + ), + migrations.AddField( + model_name='menu', + name='location', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='mensaviewer.location'), + ), + ] diff --git a/mensaviewer/migrations/0018_rename_username_comment_author_and_more.py b/mensaviewer/migrations/0018_rename_username_comment_author_and_more.py new file mode 100644 index 0000000..e8bc311 --- /dev/null +++ b/mensaviewer/migrations/0018_rename_username_comment_author_and_more.py @@ -0,0 +1,42 @@ +# Generated by Django 4.1.4 on 2023-01-06 22:32 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('mensaviewer', '0017_remove_menu_days_remove_menu_locations_location_city_and_more'), + ] + + operations = [ + migrations.RenameField( + model_name='comment', + old_name='username', + new_name='author', + ), + migrations.RenameField( + model_name='comment', + old_name='related_menu', + new_name='menu', + ), + migrations.RemoveField( + model_name='comment', + name='comment', + ), + migrations.AddField( + model_name='comment', + name='text', + field=models.CharField(blank=True, max_length=512, null=True), + ), + migrations.CreateModel( + name='NewsArticle', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('author', models.CharField(default='anon', max_length=144)), + ('text', models.CharField(blank=True, max_length=8192, null=True)), + ('location', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mensaviewer.location')), + ], + ), + ] diff --git a/mensaviewer/migrations/0019_alter_comment_text_alter_newsarticle_text.py b/mensaviewer/migrations/0019_alter_comment_text_alter_newsarticle_text.py new file mode 100644 index 0000000..18f1dc5 --- /dev/null +++ b/mensaviewer/migrations/0019_alter_comment_text_alter_newsarticle_text.py @@ -0,0 +1,23 @@ +# Generated by Django 4.1.4 on 2023-01-06 22:39 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('mensaviewer', '0018_rename_username_comment_author_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='comment', + name='text', + field=models.TextField(blank=True, null=True), + ), + migrations.AlterField( + model_name='newsarticle', + name='text', + field=models.TextField(blank=True, null=True), + ), + ] diff --git a/mensaviewer/migrations/0020_newsarticle_timestamp.py b/mensaviewer/migrations/0020_newsarticle_timestamp.py new file mode 100644 index 0000000..22c1ef3 --- /dev/null +++ b/mensaviewer/migrations/0020_newsarticle_timestamp.py @@ -0,0 +1,19 @@ +# Generated by Django 4.1.4 on 2023-01-08 01:01 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('mensaviewer', '0019_alter_comment_text_alter_newsarticle_text'), + ] + + operations = [ + migrations.AddField( + model_name='newsarticle', + name='timestamp', + field=models.DateTimeField(blank=True, default=django.utils.timezone.now), + ), + ] diff --git a/mensaviewer/migrations/0021_newsarticle_title.py b/mensaviewer/migrations/0021_newsarticle_title.py new file mode 100644 index 0000000..68be4bf --- /dev/null +++ b/mensaviewer/migrations/0021_newsarticle_title.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.4 on 2023-01-08 02:10 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('mensaviewer', '0020_newsarticle_timestamp'), + ] + + operations = [ + migrations.AddField( + model_name='newsarticle', + name='title', + field=models.CharField(blank=True, max_length=144), + ), + ] diff --git a/mensaviewer/migrations/0022_alter_menu_allergens_alter_menu_art_alter_menu_name_and_more.py b/mensaviewer/migrations/0022_alter_menu_allergens_alter_menu_art_alter_menu_name_and_more.py new file mode 100644 index 0000000..e2dac3f --- /dev/null +++ b/mensaviewer/migrations/0022_alter_menu_allergens_alter_menu_art_alter_menu_name_and_more.py @@ -0,0 +1,38 @@ +# Generated by Django 4.1.4 on 2023-01-08 03:00 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('mensaviewer', '0021_newsarticle_title'), + ] + + operations = [ + migrations.AlterField( + model_name='menu', + name='allergens', + field=models.JSONField(blank=True, default=[]), + ), + migrations.AlterField( + model_name='menu', + name='art', + field=models.CharField(choices=[('M', 'Mensa'), ('C', 'Cafeteria')], default='M', max_length=1), + ), + migrations.AlterField( + model_name='menu', + name='name', + field=models.CharField(blank=True, default='', max_length=144), + ), + migrations.AlterField( + model_name='menu', + name='price', + field=models.CharField(blank=True, default='', max_length=144), + ), + migrations.AlterField( + model_name='menu', + name='types', + field=models.JSONField(blank=True, default=[]), + ), + ] diff --git a/mensaviewer/migrations/0023_alter_menu_allergens_alter_menu_art_alter_menu_name_and_more.py b/mensaviewer/migrations/0023_alter_menu_allergens_alter_menu_art_alter_menu_name_and_more.py new file mode 100644 index 0000000..83050e1 --- /dev/null +++ b/mensaviewer/migrations/0023_alter_menu_allergens_alter_menu_art_alter_menu_name_and_more.py @@ -0,0 +1,38 @@ +# Generated by Django 4.1.4 on 2023-01-08 18:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('mensaviewer', '0022_alter_menu_allergens_alter_menu_art_alter_menu_name_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='menu', + name='allergens', + field=models.JSONField(blank=True, default=list), + ), + migrations.AlterField( + model_name='menu', + name='art', + field=models.CharField(choices=[('Mensa', 'Mensa'), ('Cafeteria', 'Cafeteria')], default='M', max_length=24), + ), + migrations.AlterField( + model_name='menu', + name='name', + field=models.CharField(blank=True, default='---', max_length=144), + ), + migrations.AlterField( + model_name='menu', + name='price', + field=models.CharField(blank=True, default='---', max_length=144), + ), + migrations.AlterField( + model_name='menu', + name='types', + field=models.JSONField(blank=True, default=list), + ), + ] diff --git a/mensaviewer/models.py b/mensaviewer/models.py index ffe945b..d820e14 100644 --- a/mensaviewer/models.py +++ b/mensaviewer/models.py @@ -10,7 +10,7 @@ class Location(models.Model): mensa_id = models.CharField(max_length=2, null=True, blank=True) def __str__(self): - return f"{self.city} - {self.name} [{self.mensa_id}]" + return f"{self.city} - {self.name}" @property def menus(self): @@ -33,7 +33,7 @@ class Menu(models.Model): blank=True) def __str__(self): - return f"{self.name}" + return f"{self.name} - {self.location} | {self.day}" @property def get_price(self): @@ -69,6 +69,9 @@ class Comment(models.Model): 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) @@ -76,3 +79,6 @@ class NewsArticle(models.Model): 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}" diff --git a/mensaviewer/templates/base.html b/mensaviewer/templates/base.html index e8f51df..2c8f291 100644 --- a/mensaviewer/templates/base.html +++ b/mensaviewer/templates/base.html @@ -5,19 +5,28 @@
-