from django.views.generic import DetailView from django.views.generic.edit import CreateView from django.shortcuts import render, redirect, reverse from .models import * from .helpers import * from .forms import * from .data import * def home(request): filter_query = request.COOKIES.get('filter_query', '') if len(request.GET.getlist('filterform')) == 0 and filter_query != '': return redirect(reverse('mensaviewer:home') + '?' + filter_query) selected_location_ids = request.GET.getlist('location') checked_types = request.GET.getlist('type') status = request.GET.get('status') selected_locations = Location.objects.filter(mensa_id__in=selected_location_ids) context = { 'types': [(TYPES[type], type) for type in TYPES], 'checked_types': checked_types, 'status_values': STATUS_VALUES, 'status': status, 'locations': Location.objects.all(), 'selected_locations': selected_location_ids, 'menu_data': load_data(selected_locations, checked_types, status), } response = render(request, "flensburg.html", context) response.set_cookie('filter_query', request.GET.urlencode()) return response def like(request, pk): if request.method == 'GET': menu = Menu.objects.get(pk=pk) menu.likes += 1 menu.save() return redirect(reverse('mensaviewer:home') + '?' + request.GET.get('next')) def dislike(request, pk): if request.method == 'GET': menu = Menu.objects.get(pk=pk) menu.likes -= 1 menu.save() return redirect(reverse('mensaviewer:home') + '?' + request.GET.get('next')) class MenuDetailView(DetailView): model = Menu template_name = "menu_detail.html" queryset = Menu.objects.all() class LocationCreateView(CreateView): model = Location fields = '__all__' template_name = "create_location.html" success_url = "/" class CommentCreateView(CreateView): model = Comment fields = ['username', 'comment'] template_name = "comment.html" success_url = '/menu/{pk}/' def form_valid(self, form): self.menu = get_object_or_404(Menu, pk=self.kwargs['pk']) form.instance.menu = self.menu print(self.menu) return super().form_valid(form)