summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--website/static/website/js/cloud.js21
-rw-r--r--website/static/website/templates/index.html29
-rw-r--r--website/views.py82
3 files changed, 99 insertions, 33 deletions
diff --git a/website/static/website/js/cloud.js b/website/static/website/js/cloud.js
index 43f035f..f2f7f19 100644
--- a/website/static/website/js/cloud.js
+++ b/website/static/website/js/cloud.js
@@ -905,6 +905,21 @@ $(document.body).ready(function() {
});
});
/********************************************/
+ /********************************************/
+ /********************************************/
+ /********* search feature *******************/
+ /********************************************/
+ $(document).on("change", "input[type=radio][name=optradio]", function() {
+ if (this.value == 'bybook') {
+ $("#searched_code").hide();
+ $("#searched_book").show();
+ }
+ else if (this.value == 'bycode') {
+ $("#searched_code").show();
+ $("#searched_book").hide();
+ }
+ });
+ /********************************************/
$(document).on("click", "#search", function() {
ajax_loader(this);
$("#relevant").html('');
@@ -923,7 +938,7 @@ $(document.body).ready(function() {
$("#relevant").html('<h2>Relevant</h2><hr>');
for (var i = 0; i < data.length; i++) {
$("#relevant").append(
- '<a href="?book_id=' + data[i].ids + '" class="">' + data[i].book +
+ '<a href="?book_id=' + data[i].id + '" class="">' + data[i].book +
' (Author: ' + data[i].author + ')</a><hr>');
}
@@ -986,7 +1001,7 @@ $(document.body).ready(function() {
$("#popular").html('<h2>Popular</h2><hr>');
for (var i = 0; i < data.length; i++) {
$("#popular").append(
- '<a href="?book_id=' + data[i].ids + '" class="">' + data[i].book +
+ '<a href="?book_id=' + data[i].id + '" class="">' + data[i].book +
' (Author: ' + data[i].author + ')</a><hr>');
}
ajax_loader("clear");
@@ -1003,7 +1018,7 @@ $(document.body).ready(function() {
$("#recent").html('<h2>Recent</h2><hr>');
for (var i = 0; i < data.length; i++) {
$("#recent").append(
- '<a href="?book_id=' + data[i].ids + '" class="">' + data[i].book +
+ '<a href="?book_id=' + data[i].id + '" class="">' + data[i].book +
' (Author: ' + data[i].author + ')</a><hr>');
}
ajax_loader("clear");
diff --git a/website/static/website/templates/index.html b/website/static/website/templates/index.html
index 18dd01b..27dbca9 100644
--- a/website/static/website/templates/index.html
+++ b/website/static/website/templates/index.html
@@ -45,9 +45,9 @@
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav ml-auto">
{% if user and not user.is_anonymous %} {% if not user.is_staff %}
- <!--> <li class="nav-item">
+ <li class="nav-item">
<a class="nav-link" href="" id="search_book" data-toggle="modal" data-target="#search_wrapper">Search Book</a>
- </li> -->
+ </li>
<li class="nav-item">
<a class="nav-link" href="" id="about-us" data-toggle="modal" data-target="#about_wrapper">About</a>
</li>
@@ -69,9 +69,9 @@
<a style="color:red">Hello, {{ user.get_full_name }}!</a>
</li>
{% else %}
- <!-- > <li class="nav-item">
- <a class="nav-link" href="" id="search_book" data-toggle="modal" data-target="#search_wrapper">Search Book</a>
- </li> -->
+ <li class="nav-item">
+ <a class="nav-link" href="" id="search_book" data-toggle="modal" data-target="#search_wrapper">Search</a>
+ </li>
<li class="nav-item">
<a class="nav-link" href="" id="about-us" data-toggle="modal" data-target="#about_wrapper">About</a>
</li>
@@ -500,21 +500,34 @@
<!-- Modal Header -->
<div class="modal-header">
- <h4 class="modal-title">Search book</h4>
+ <h4 class="modal-title">Search</h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="input-group">
- <input id="search-input" type="text" class="form-control" placeholder="Enter book or author name">
+ <input id="search-input" type="text" class="form-control" placeholder="Enter book/author name or code string">
<span class="input-group-btn">
<button id="search" class="btn btn-info" type="button">Search</button>
</span>
</div>
+
+ <div class="custom-control custom-radio">
+ <label class="radio-inline"><input type="radio" value="bybook" name="optradio" checked="checked"> Search by book/author name</label>
+ <label class="radio-inline"><input type="radio" value="bycode" name="optradio"> Search by code</label>
+ </div>
</div>
<div class="container">
- <div class="row">
+ <div id="searched_book" class="row">
+ <div id="relevant" class="col-4 small">
+ </div>
+ <div id="popular" class="col-4 small bg-light text-primary" style="word-wrap: break-word;overflow:auto;">
+ </div>
+ <div id="recent" class="col-4 small bg-light text-primary" style="word-wrap: break-word;overflow:auto;">
+ </div>
+ </div>
+ <div id="searched_code" class="row">
<div id="relevant" class="col-4 small">
</div>
<div id="popular" class="col-4 small bg-light text-primary" style="word-wrap: break-word;overflow:auto;">
diff --git a/website/views.py b/website/views.py
index e554ef4..383a8d6 100644
--- a/website/views.py
+++ b/website/views.py
@@ -147,23 +147,10 @@ def index(request):
template = loader.get_template('index.html')
return HttpResponse(template.render(context, request))
elif book_id:
- books = TextbookCompanionPreference.objects\
- .db_manager('r').raw("""
- SELECT DISTINCT (loc.category_id),pe.id,
- tcbm.sub_category,loc.maincategory, pe.book as
- book,loc.category_id,tcbm.sub_category,
- pe.author as author, pe.publisher as publisher,
- pe.year as year, pe.id as pe_id, pe.edition,
- po.approval_date as approval_date
- FROM textbook_companion_preference pe LEFT JOIN
- textbook_companion_proposal po ON pe.proposal_id = po.id
- LEFT JOIN textbook_companion_book_main_subcategories
- tcbm ON pe.id = tcbm.pref_id LEFT JOIN list_of_category
- loc ON tcbm.main_category = loc.category_id WHERE
- po.proposal_status = 3 AND pe.approval_status = 1
- AND pe.id = tcbm.pref_id AND
- pe.cloud_pref_err_status = 0 AND
- pe.id=%s""", [book_id])
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_BOOK_CATEGORY_FROM_ID,
+ params=[book_id])
+ books = cursor.fetchone()
books = list(books)
if len(books) == 0:
@@ -179,9 +166,9 @@ def index(request):
template = loader.get_template('index.html')
return HttpResponse(template.render(context, request))
- books = get_books(books[0].sub_category)
- maincat_id = books[0].category_id
- subcat_id = books[0].sub_category
+ req_books = get_books(books[2])
+ maincat_id = books[0]
+ subcat_id = books[2]
request.session['maincat_id'] = maincat_id
request.session['subcategory_id'] = subcat_id
request.session['book_id'] = book_id
@@ -196,8 +183,8 @@ def index(request):
'subcatg': subcateg_all,
'maincat_id': maincat_id,
'chapters': chapters,
- 'subcategory_id': books[0].sub_category,
- 'books': books,
+ 'subcategory_id': books[2],
+ 'books': req_books,
'book_id': int(book_id),
}
@@ -375,3 +362,54 @@ def reset(request):
response = {"data": "ok"}
return HttpResponse(simplejson.dumps(response),
content_type='application/json')
+
+
+def search_book(request):
+ result = {}
+ response_dict = []
+ if request.is_ajax():
+ exact_search_string = request.GET.get('search_string')
+ search_string = "%" + exact_search_string + "%"
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_SEARCH_BOOK_SQL, [search_string, search_string,
+ str(exact_search_string),
+ str(exact_search_string)])
+ result = dictfetchall(cursor)
+ return HttpResponse(simplejson.dumps(result),
+ content_type='application/json')
+
+
+def popular(request):
+ result = {}
+ response_dict = []
+ if request.is_ajax():
+ search_string = request.GET.get('search_string')
+ search_string = "%" + search_string + "%"
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_SEARCH_POPULAR_BOOK_SQL)
+ result = dictfetchall(cursor)
+ return HttpResponse(simplejson.dumps(result),
+ content_type='application/json')
+
+
+def recent(request):
+ result = {}
+ response_dict = []
+ if request.is_ajax():
+ exact_search_string = request.GET.get('search_string')
+ search_string = "%" + exact_search_string + "%"
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_SEARCH_RECENT_BOOK_SQL)
+ result = dictfetchall(cursor)
+ return HttpResponse(simplejson.dumps(result),
+ content_type='application/json')
+
+
+def update_pref_hits(pref_id):
+ updatecount = TextbookCompanionPreferenceHits.objects.using('r')\
+ .filter(pref_id=pref_id)\
+ .update(hitcount=F('hitcount') + 1)
+ if not updatecount:
+ insertcount = TextbookCompanionPreferenceHits.objects.using('r')\
+ .get_or_create(pref_id=pref_id, hitcount=1)
+ return