from django.shortcuts import render from django.http import HttpResponse from django.views.decorators.cache import cache_page from django.views.generic import DetailView from django.shortcuts import redirect from .helpers import get_filtered_plan from .models import Meal, DailyMenu, Mensa, Comment from .forms import FilterForm, CommentForm @cache_page(60 * 15) def mensa_flensburg(request): flags = {'vegan': None, 'vegetarian': None, 'price_for': None,} if request.method == 'POST': form = FilterForm(request.POST) if form.is_valid(): flags['vegan'] = form.cleaned_data['vegan'] flags['vegetarian'] = form.cleaned_data['vegetarian'] flags['price_for'] = form.cleaned_data['price_for'] else: flags['vegan'] = request.COOKIES.get('vegan') flags['vegetarian'] = request.COOKIES.get('vegetarian') flags['price_for'] = request.COOKIES.get('price_for') form = FilterForm(request.COOKIES) context = { 'mensa_list': { 'Hauptmensa': get_filtered_plan(7, flags), 'B-Mensa': get_filtered_plan(14, flags), }, 'form': form, } response = render(request, 'mensa_list.html', context) response.set_cookie('vegan', flags['vegan']) response.set_cookie('vegetarian', flags['vegetarian']) response.set_cookie('price_for', flags['price_for']) return response def meal(request, meal_id): form = CommentForm() if request.method == 'POST': if request.POST['button'] == 'comment': form = CommentForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] text = form.cleaned_data['text'] Comment(meal=Meal.objects.get(pk=meal_id), username=username, comment=text).save() elif request.POST['button'] == 'like': meal = Meal.objects.get(pk=meal_id) meal.likes += 1 meal.save() elif request.POST['button'] == 'dislike': meal = Meal.objects.get(pk=meal_id) meal.dislikes += 1 meal.save() else: form = CommentForm() context = { 'meal': Meal.objects.get(pk=meal_id), 'form': form, 'comments': Comment.objects.filter(meal=Meal.objects.get(pk=meal_id)) } return render(request, 'meal.html', context) @cache_page(60 * 15) def mensa_by_id(request, mensa_id): flags = {'vegan': None, 'vegetarian': None, 'price_for': None,} if request.method == 'POST': for key in flags: flags[key] = request.POST.get(key) else: for key in flags: flags[key] = request.COOKIES.get(key) context = { 'mensa_list': { f'Mensa {mensa_id}': get_filtered_plan((mensa_id), flags), }, 'flags': flags, } response = render(request, 'mensa_list.html', context) response.set_cookie('vegan', flags['vegan']) response.set_cookie('vegetarian', flags['vegetarian']) response.set_cookie('price_for', flags['price_for']) return response def meal_form(request): return render(request, 'forms.html') def meal_delet(request): return def meal_update(request): return def meal_save(request): #mensa = request.POST.get('mensa') id = request.POST.get('id') name = request.POST.get('name') art = request.POST.get('art') priceStudents = request.POST.get('priceStudents') priceEmployee = request.POST.get('priceEmployee') priceGast = request.POST.get('priceGast') allergens = request.POST.get('allergens') types = request.POST.get('types') b = None if id: b = Meal.objects.get(id=id) b.name = name b.art = art b.priceStudents = priceStudents b.priceEmployee = priceEmployee b.priceGast = priceGast b.allergens = allergens b.types = types else: b = Meal(name=name, art=art, price_student=priceStudents,price_employee=priceEmployee,price_guest=priceGast,allergens=allergens,types=types) b.save() return redirect('mensa:mensa_flensburg')