Refactoring template views, working towards session data, & clean up of

views
This commit is contained in:
Raelon Masters
2020-06-30 22:44:26 -04:00
parent 059d308a0f
commit a2fdca5a7c
6 changed files with 86 additions and 34 deletions

View File

@@ -21,20 +21,17 @@ from interface import views
urlpatterns = [
path("admin/", admin.site.urls),
path("", views.index, name="index"),
path("sort/<_order>", views.index, name="index"),
path("download/<pk>", views.download, name="download"),
path("favorite/<pk>", views.favorite, name="favorite"),
path("share/<pk>", views.share, name="share"),
path("share/<pk>", views.info, name="info"),
path("prev_page/<bookset>", views.prev_page, name="prev_page"),
path("next_page/<bookset>", views.next_page, name="next_page"),
path("search/", views.search, name="search"),
path("search/<query>", views.search, name="search"),
path("search/<query>/<_set>", views.search, name="search"),
path(
"show_collection/<_collection>/<_colset>",
views.show_collection,
name="show_collection",
),
path("search/", views.index, name="search"),
path("search/<query>", views.index, name="search"),
path("search/<query>/<_set>", views.index, name="search"),
path("show_collection/<_collection>/<_colset>", views.show_collection, name="show_collection",),
]
if settings.DEBUG:
import debug_toolbar

View File

@@ -376,7 +376,7 @@ body {
display: flex;
align-items: center;
justify-content: flex-start;
padding: 0px 0px 0px 5px;
padding: 0px 5px 0px 5px;
font-size: medium;
border-bottom: 1px solid;
background-color: #fefefeb3;

View File

@@ -388,7 +388,7 @@ body {
display: flex;
align-items: center;
justify-content: flex-start;
padding: 0px 0px 0px 5px;
padding: 0px 5px 0px 5px;
font-size: medium;
border-bottom: 1px solid;
background-color: $ui-color-1;

View File

@@ -74,4 +74,10 @@ $(document).ready(function(){
$('#btn_login').on('click', function(){
$('#hdr_nav_login').toggle();
});
$('#sortlist').change(function () {
var optionSelected = $(this).find("option:selected");
var valueSelected = optionSelected.val();
var textSelected = optionSelected.text();
window.location.href=valueSelected
});
});

View File

@@ -23,6 +23,8 @@
<body>
<input type="hidden" id="_set" name="_set" value="{{ Set }}" />
<input type="hidden" id="_search" name="_search" value="{{ PostedSearch }}" />
<input type="hidden" id="_order" name="_order" value="{{ Order }}" />
<div id="pop_over_0">
<!-- Login Form -->
<div class="hdr_nav" id="hdr_nav_login">
@@ -37,7 +39,7 @@
<h1 class="app_hdr" id="hdr_branding">pyShelf {{Version}}</h1>
<div class="hdr_nav" id="hdr_nav_center">
<ul id="tab_nav_menu">
<li class="nav_menu_tab active_tab"><i class="fas fa-home"></i>&nbsp;Home</li>
<li class="nav_menu_tab active_tab" onclick="window.location.href = '/'"><i class="fas fa-home"></i>&nbsp;Home</li>
<li class="nav_menu_tab"><i class="fas fa-layer-group"></i>&nbsp;Collections</li>
<li class="nav_menu_tab"><i class="fas fa-star"></i>&nbsp;Favorites</li>
<li class="nav_menu_tab"><i class="fas fa-bug"></i>&nbsp;Bug report</li>
@@ -45,14 +47,14 @@
</div>
</div>
<div id="horiz_nav_main">
<div id="horiz_nav_left">
<div id="horiz_nav_left">
<i class="fas fa-arrow-circle-left nav_icon prev_page" onclick="window.location.href = '/prev_page/{{ Set }}'"></i>
<i class="fas fa-sort nav_icon"></i>
<select id = "sortlist">
<option value = "1">Title</option>
<option value = "2">Author</option>
<option value = "3">Collection</option>
<option value = "4">Tags</option>
<option value = "sort/title" id="title-sort">Title</option>
<option value = "sort/author" id="author-sort">Author</option>
<option value = "sort/collection" id="collection-sort">Collection</option>
<option value = "sort/tag" id="tag-sort">Tags</option>
</select>
</div>
<div id="horiz_nav_center">

View File

@@ -15,26 +15,17 @@ from .models import Books, Collections, Navigation
config = Config(Path("../"))
collections = Collections.objects.all()
def index(request):
def index(request, query=None, _set=1, _limit=None, _order='title'):
"""
Return template index
TODO: revise the way sets are handled so that they can be used uniformly in js
for ui page displays
"""
_set = 1
_payload = payload(query, _set, _limit, _order)
return render(
request,
"index.html",
{
"Books": book_set(20, _set),
"Set": str(_set),
"Version": config.VERSION,
"LeftNavCollections": menu("collections"),
"LeftNavMenu0": menu("nav_l_0"),
"BookStats": Books.objects.all().count,
"CollectionStats": Collections.objects.all().count,
"CollectionObject": collections_list()
},
_payload
)
def show_collection(request, _collection, _colset):
@@ -69,7 +60,8 @@ def next_page(request, bookset):
_set = 1
return render(
request,
"index.html",
"""
index.html",
{
"Books": book_set(None, _set),
"Set": str(_set),
@@ -84,6 +76,7 @@ def next_page(request, bookset):
"LeftNavCollections": menu("collections"),
"LeftNav": menu("collections"),
},
"""
)
def prev_page(request, bookset):
@@ -123,7 +116,20 @@ def search(request, query=None, _set=1, _limit=None):
"""
_set = int(_set)
if query is None:
return render(request, "index.html", {"Books": None, "Version": config.VERSION})
return render(
request,
"index.html",
{
"Books": book_set(20, _set),
"Set": str(_set),
"Version": config.VERSION,
"LeftNavCollections": menu("collections"),
"LeftNavMenu0": menu("nav_l_0"),
"BookStats": Books.objects.all().count,
"CollectionStats": Collections.objects.all().count,
"CollectionObject": collections_list()
}
)
if _limit is None:
_limit = 20 ## TODO set to user defaults
if _set < 1:
@@ -147,7 +153,7 @@ def search(request, query=None, _set=1, _limit=None):
},
)
def book_set(_limit=None, _set=1):
def book_set(_order, _limit=None, _set=1):
"""
Get books results by set #
"""
@@ -155,7 +161,7 @@ def book_set(_limit=None, _set=1):
_limit = 20 # TODO default from user choice
_set_max = int(_set) * _limit
_set_min = _set_max - _limit
books = Books.objects.all()[_set_min:_set_max]
books = Books.objects.all().order_by(_order)[_set_min:_set_max]
return books
def collection(_collection, _set, _limit=None):
@@ -297,3 +303,44 @@ def collections_list():
if i.collection not in collection_key:
collection_key.append(i.collection)
return json.dumps(list(set(collection_key)))
def payload(query, _set, _limit, _order):
"""
Return formatted data to template
# TODO hook into payload and provide handling of next,prev, & search combos
"""
_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 of %s"%(_set_min, _set_max)
if query:
_results = Books().generic_search(query)
_r_len = _results.count()
_r, _search = _results[_set_min:_set_max], query
else: _r, _r_len, _search = book_set(_order, _limit, _set), None, None
_bookstats, _collectionstats, _collectionobject = \
Books.objects.all().count, Collections.objects.all().count, \
collections_list()
if (_r_len): _btotal = _r_len
else: _btotal = _bookstats
return {
"Books": _r,
"Set": str(_set),
"Version": config.VERSION,
"LeftNavCollections": menu("collections"),
"LeftNavMenu0": menu("nav_l_0"),
"BookStats": _btotal,
"CollectionStats": _collectionstats,
"CollectionObject": _collectionobject,
"NowShowing": _now_showing,
"PostedSearch": _search,
"SearchLen": _r_len,
"Order": _order
}