gulasch-r0mstore/roms/views.py

129 lines
3.3 KiB
Python
Raw Normal View History

from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
2017-05-16 00:05:52 +02:00
from django.views.generic.edit import CreateView, UpdateView
2017-05-14 01:53:16 +02:00
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import JsonResponse
2017-05-14 01:53:16 +02:00
from django.contrib.auth import get_user_model
from django.db.models import Q
2017-02-01 22:05:48 +01:00
2017-05-07 01:31:35 +02:00
from taggit.models import Tag
2017-05-16 16:13:35 +02:00
from dal import autocomplete
2017-05-07 01:31:35 +02:00
from roms.models import Rom
2017-05-16 16:13:35 +02:00
from roms.forms import RomCreateForm, RomUpdateForm
2017-05-07 01:31:35 +02:00
class RomList(ListView):
template_name = 'roms/list.html'
context_object_name = 'roms'
def get_queryset(self):
tag = self.kwargs.get('tag', None)
queryset = Rom.objects.all().filter(approved = True)
if tag != None:
queryset = queryset.filter(tags__slug = tag)
return queryset
def get_context_data(self, **kwargs):
context = super(RomList, self).get_context_data(**kwargs)
context['filter_tag'] = self.kwargs.get('tag', None)
context['tags'] = Tag.objects.all()
return context
2017-05-07 02:51:25 +02:00
class RomDetailView(DetailView):
pk_url_kwarg = 'id'
template_name = 'roms/details.html'
context_object_name = 'rom'
def get_queryset(self):
2017-05-14 01:53:16 +02:00
queryset = Rom.objects.all().filter(Q(approved = True) | Q(user__id = self.request.user.id))
2017-05-07 02:51:25 +02:00
return queryset
2017-05-14 01:53:16 +02:00
class RomCreateView(LoginRequiredMixin, CreateView):
model = Rom
2017-05-16 16:13:35 +02:00
form_class = RomCreateForm
2017-05-14 01:53:16 +02:00
template_name = 'roms/create.html'
def form_valid(self, form):
res = super(RomCreateView, self).form_valid(form)
self.object.user = self.request.user
self.object.save()
return res
2017-05-16 00:05:52 +02:00
class RomUpdateView(LoginRequiredMixin, UpdateView):
model = Rom
pk_url_kwarg = 'id'
2017-05-16 16:13:35 +02:00
form_class = RomUpdateForm
2017-05-16 00:05:52 +02:00
template_name = 'roms/update.html'
def get_queryset(self):
return Rom.objects.filter(user__id = self.request.user.id)
2017-05-16 16:13:35 +02:00
class TagAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
# Don't forget to filter out results depending on the visitor !
if not self.request.user.is_authenticated():
return Tag.objects.none()
qs = Tag.objects.all()
if self.q:
qs = qs.filter(name__istartswith=self.q)
return qs
2017-05-16 00:05:52 +02:00
2017-05-14 01:53:16 +02:00
class RomListJson(ListView):
2017-05-07 01:31:35 +02:00
def get_queryset(self):
2017-05-07 01:31:35 +02:00
tag = self.kwargs.get('tag', None)
queryset = Rom.objects.all().filter(approved = True)
if tag != None:
queryset = queryset.filter(tags__slug = tag)
return queryset
def render_to_response(self, context, **response_kwargs):
queryset = self.get_queryset()
2017-05-07 01:31:35 +02:00
filter_tag = self.kwargs.get('tag', '')
json = {
'filter_tag' : filter_tag,
'tags': {},
'roms' : {},
}
for tag in Tag.objects.all():
json['tags'][tag.slug] = tag.name
for rom in queryset:
2017-05-07 01:31:35 +02:00
json['roms'][rom.id] = rom.name
return JsonResponse(json, **response_kwargs, safe=False)
class RomDetailViewJson(DetailView):
pk_url_kwarg = 'id'
2017-05-07 02:51:25 +02:00
def get_queryset(self):
queryset = Rom.objects.all().filter(approved = True)
return queryset
def render_to_response(self, context, **response_kwargs):
rom = self.get_object()
return JsonResponse(rom.to_json(), **response_kwargs, safe=False)