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.
103 lines
4.2 KiB
Python
103 lines
4.2 KiB
Python
from .models import Mensa, DailyMenu, Meal
|
|
|
|
import requests
|
|
import datetime
|
|
|
|
|
|
def fetch_mensa(mensa_id):
|
|
res = requests.get('https://mensa.fly.dev/' + str(mensa_id))
|
|
if res.status_code != 200:
|
|
raise Exception('request failed')
|
|
return res.json()
|
|
|
|
|
|
def save_in_database(mensa_id):
|
|
# save mensa with mensa_id 'mensa_id' in database if entry does not exit yet
|
|
if mensa_id not in [m.mensa_id for m in Mensa.objects.all()]:
|
|
Mensa(mensa_id=mensa_id).save()
|
|
|
|
# get mensa with 'mensa_id' from database
|
|
mensa = Mensa.objects.get(mensa_id=mensa_id)
|
|
|
|
# cycle through days listed in fetched data
|
|
for day in fetch_mensa(mensa_id):
|
|
# save daily_menu for mensa with 'mensa_id' in database if entry does not exist yet
|
|
if day['day'] + str(mensa_id) not in [str(day.date) + str(day.mensa) for day in DailyMenu.objects.all()]:
|
|
DailyMenu(date=day['day'], mensa=mensa).save()
|
|
|
|
# get this mensas daily_menu for current day from database
|
|
daily_menu = DailyMenu.objects.get(date=day['day'], mensa=mensa)
|
|
|
|
# cycle through meals listed in current day
|
|
for meal in day['menus']:
|
|
name = meal['name']
|
|
|
|
# if meal with name 'name' already exists in database
|
|
if name in [meal_object.name for meal_object in Meal.objects.all()]:
|
|
# simply add current daily_menu to it's related_daily_menus
|
|
Meal.objects.get(name=name).related_daily_menus.add(daily_menu)
|
|
|
|
# otherwise
|
|
else:
|
|
# create new meal object, save in database and add current daily_menu to it's related_daily_menus
|
|
art = meal['art']
|
|
types = ' '.join(meal['types'])
|
|
allergens = ' '.join(meal['allergens'])
|
|
price_student = 0
|
|
price_employee = 0
|
|
price_guest = 0
|
|
if " / " in meal['price']:
|
|
price_student = float(meal['price'].split(' / ')[0].replace('€', '').replace(',', '.'))
|
|
price_employee = float(meal['price'].split(' / ')[1].replace('€', '').replace(',', '.'))
|
|
price_guest = float(meal['price'].split(' / ')[2].replace('€', '').replace(',', '.'))
|
|
|
|
new_meal = Meal(art=art, name=name, price_student=price_student, price_employee=price_employee, price_guest=price_guest, allergens=allergens, types=types)
|
|
new_meal.save()
|
|
new_meal.related_daily_menus.add(daily_menu)
|
|
new_meal.save()
|
|
|
|
|
|
def get_filtered_plan(mensa_id, flags):
|
|
|
|
|
|
|
|
week = []
|
|
for menu in DailyMenu.objects.filter(mensa=Mensa.objects.get(mensa_id=mensa_id), date__week=datetime.date.today().isocalendar().week):
|
|
meals = []
|
|
for meal in Meal.objects.filter(related_daily_menus=menu, types__contains='vn' if flags['vegan'] else 've' if flags['vegetarian'] else ''):
|
|
meals.append({
|
|
'art': meal.art,
|
|
'name': meal.name,
|
|
'price': meal.price_student if flags['price_for'] == 's' else meal.price_employee if flags['price_for'] == 'e' else meal.price_guest,
|
|
'allergens': meal.allergens,
|
|
'types': meal.types,
|
|
'likes': meal.likes,
|
|
'dislikes': meal.dislikes,
|
|
'pk': meal.pk
|
|
})
|
|
week.append({'date': menu.date, 'meals': meals})
|
|
return week
|
|
|
|
|
|
def filter_plan_old(plan, flags):
|
|
for day in plan:
|
|
filtered_menus = []
|
|
for menu in day['menus']:
|
|
if ' / ' in menu['price']:
|
|
if flags['price_for'] == 's':
|
|
menu['price'] = menu['price'].split(' / ')[0]
|
|
elif flags['price_for'] == 'e':
|
|
menu['price'] = menu['price'].split(' / ')[1]
|
|
elif flags['price_for'] == 'g':
|
|
menu['price'] = menu['price'].split(' / ')[2]
|
|
if flags['vegan'] == True:
|
|
if 'vn' in menu['types']:
|
|
filtered_menus.append(menu)
|
|
elif flags['vegetarian'] == True:
|
|
if 've' in menu['types']:
|
|
filtered_menus.append(menu)
|
|
else:
|
|
filtered_menus.append(menu)
|
|
day['menus'] = filtered_menus
|
|
return plan
|