arrowcounter/counter/views.py

52 lines
1.6 KiB
Python
Raw Normal View History

from django.shortcuts import render
from django.http import HttpResponse
2018-08-09 16:06:25 +00:00
from django.views import generic
from django.template import loader
2018-08-09 16:06:25 +00:00
from django.urls import reverse_lazy
from .models import ArrowCount
2018-08-09 16:06:25 +00:00
from .forms import ArrowCountForm
from django.contrib.auth.decorators import login_required
from django.conf import settings
from django.core.exceptions import SuspiciousOperation
def index(request):
template = loader.get_template('index.html')
return HttpResponse(template.render({}, request))
@login_required
def arrow_count_list(request):
page = request.GET.get('page')
if not page:
page = 1
else:
page = int(page)
if page <= 0:
raise SuspiciousOperation("page is negative or 0")
start = settings.ITEMS_PER_PAGE * (page - 1)
finish = settings.ITEMS_PER_PAGE + start
2018-08-09 16:06:25 +00:00
counts = ArrowCount.objects.order_by('-date') \
.filter(user = request.user)[start:finish]
template = loader.get_template('counter/list.html')
return HttpResponse(template.render({'counts': counts}, request))
2018-08-09 16:06:25 +00:00
class NewArrowCount(generic.CreateView):
form_class = ArrowCountForm
success_url = reverse_lazy('count_list')
template_name = 'counter/new.html'
def form_valid(self, form):
form.instance.user = self.request.user
return super().form_valid(form)
class EditArrowCount(generic.UpdateView):
form_class = ArrowCountForm
success_url = reverse_lazy('count_list')
template_name = 'counter/edit.html'
def get_object(self, queryset=None):
obj = ArrowCount.objects.get(id=self.kwargs['id'])
return obj