From 7d2b429cbee20e003542487e55cfd7100f03bd17 Mon Sep 17 00:00:00 2001 From: MassiveAtoms Date: Thu, 9 Jul 2020 11:58:09 -0300 Subject: [PATCH] optimized queries --- .../CalibreWebCompanion/settings.py | 35 ++++++++---- .../CalibreWebCompanion/urls.py | 13 ++++- .../templates/library/publisher_detail.html | 4 +- .../library/templates/results.html | 2 +- CalibreWebCompanion/library/views.py | 55 ++++++++++++------- 5 files changed, 75 insertions(+), 34 deletions(-) diff --git a/CalibreWebCompanion/CalibreWebCompanion/settings.py b/CalibreWebCompanion/CalibreWebCompanion/settings.py index 5e06eef..d4a27f6 100644 --- a/CalibreWebCompanion/CalibreWebCompanion/settings.py +++ b/CalibreWebCompanion/CalibreWebCompanion/settings.py @@ -14,7 +14,8 @@ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -CALIBRE_DIR = os.path.abspath("C:\\Users\\MassiveAtoms\\Documents\\Calibre Library") +CALIBRE_DIR = os.path.abspath( + "C:\\Users\\MassiveAtoms\\Documents\\Calibre Library") EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' @@ -36,14 +37,31 @@ STATIC_URL = '/static/' SECRET_KEY = 'u(8^+rb%rz5hsx4v^^y(ul7g(4n7a8!db@s*9(m5cs*2_ppy8+' -ALLOWED_HOSTS = [] - -#### Don't change things beyond this +ALLOWED_HOSTS = ['127.0.0.1', ] +INTERNAL_IPS = [ + # ... + '127.0.0.1', + # ... +] +# Don't change things beyond this # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True +DEBUG_TOOLBAR_PANELS = [ + 'debug_toolbar.panels.timer.TimerPanel', + 'debug_toolbar.panels.headers.HeadersPanel', + 'debug_toolbar.panels.request.RequestPanel', + 'debug_toolbar.panels.sql.SQLPanel', + 'debug_toolbar.panels.templates.TemplatesPanel', + 'debug_toolbar.panels.cache.CachePanel', + 'debug_toolbar.panels.signals.SignalsPanel', + 'debug_toolbar.panels.redirects.RedirectsPanel', + 'debug_toolbar.panels.profiling.ProfilingPanel', +] + + @@ -59,10 +77,12 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - "library" + "library", + # 'debug_toolbar', # for debugging purposes ] MIDDLEWARE = [ + # 'debug_toolbar.middleware.DebugToolbarMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', @@ -111,9 +131,6 @@ DATABASES = { } - - - DATABASE_ROUTERS = ["db_routers.DjangoRouter", "db_routers.CalibreRouter"] # Password validation @@ -147,5 +164,3 @@ USE_I18N = True USE_L10N = True USE_TZ = True - - diff --git a/CalibreWebCompanion/CalibreWebCompanion/urls.py b/CalibreWebCompanion/CalibreWebCompanion/urls.py index 813db69..b18c9e0 100644 --- a/CalibreWebCompanion/CalibreWebCompanion/urls.py +++ b/CalibreWebCompanion/CalibreWebCompanion/urls.py @@ -18,11 +18,20 @@ from django.urls import path, include from django.conf import settings from django.conf.urls.static import static from django.views.generic import RedirectView +from django.conf import settings +from django.urls import include, path + urlpatterns = [ path('admin/', admin.site.urls), path('accounts/', include('django.contrib.auth.urls')), path('', include('library.urls')), path('', RedirectView.as_view(url='books/', permanent=True)), -] -urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) \ No newline at end of file +] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + + +# if settings.DEBUG: # for DEBUGging purposes +# import debug_toolbar +# urlpatterns = [ +# path('__debug__/', include(debug_toolbar.urls)), +# ] + urlpatterns diff --git a/CalibreWebCompanion/library/templates/library/publisher_detail.html b/CalibreWebCompanion/library/templates/library/publisher_detail.html index 0e9f161..505db2a 100644 --- a/CalibreWebCompanion/library/templates/library/publisher_detail.html +++ b/CalibreWebCompanion/library/templates/library/publisher_detail.html @@ -14,11 +14,11 @@ Tags Added - {% for book in publisher.released.all %} + {% for book in books %} {{ book.title }} {{book.author_sort}} - {% for rating in book.ratings.all %} + {% for rating in book.rating.all %} {{rating}} {% endfor %} diff --git a/CalibreWebCompanion/library/templates/results.html b/CalibreWebCompanion/library/templates/results.html index 1d86dfb..849b4c6 100644 --- a/CalibreWebCompanion/library/templates/results.html +++ b/CalibreWebCompanion/library/templates/results.html @@ -13,7 +13,7 @@ Tags Added - {% for book in books_list %} + {% for book in book_list %} {{ book.title }} {{book.author_sort}} diff --git a/CalibreWebCompanion/library/views.py b/CalibreWebCompanion/library/views.py index bacd1a0..bcfaa30 100644 --- a/CalibreWebCompanion/library/views.py +++ b/CalibreWebCompanion/library/views.py @@ -5,7 +5,6 @@ from django.http import HttpResponseRedirect from .forms import SearchForm, UserCreationForm from django.db import models from django.db.models import Q -# 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 @@ -13,19 +12,20 @@ from django.contrib.auth.decorators import login_required @login_required def index(request): - return render(request,'accounts/index.html') + 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) + login(request, user) + return render(request, 'registration/index.html') + context['form'] = form + return render(request, 'registration/sign_up.html', context) + class SearchView(generic.TemplateView): template_name = 'search.html' @@ -40,9 +40,12 @@ class ResultsView(generic.ListView): # no clue if this is secure. def get_queryset(self): # new title = self.request.GET.get('title') author = self.request.GET.get('author') - return Book.objects.filter( - Q(sort__icontains=title) and Q(author_sort__icontains=author) - ) + books = Book.objects.prefetch_related("tags", "ratings") + if title: + books =books.filter(sort__icontains=title) + if author: + books = books.filter(author_sort__icontains=author) + return books class AuthorListView(generic.ListView): @@ -51,6 +54,11 @@ class AuthorListView(generic.ListView): class BookListView(generic.ListView): model = Book + def get_queryset(self): + # Annotate the books with ratings, tags, etc + # books = Book.objects.annotate( + queryset = Book.objects.prefetch_related("tags", "ratings") + return queryset class PublisherListView(generic.ListView): @@ -72,9 +80,9 @@ class AuthorDetailView(generic.DetailView): # 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( - [b.book for b in books.all()], key=lambda x: x.title) + books = Book.objects.prefetch_related("tags", "ratings") + books = books.filter(authors=context["object"].id) + context['books'] = sorted(books, key=lambda x: x.title) return context @@ -99,6 +107,15 @@ class BookDetailView(generic.DetailView): class PublisherDetailView(generic.DetailView): model = Publisher + 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 + class RatingDetailView(generic.DetailView): model = Rating @@ -107,9 +124,9 @@ class RatingDetailView(generic.DetailView): # 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) - context['books'] = sorted( - [b.book for b in books.all()], key=lambda x: x.title) + books = Book.objects.prefetch_related("tags", "ratings") + books = books.filter(ratings=context["object"].id) + context['books'] = sorted(books, key=lambda x: x.title) return context @@ -120,7 +137,7 @@ class TagDetailView(generic.DetailView): # 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) - context['books'] = sorted( - [b.book for b in books.all()], key=lambda x: x.title) + books = Book.objects.prefetch_related("tags", "ratings") + books = books.filter(tags=context["object"].id) + context['books'] = sorted(books, key=lambda x: x.title) return context