optimized queries

This commit is contained in:
MassiveAtoms
2020-07-09 11:58:09 -03:00
parent caa3022cdf
commit 7d2b429cbe
5 changed files with 75 additions and 34 deletions

View File

@ -14,11 +14,11 @@
<th onclick="sortTable(3)">Tags</th>
<th onclick="sortTable(4)">Added</th>
</tr>
{% for book in publisher.released.all %}
{% for book in books %}
<tr>
<td><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></td>
<td>{{book.author_sort}}</td>
<td> {% for rating in book.ratings.all %}
<td> {% for rating in book.rating.all %}
{{rating}}
{% endfor %}
</td>

View File

@ -13,7 +13,7 @@
<th onclick="sortTable(3)">Tags</th>
<th onclick="sortTable(4)">Added</th>
</tr>
{% for book in books_list %}
{% for book in book_list %}
<tr>
<td><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></td>
<td>{{book.author_sort}}</td>

View File

@ -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