calibre-web-companion/CalibreWebCompanion/library/views.py

127 lines
4.0 KiB
Python
Raw Normal View History

2020-07-08 02:11:21 +00:00
from django.shortcuts import render
from django.views import generic
from .models import Author, Book, Comment, Rating, BookAuthorLink, Publisher, Tag, BookTagLink, BookRatingLink, Data
2020-07-08 16:22:37 +00:00
from django.http import HttpResponseRedirect
2020-07-09 01:57:30 +00:00
from .forms import SearchForm
from django.db import models
from django.db.models import Q
2020-07-09 05:14:08 +00:00
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
2020-07-09 01:57:30 +00:00
2020-07-09 05:14:08 +00:00
@login_required
def index(request):
return render(request,'accounts/index.html')
def sign_up(request):
context = {}
form = UserCreationForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
user = form.save()
login(request,user)
return render(request,'registration/index.html')
context['form']=form
return render(request,'registration/sign_up.html',context)
2020-07-09 01:57:30 +00:00
class SearchView(generic.TemplateView):
template_name = 'search.html'
class ResultsView(generic.ListView): # no clue if this is secure.
# according to this https://stackoverflow.com/questions/13574043/how-do-django-forms-sanitize-text-input-to-prevent-sql-injection-xss-etc
2020-07-09 01:57:30 +00:00
# it is
model = Book
2020-07-09 01:57:30 +00:00
template_name = 'results.html'
def get_queryset(self): # new
2020-07-09 01:57:30 +00:00
title = self.request.GET.get('title')
author = self.request.GET.get('author')
return Book.objects.filter(
2020-07-09 01:57:30 +00:00
Q(sort__icontains=title) and Q(author_sort__icontains=author)
)
2020-07-08 02:11:21 +00:00
2020-07-08 02:11:21 +00:00
class AuthorListView(generic.ListView):
model = Author
2020-07-08 02:11:21 +00:00
class BookListView(generic.ListView):
model = Book
2020-07-08 02:11:21 +00:00
class PublisherListView(generic.ListView):
model = Publisher
2020-07-08 02:11:21 +00:00
class RatingListView(generic.ListView):
model = Rating
2020-07-08 02:11:21 +00:00
class TagListView(generic.ListView):
model = Tag
2020-07-08 02:11:21 +00:00
class AuthorDetailView(generic.DetailView):
model = Author
2020-07-08 02:11:21 +00:00
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(AuthorDetailView, self).get_context_data(**kwargs)
# Create any data and add it to the context
books = BookAuthorLink.objects.filter(author=context["object"].id)
context['books'] = sorted(
2020-07-09 01:57:30 +00:00
[b.book for b in books.all()], key=lambda x: x.title)
2020-07-08 02:11:21 +00:00
return context
class BookDetailView(generic.DetailView):
model = Book
2020-07-08 02:11:21 +00:00
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(BookDetailView, self).get_context_data(**kwargs)
# Create any data and add it to the context
try:
context['comment'] = Comment.objects.get(
2020-07-08 02:11:21 +00:00
book=context["object"].id).text
except:
pass
context["imgpath"] = context["object"].path + "/cover.jpg"
download = Data.objects.get(book=context["object"].id)
context["download"] = f"{context['object'].path}/{download.name}.{download.format}"
return context
class PublisherDetailView(generic.DetailView):
model = Publisher
2020-07-08 02:11:21 +00:00
class RatingDetailView(generic.DetailView):
model = Rating
2020-07-08 02:11:21 +00:00
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(RatingDetailView, self).get_context_data(**kwargs)
# Create any data and add it to the context
books = BookRatingLink.objects.filter(rating=context["object"].id)
2020-07-09 01:57:30 +00:00
context['books'] = sorted(
[b.book for b in books.all()], key=lambda x: x.title)
2020-07-08 02:11:21 +00:00
return context
class TagDetailView(generic.DetailView):
model = Tag
2020-07-08 02:11:21 +00:00
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(TagDetailView, self).get_context_data(**kwargs)
# Create any data and add it to the context
books = BookTagLink.objects.filter(tag=context["object"].id)
2020-07-09 01:57:30 +00:00
context['books'] = sorted(
[b.book for b in books.all()], key=lambda x: x.title)
2020-07-08 02:11:21 +00:00
return context