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.

223 lines
6.1 KiB
Python

from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.shortcuts import render, redirect, reverse
from django.urls import reverse_lazy
from .models import *
from .helpers import *
from .forms import *
from .data import *
def flensburg(request):
filter_query = request.COOKIES.get('filter_query', '')
if len(request.GET.getlist('filterform')) == 0 and filter_query != '':
return redirect(reverse('mensaviewer:flensburg') + '?' + filter_query)
checked_types = request.GET.getlist('type')
status = request.GET.get('status')
selected_locations = Location.objects.filter(mensa_id__in=[7, 14])
if not selected_locations.exists():
Location(city="Flensburg", name="Hauptmensa", mensa_id=7).save()
Location(city="Flensburg", name="B-Mensa", mensa_id=14).save()
context = {
'types': [(TYPES[type], type) for type in TYPES],
'checked_types': checked_types,
'status_values': STATUS_VALUES,
'status': status,
'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 custom(request):
filter_query = request.COOKIES.get('filter_query', '')
if len(request.GET.getlist('filterform')) == 0 and filter_query != '':
return redirect(reverse('mensaviewer:custom') + '?' + 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, "custom.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(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(request.GET.get('next'))
class LocationCreateView(CreateView):
model = Location
template_name = "location/edit.html"
fields = '__all__'
success_url = reverse_lazy("mensaviewer:location_list")
class LocationUpdateView(UpdateView):
model = Location
template_name = "location/edit.html"
fields = '__all__'
success_url = reverse_lazy("mensaviewer:location_list")
class LocationDeleteView(DeleteView):
model = Location
template_name = "location/delete.html"
success_url = reverse_lazy("mensaviewer:location_list")
class LocationDetailView(DetailView):
model = Location
template_name = "location/detail.html"
class LocationListView(ListView):
model = Location
template_name = "location/list.html"
class NewsArticleCreateView(CreateView):
model = NewsArticle
template_name = "news/edit.html"
fields = ['location', 'title', 'author', 'text']
success_url = reverse_lazy("mensaviewer:news_list")
def get_initial(self):
if 'pk' in self.kwargs:
return { 'location': get_object_or_404(Location, pk=self.kwargs['pk']) }
else:
return { 'location': None }
class NewsArticleUpdateView(UpdateView):
model = NewsArticle
template_name = "news/edit.html"
fields = ['location', 'title', 'author', 'text']
success_url = reverse_lazy("mensaviewer:news_list")
class NewsArticleDeleteView(DeleteView):
model = NewsArticle
template_name = "news/delete.html"
success_url = reverse_lazy("mensaviewer:news_list")
class NewsArticleDetailView(DetailView):
model = NewsArticle
template_name = "news/detail.html"
class NewsArticleListView(ListView):
model = NewsArticle
template_name = "news/list.html"
class MenuCreateView(CreateView):
model = Menu
form_class = MenuForm
template_name = "menu/edit.html"
success_url = reverse_lazy("mensaviewer:menu_list")
def get_initial(self):
if 'pk' in self.kwargs:
return { 'location': get_object_or_404(Location, pk=self.kwargs['pk']) }
else:
return { 'location': None }
class MenuUpdateView(UpdateView):
model = Menu
form_class = MenuForm
template_name = "menu/edit.html"
success_url = reverse_lazy("mensaviewer:menu_list")
def get_initial(self):
menu = get_object_or_404(Menu, pk=self.kwargs['pk'])
print(menu.types)
return { 'types': menu.types }
class MenuDeleteView(DeleteView):
model = Menu
template_name = "menu/delete.html"
success_url = reverse_lazy("mensaviewer:menu_list")
class MenuDetailView(DetailView):
model = Menu
template_name = "menu/detail.html"
class MenuListView(ListView):
model = Menu
template_name = "menu/list.html"
class CommentCreateView(CreateView):
model = Comment
fields = ['menu', 'author', 'text']
template_name = "comment/edit.html"
success_url = '/menu/{menu_id}/'
def get_initial(self):
if 'pk' in self.kwargs:
return { 'menu': get_object_or_404(Menu, pk=self.kwargs['pk']) }
else:
return { 'menu': None }
class CommentUpdateView(UpdateView):
model = Comment
fields = ['author', 'text']
template_name = "comment/edit.html"
success_url = '/menu/{menu_id}/'
class CommentDeleteView(DeleteView):
model = Comment
template_name = "comment/delete.html"
success_url = '/menu/{menu_id}/'
class CommentDetailView(DetailView):
model = Comment
template_name = "comment/detail.html"
class CommentListView(ListView):
model = Comment
template_name = "comment/list.html"