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

236 lines
8.1 KiB
Python
Raw Permalink Normal View History

2020-08-16 04:45:28 +00:00
from django.utils.decorators import method_decorator
2020-07-08 02:11:21 +00:00
from django.shortcuts import render
from django.views import generic
2020-07-15 15:20:35 +00:00
from .models import Author, Book, Comment, Rating, BookAuthorLink, Publisher, Tag, BookTagLink, BookRatingLink, Data, Identifier, Series
2020-07-08 16:22:37 +00:00
from django.http import HttpResponseRedirect
2020-07-09 13:41:41 +00:00
from .forms import SearchForm, UserCreationForm
2020-07-09 01:57:30 +00:00
from django.db import models
from django.db.models import Q
2020-07-09 05:14:08 +00:00
from django.contrib.auth.models import User
from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
2020-08-02 02:51:34 +00:00
import logging
2020-07-09 01:57:30 +00:00
2020-08-02 02:51:34 +00:00
logger = logging.getLogger(__name__)
2020-07-17 03:48:54 +00:00
# might be helpful for vary headers later
2020-07-15 23:30:04 +00:00
2020-07-09 05:14:08 +00:00
@login_required
def index(request):
2020-07-09 14:58:09 +00:00
return render(request, 'accounts/index.html')
2020-07-09 05:14:08 +00:00
def sign_up(request):
context = {}
form = UserCreationForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
user = form.save()
2020-07-09 14:58:09 +00:00
login(request, user)
return render(request, 'registration/index.html')
context['form'] = form
return render(request, 'registration/sign_up.html', context)
2020-07-09 05:14:08 +00:00
2020-07-09 01:57:30 +00:00
class SearchView(generic.TemplateView):
template_name = 'search.html'
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(SearchView, self).dispatch(*args, **kwargs)
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'
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(ResultsView, self).dispatch(*args, **kwargs)
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')
2020-07-15 03:34:44 +00:00
identifier = self.request.GET.get("identifier")
2020-07-15 13:42:49 +00:00
generic = self.request.GET.get("generic")
2020-07-09 14:58:09 +00:00
books = Book.objects.prefetch_related("tags", "ratings")
if title:
2020-07-15 13:42:49 +00:00
books = books.filter(sort__icontains=title)
2020-07-09 14:58:09 +00:00
if author:
2020-08-16 04:45:28 +00:00
# authors are stored as author_sort and author, needs to be slightly more complex
author_obj = Author.objects.filter(name__icontains=author).first()
if not author_obj:
author_id = -1
else:
author_id = author_obj.id
books = books.filter(
Q(author_sort__icontains=author) |
Q(authors__id=author_id)
)
2020-07-15 03:34:44 +00:00
if identifier:
books = books.filter(identifier__val=identifier)
2020-07-15 13:42:49 +00:00
if generic:
2020-08-16 04:45:28 +00:00
author_obj = Author.objects.filter(name__icontains=generic).first()
if not author_obj:
author_id = -1
else:
author_id = author_obj.id
2020-07-15 13:42:49 +00:00
books = books.filter(
2020-07-17 03:48:54 +00:00
Q(sort__icontains=generic) |
Q(author_sort__icontains=generic) |
2020-08-16 04:45:28 +00:00
Q(authors__id=author_id) |
2020-07-15 13:42:49 +00:00
Q(identifier__val=generic)
)
2020-07-09 14:58:09 +00:00
return books
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
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(AuthorListView, self).dispatch(*args, **kwargs)
2020-07-08 02:11:21 +00:00
class BookListView(generic.ListView):
model = Book
2020-07-15 13:42:49 +00:00
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(BookListView, self).dispatch(*args, **kwargs)
2020-07-09 14:58:09 +00:00
def get_queryset(self):
# Annotate the books with ratings, tags, etc
# books = Book.objects.annotate(
queryset = Book.objects.prefetch_related("tags", "ratings")
return queryset
2020-07-08 02:11:21 +00:00
class PublisherListView(generic.ListView):
model = Publisher
2020-07-08 02:11:21 +00:00
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(PublisherListView, self).dispatch(*args, **kwargs)
2020-07-08 02:11:21 +00:00
class RatingListView(generic.ListView):
model = Rating
2020-07-08 02:11:21 +00:00
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(RatingListView, self).dispatch(*args, **kwargs)
class SeriesListView(generic.ListView): # make url entry and template, sometime
2020-07-15 15:20:35 +00:00
model = Series
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(SeriesListView, self).dispatch(*args, **kwargs)
2020-07-08 02:11:21 +00:00
class TagListView(generic.ListView):
model = Tag
2020-07-08 02:11:21 +00:00
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(TagListView, self).dispatch(*args, **kwargs)
2020-07-08 02:11:21 +00:00
class AuthorDetailView(generic.DetailView):
model = Author
2020-07-08 02:11:21 +00:00
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(AuthorDetailView, self).dispatch(*args, **kwargs)
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
2020-07-09 14:58:09 +00:00
books = Book.objects.prefetch_related("tags", "ratings")
books = books.filter(authors=context["object"].id)
context['books'] = sorted(books, 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
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(BookDetailView, self).dispatch(*args, **kwargs)
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.lower()}"
2020-07-08 02:11:21 +00:00
return context
class PublisherDetailView(generic.DetailView):
model = Publisher
2020-07-08 02:11:21 +00:00
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(PublisherDetailView, self).dispatch(*args, **kwargs)
2020-07-09 14:58:09 +00:00
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(PublisherDetailView, self).get_context_data(**kwargs)
# Create any data and add it to the context
books = Book.objects.prefetch_related("tags", "ratings")
books = books.filter(publishers=context["object"].id)
context['books'] = sorted(books, key=lambda x: x.title)
return context
2020-07-08 02:11:21 +00:00
class RatingDetailView(generic.DetailView):
model = Rating
2020-07-08 02:11:21 +00:00
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(RatingDetailView, self).dispatch(*args, **kwargs)
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
2020-07-09 14:58:09 +00:00
books = Book.objects.prefetch_related("tags", "ratings")
books = books.filter(ratings=context["object"].id)
context['books'] = sorted(books, 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
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(TagDetailView, self).dispatch(*args, **kwargs)
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
2020-07-09 14:58:09 +00:00
books = Book.objects.prefetch_related("tags", "ratings")
books = books.filter(tags=context["object"].id)
context['books'] = sorted(books, key=lambda x: x.title)
2020-07-08 02:11:21 +00:00
return context
2020-07-15 15:20:35 +00:00
2020-07-17 03:48:54 +00:00
2020-07-15 15:20:35 +00:00
class SeriesDetailView(generic.DetailView):
model = Series
2020-07-17 03:48:54 +00:00
def dispatch(self, *args, **kwargs):
return super(SeriesDetailView, self).dispatch(*args, **kwargs)
2020-07-15 15:20:35 +00:00
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(SeriesDetailView, self).get_context_data(**kwargs)
# Create any data and add it to the context
books = Book.objects.prefetch_related("tags", "ratings")
books = books.filter(series=context["object"].id)
context['books'] = sorted(books, key=lambda x: x.title)
2020-07-17 03:48:54 +00:00
return context