Added Ajax handler for book info call

This commit is contained in:
Raelon Masters
2020-07-31 02:27:07 -04:00
parent be2c525d9a
commit 5e36fc1d16
3 changed files with 72 additions and 31 deletions

View File

@@ -1,22 +1,22 @@
import json
import os
from base64 import b64decode, b64encode
from pathlib import Path
from backend.lib.config import Config
from django.conf import settings
from django.contrib import auth
from django.contrib.auth import authenticate, get_user_model, login, logout
from django.contrib.auth.models import User
from django.db import models
from django.http import JsonResponse
from django.shortcuts import HttpResponse, render, redirect # render_to_response
from django.utils.text import slugify
from django.contrib.auth import login, authenticate, logout
from django.contrib import auth
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.auth import get_user_model
from django.utils.datastructures import MultiValueDictKeyError
import json
from .forms import SignUpForm, UserLoginForm
from .models import Books, Collections, Navigation, Favorites, User
from django.shortcuts import HttpResponse, redirect, render # render_to_response
from django.template.loader import render_to_string
from django.utils.datastructures import MultiValueDictKeyError
from django.utils.text import slugify
from .forms import SignUpForm, UserLoginForm
from .models import Books, Collections, Favorites, Navigation, User
config = Config(Path("../"))
@@ -380,6 +380,12 @@ def collections_list():
def live(request, **kwargs):
"""
Respond to live requests. Primarily used as a response object for Ajax calls
:param GET['hook']: collection_listing, book_details, register
:param kwargs['pk']: Primary key of requested object
:return: JsonResponse Object, status response code
"""
err_txt = {"err": "There is no responder for your request"}
try: hook = request.GET['hook']
except MultiValueDictKeyError as e: return JsonResponse(err_txt, status=404)
@@ -445,10 +451,10 @@ def payload(request, query, _set, _limit, _order, **kwargs):
_r, _r_len = \
_results.order_by(_order)[_set_min:_set_max],\
_results.count()
else:
else: # No new query was passed
try:
query = request.session['cached_query'] # Is there a cached query?
if query == None: raise KeyError
if query == None: raise KeyError # No cached query exists jump to KeyError
if request.session['ascending']:
_results = Books().generic_search(query)
else: _results = Books().generic_search(query).reverse()
@@ -464,7 +470,7 @@ def payload(request, query, _set, _limit, _order, **kwargs):
_bookstats = Books.objects.all().count()
if (_r_len): _btotal = str(_r_len)
else: _btotal = str(_bookstats)
# Format the payload and return it to the view
return {
"Books": _r,
"Set": str(_set),
@@ -479,4 +485,3 @@ def payload(request, query, _set, _limit, _order, **kwargs):
"SearchLen": _r_len,
"Order": _order,
}