add search

This commit is contained in:
MassiveAtoms 2020-07-08 22:57:30 -03:00
parent 0ab703f8b9
commit 99a74b75a9
5 changed files with 81 additions and 24 deletions

View File

@ -18,6 +18,7 @@
<div class="col-sm-2">
{% block sidebar %}
<div class="sidenav">
<a href="{% url 'search' %}">Search</a>
<a href="{% url 'books' %}">Books</a>
<button class="dropdown-btn">Authors
<i class="fa fa-caret-down"></i>

View File

@ -0,0 +1,40 @@
{% extends "base.html" %}
{% block content %}
{% load static %}
<h1>Results</h1>
<table id="books">
<tr>
<!--When a header is clicked, run the sortTable function, with a parameter, 0 for sorting by names, 1 for sorting by country:-->
<th onclick="sortTable(0)">Title</th>
<th onclick="sortTable(1)">Author</th>
<th onclick="sortTable(2)">Rating</th>
<th onclick="sortTable(3)">Tags</th>
<th onclick="sortTable(4)">Added</th>
</tr>
{% for book in books_list %}
<tr>
<td><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></td>
<td>{{book.author_sort}}</td>
<td> {% for rating in book.ratings.all %}
{{rating}}
{% endfor %}
</td>
<td>
{% for tag in book.tags.all %}
{{tag}},
{% endfor %}
</td>
<td>{{book.timestamp}}</td>
</tr>
{% endfor %}
</table>
{% endblock %}

View File

@ -0,0 +1,15 @@
{% extends "base.html" %}
{% block content %}
{% load static %}
<form action="{% url 'results' %}" method="get">
<label for="title">Title: </label>
<input id="title" type="text" name="title" value="">
<label for="author">Author: </label>
<input id="author" type="text" name="author" value="">
<input type="submit" value="search">
</form>
{% endblock %}

View File

@ -16,5 +16,8 @@ urlpatterns = [
path('rating/<int:pk>', views.RatingDetailView.as_view(), name='rating-detail-view'),
path('tag/<int:pk>', views.TagDetailView.as_view(), name='tag-detail-view'),
path('results/', views.ResultsView.as_view(), name='results'),
path('search/', views.SearchView.as_view(), name='search'),
]

View File

@ -2,29 +2,24 @@ from django.shortcuts import render
from django.views import generic
from .models import Authors, Books, Comments, Ratings, BooksAuthorsLink, Publishers, Tags, BooksTagsLink, BooksRatingsLink, Data
from django.http import HttpResponseRedirect
# from .forms import SearchForms
from .forms import SearchForm
from django.db import models
from django.db.models import Q
class SearchView(generic.TemplateView):
template_name = 'search.html'
# def get_results(request): # TODO this might not be what i want
# # if this is a POST request we need to process the form data
# if request.method == 'POST':
# # create a form instance and populate it with data from the request:
# form = SearchForm(request.POST)
# # check whether it's valid:
# if form.is_valid():
# books = Books.objects.all()
# if form.title:
# books.filter(sort_icontains=form.title)
# if form.author:
# books.filter(author_sort_icontains=form.author)
# return HttpResponseRedirect('/results/')
# # if a GET (or any other method) we'll create a blank form
# else:
# form = NameForm()
# return render(request, 'name.html', {'form': form})
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
# it is
model = Books
template_name = 'results.html'
def get_queryset(self): # new
title = self.request.GET.get('title')
author = self.request.GET.get('author')
return Books.objects.filter(
Q(sort__icontains=title) and Q(author_sort__icontains=author)
)
class AuthorListView(generic.ListView):
model = Authors
@ -54,7 +49,8 @@ class AuthorDetailView(generic.DetailView):
context = super(AuthorDetailView, self).get_context_data(**kwargs)
# Create any data and add it to the context
books = BooksAuthorsLink.objects.filter(author=context["object"].id)
context['books'] = context['books'] = sorted([b.book for b in books.all()], key=lambda x: x.title)
context['books'] = context['books'] = sorted(
[b.book for b in books.all()], key=lambda x: x.title)
return context
@ -88,7 +84,8 @@ class RatingDetailView(generic.DetailView):
context = super(RatingDetailView, self).get_context_data(**kwargs)
# Create any data and add it to the context
books = BooksRatingsLink.objects.filter(rating=context["object"].id)
context['books'] = sorted([b.book for b in books.all()], key=lambda x: x.title)
context['books'] = sorted(
[b.book for b in books.all()], key=lambda x: x.title)
return context
@ -100,5 +97,6 @@ class TagDetailView(generic.DetailView):
context = super(TagDetailView, self).get_context_data(**kwargs)
# Create any data and add it to the context
books = BooksTagsLink.objects.filter(tag=context["object"].id)
context['books'] = sorted([b.book for b in books.all()], key=lambda x: x.title)
context['books'] = sorted(
[b.book for b in books.all()], key=lambda x: x.title)
return context