mirror of
https://github.com/th3r00t/pyShelf.git
synced 2026-04-28 01:59:35 -04:00
show_collections now prepares its own payload relevant to the collection set
This commit is contained in:
@@ -12,13 +12,13 @@ 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
|
||||
|
||||
config = Config(Path("../"))
|
||||
|
||||
collections = Collections.objects.all()
|
||||
|
||||
|
||||
def index(request, query=None, _set=1, _limit=None, _order='title'):
|
||||
@@ -37,7 +37,7 @@ def favorites(request, query=None, _set=1, _limit=None, _order='title'):
|
||||
"""
|
||||
Return template index
|
||||
"""
|
||||
_payload = payload(request, query, _set, _limit, _order, favorites=True)
|
||||
_payload = payload(request, query, _set, _limit, _order, favorites=True, reset=True)
|
||||
return render(
|
||||
request,
|
||||
"index.html",
|
||||
@@ -45,6 +45,11 @@ def favorites(request, query=None, _set=1, _limit=None, _order='title'):
|
||||
)
|
||||
|
||||
|
||||
def collectionspage(request):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def signup(request):
|
||||
if request.method == 'POST':
|
||||
form = SignUpForm(request.POST)
|
||||
@@ -83,7 +88,7 @@ def home(request, query=None, _set=1, _limit=None, _order='title'):
|
||||
"""
|
||||
Reset Search Queries & Return Home
|
||||
"""
|
||||
_payload = payload(request, query, _set, _limit, _order, reset='1')
|
||||
_payload = payload(request, query, _set, _limit, _order, reset=True)
|
||||
return render(
|
||||
request,
|
||||
"index.html",
|
||||
@@ -91,27 +96,71 @@ def home(request, query=None, _set=1, _limit=None, _order='title'):
|
||||
)
|
||||
|
||||
|
||||
def show_collection(request, _collection, _colset):
|
||||
def show_collection(request, query, _set, _limit=None, _order='title', **kwargs):
|
||||
try:
|
||||
_set = int(_colset) + 1
|
||||
_set = int(_set) + 1
|
||||
except Exception:
|
||||
_set = 1
|
||||
|
||||
_set = int(_set) # Get set #
|
||||
if _set < 1: _set = 1 # If there is no set data, set = 1
|
||||
if _limit is None: _limit = 20 # If we havent set limit limit = 20
|
||||
_set_max = int(_set) * _limit # Multiply current set by limit to get upper limits
|
||||
_set_min = _set_max - _limit # Subtract limiter to get bottom limit
|
||||
_now_showing = "%s-%s"%(_set_min, _set_max) # Set the set count
|
||||
if query: # Are we sending a query?
|
||||
if query != request.session.get('cached_query'): # Is it differnt to the last query?
|
||||
request.session['cached_query'] = query # Set the session data to track the query
|
||||
if request.session['ascending']:
|
||||
_r = _results = Collections().generic_search(query) # Get the query, or in reverse below
|
||||
else: _results = Collections().generic_search(query).reverse()
|
||||
_r, _r_len = \
|
||||
_results[_set_min:_set_max],\
|
||||
len(_results)
|
||||
elif query == request.session.get('cached_query'): # The queries are equal check the direction
|
||||
if request.session['ascending']:
|
||||
_results = Collections().generic_search(query)
|
||||
else: _results = Collections().generic_search(query).reverse()
|
||||
_r, _r_len = \
|
||||
_results.order_by(_order)[_set_min:_set_max],\
|
||||
len(_results)
|
||||
else:
|
||||
try:
|
||||
query = request.session['cached_query'] # Is there a cached query?
|
||||
if query == None: raise KeyError
|
||||
if request.session['ascending']:
|
||||
_results = Collections().generic_search(query)
|
||||
else: _results = Collections().generic_search(query).reverse()
|
||||
_r, _r_len = \
|
||||
_results.order_by(_order)[_set_min:_set_max],\
|
||||
len(_results)
|
||||
except KeyError:
|
||||
if request.session['ascending']:
|
||||
_r = book_set(request, _order, _limit, _set, False, **kwargs)
|
||||
else: _r = book_set(request, _order, _limit, _set, True, **kwargs)
|
||||
_r_len, _search = None, None
|
||||
|
||||
_bookstats = len(Collections().generic_search(query))
|
||||
if (_r_len): _btotal = str(_r_len)
|
||||
else: _btotal = str(_bookstats)
|
||||
|
||||
return render(
|
||||
request,
|
||||
"index.html",
|
||||
request,
|
||||
"index.html",
|
||||
{
|
||||
"Books": collection(_collection, _set),
|
||||
"Books": _results,
|
||||
"Set": str(_set),
|
||||
"Version": config.VERSION,
|
||||
"LeftNavCollections": menu("collections"),
|
||||
"LeftNav": menu("collections"),
|
||||
"Collections": collections_list(),
|
||||
"LeftNavMenu0": menu("nav_l_0"),
|
||||
"BookStats": Books.objects.all().count,
|
||||
"CollectionStats": Collections.objects.all().count,
|
||||
"CollectionObject": collections_list()
|
||||
},
|
||||
)
|
||||
# "LeftNavCollections": menu("collections"),
|
||||
# "LeftNavMenu0": menu("nav_l_0"),
|
||||
"BookStats": _btotal,
|
||||
"CollectionStats": Collections.objects.all().count(),
|
||||
"CollectionObject": collections_list,
|
||||
"NowShowing": _now_showing,
|
||||
"PostedSearch": query,
|
||||
"SearchLen": _r_len,
|
||||
"Order": _order,
|
||||
})
|
||||
|
||||
|
||||
def flip_sort(request, bookset=1, query=None, _limit=None, _order='title'):
|
||||
@@ -183,18 +232,22 @@ def book_set(request, _order, _limit=None, _set=1, _flip=False, **kwargs):
|
||||
books = BookObject.order_by(_order).reverse()[_set_min:_set_max]
|
||||
else:
|
||||
books = BookObject.order_by(_order)[_set_min:_set_max]
|
||||
return mark_favorites(request, books)
|
||||
|
||||
|
||||
def mark_favorites(request, bookset):
|
||||
try:
|
||||
favorites = Favorites.objects.filter(user=request.user)
|
||||
for book in books:
|
||||
for book in bookset:
|
||||
for favorite in favorites:
|
||||
if book == favorite.book:
|
||||
book.is_favorite = True
|
||||
pass
|
||||
return books
|
||||
return bookset
|
||||
except Exception as e:
|
||||
for book in books:
|
||||
for book in bookset:
|
||||
book.if_favorite = False
|
||||
return books
|
||||
return bookset
|
||||
|
||||
|
||||
def collection(_collection, _set, _limit=None):
|
||||
@@ -213,26 +266,6 @@ def collection(_collection, _set, _limit=None):
|
||||
return Books.objects.filter(id__in=_books)
|
||||
|
||||
|
||||
def book_set_as_dict(_limit=None, _set=1):
|
||||
if _limit is None:
|
||||
_limit = 20
|
||||
_set_max = int(_set) * _limit
|
||||
_set_min = _set_max - _limit
|
||||
_set = {}
|
||||
for book in Books.objects.all()[_set_min:_set_max]:
|
||||
_set[book.title] = {
|
||||
"title": book.title,
|
||||
"author": book.author,
|
||||
"categories": book.categories,
|
||||
"cover": book.cover,
|
||||
"pages": book.pages,
|
||||
"progress": book.progress,
|
||||
"file_name": book.file_name,
|
||||
"pk": book.pk,
|
||||
}
|
||||
return json.dumps(_set)
|
||||
|
||||
|
||||
def download(request, pk):
|
||||
"""
|
||||
Download book by primary key
|
||||
@@ -312,20 +345,14 @@ def format_list(list_in):
|
||||
|
||||
def menu(which, _set=1, parent=None):
|
||||
if which == "collections":
|
||||
collection_list = collections
|
||||
_collections_list = Collections.objects.all()
|
||||
_collections, collection_key, x = [], [], 0
|
||||
for i in collection_list:
|
||||
for i in _collections_list:
|
||||
if i.collection not in collection_key:
|
||||
# Using c as the alternating row identifier
|
||||
# set c here
|
||||
if x % 2 == 0:
|
||||
c = 0
|
||||
else:
|
||||
c = 1
|
||||
if x <= 10:
|
||||
x = x + 1
|
||||
else:
|
||||
x = 0
|
||||
if x % 2 == 0: c = 0
|
||||
else: c = 1
|
||||
if x <= 10: x = x + 1
|
||||
else: x = 0
|
||||
# TODO trim #'s and symbols from front of collection name
|
||||
if len(i.collection) > 16:
|
||||
collection_string = i.collection[0:16] + " ..."
|
||||
@@ -343,11 +370,27 @@ def menu(which, _set=1, parent=None):
|
||||
|
||||
|
||||
def collections_list():
|
||||
collections = Collections.objects.all()
|
||||
collection_key = []
|
||||
for i in collections:
|
||||
if i.collection not in collection_key:
|
||||
collection_key.append(i.collection)
|
||||
return json.dumps(list(set(collection_key)))
|
||||
return list(set(collection_key))
|
||||
|
||||
|
||||
def live(request, **kwargs):
|
||||
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)
|
||||
|
||||
if hook == "collection_listing":
|
||||
collections = collections_list()
|
||||
return JsonResponse({"data": collections}, status=200)
|
||||
elif hook == "book_details":
|
||||
return JsonResponse({"data": Books.objects.get(pk=kwargs['pk'])}, status=200)
|
||||
else: return JsonResponse(err_txt, status=404)
|
||||
|
||||
return JsonResponse({"data": "Response sent"}, status=200)
|
||||
|
||||
|
||||
def payload(request, query, _set, _limit, _order, **kwargs):
|
||||
@@ -358,11 +401,11 @@ def payload(request, query, _set, _limit, _order, **kwargs):
|
||||
"""
|
||||
try: request.session['ascending']
|
||||
except KeyError: request.session['ascending'] = True
|
||||
try:
|
||||
try: # Are we fliping the sort?
|
||||
if kwargs['flip_sort']:
|
||||
request.session['ascending'] = not request.session['ascending']
|
||||
except KeyError: pass
|
||||
try:
|
||||
try: # Are we resetting the session?
|
||||
if kwargs['reset']:
|
||||
request.session['cached_query'] = query
|
||||
if _set < 1: _set = 1
|
||||
@@ -375,22 +418,22 @@ def payload(request, query, _set, _limit, _order, **kwargs):
|
||||
else: _r = book_set(request, _order, _limit, _set, True, **kwargs)
|
||||
_r_len, _search = None, None
|
||||
except KeyError:
|
||||
_set = int(_set)
|
||||
if _set < 1: _set = 1
|
||||
if _limit is None: _limit = 20
|
||||
_set_max = int(_set) * _limit
|
||||
_set_min = _set_max - _limit
|
||||
_now_showing = "%s-%s"%(_set_min, _set_max)
|
||||
if query:
|
||||
if query != request.session.get('cached_query'):
|
||||
request.session['cached_query'] = query
|
||||
_set = int(_set) # Get set #
|
||||
if _set < 1: _set = 1 # If there is no set data, set = 1
|
||||
if _limit is None: _limit = 20 # If we havent set limit limit = 20
|
||||
_set_max = int(_set) * _limit # Multiply current set by limit to get upper limits
|
||||
_set_min = _set_max - _limit # Subtract limiter to get bottom limit
|
||||
_now_showing = "%s-%s"%(_set_min, _set_max) # Set the set count
|
||||
if query: # Are we sending a query?
|
||||
if query != request.session.get('cached_query'): # Is it differnt to the last query?
|
||||
request.session['cached_query'] = query # Set the session data to track the query
|
||||
if request.session['ascending']:
|
||||
_results = Books().generic_search(query)
|
||||
_results = Books().generic_search(query) # Get the query, or in reverse below
|
||||
else: _results = Books().generic_search(query).reverse()
|
||||
_r, _r_len = \
|
||||
_results[_set_min:_set_max],\
|
||||
_results.count()
|
||||
elif query == request.session.get('cached_query'):
|
||||
elif query == request.session.get('cached_query'): # The queries are equal check the direction
|
||||
if request.session['ascending']:
|
||||
_results = Books().generic_search(query)
|
||||
else: _results = Books().generic_search(query).reverse()
|
||||
@@ -413,9 +456,7 @@ def payload(request, query, _set, _limit, _order, **kwargs):
|
||||
else: _r = book_set(request, _order, _limit, _set, True, **kwargs)
|
||||
_r_len, _search = None, None
|
||||
|
||||
_bookstats, _collectionstats, _collectionobject = \
|
||||
Books.objects.all().count(), Collections.objects.all().count(), \
|
||||
collections_list()
|
||||
_bookstats = Books.objects.all().count()
|
||||
if (_r_len): _btotal = str(_r_len)
|
||||
else: _btotal = str(_bookstats)
|
||||
|
||||
@@ -423,11 +464,11 @@ def payload(request, query, _set, _limit, _order, **kwargs):
|
||||
"Books": _r,
|
||||
"Set": str(_set),
|
||||
"Version": config.VERSION,
|
||||
"LeftNavCollections": menu("collections"),
|
||||
"LeftNavMenu0": menu("nav_l_0"),
|
||||
# "LeftNavCollections": menu("collections"),
|
||||
# "LeftNavMenu0": menu("nav_l_0"),
|
||||
"BookStats": _btotal,
|
||||
"CollectionStats": _collectionstats,
|
||||
"CollectionObject": _collectionobject,
|
||||
"CollectionStats": Collections.objects.all().count(),
|
||||
"CollectionObject": collections_list,
|
||||
"NowShowing": _now_showing,
|
||||
"PostedSearch": query,
|
||||
"SearchLen": _r_len,
|
||||
|
||||
Reference in New Issue
Block a user