summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorprashantsinalkar2020-01-21 17:01:33 +0530
committerprashantsinalkar2020-01-21 17:02:43 +0530
commitb28d11ad794c3ed438e42a232dc1bd42ab1691ff (patch)
tree67cdadfa8003f50cec54e9f0dbf52f6658775adb
parent01be180d3cba129a5431745937615c09d183a5b8 (diff)
downloadR_on_Cloud_Web_Interface-b28d11ad794c3ed438e42a232dc1bd42ab1691ff.tar.gz
R_on_Cloud_Web_Interface-b28d11ad794c3ed438e42a232dc1bd42ab1691ff.tar.bz2
R_on_Cloud_Web_Interface-b28d11ad794c3ed438e42a232dc1bd42ab1691ff.zip
added sorl code search for cloud
-rw-r--r--website/static/website/js/cloud.js51
-rw-r--r--website/static/website/templates/index.html14
-rw-r--r--website/static/website/templates/search_code.html28
-rw-r--r--website/urls.py2
-rw-r--r--website/views.py61
5 files changed, 132 insertions, 24 deletions
diff --git a/website/static/website/js/cloud.js b/website/static/website/js/cloud.js
index f2f7f19..9a632e7 100644
--- a/website/static/website/js/cloud.js
+++ b/website/static/website/js/cloud.js
@@ -922,8 +922,10 @@ $(document.body).ready(function() {
/********************************************/
$(document).on("click", "#search", function() {
ajax_loader(this);
- $("#relevant").html('');
var search_string = jQuery.trim($("#search-input").val());
+ if(search_string != ''){
+ if($("input[name='optradio']:checked").val() == 'bybook'){
+ $("#relevant").html('');
if (search_string == '') {
search_string = 'Null';
}
@@ -935,6 +937,7 @@ $(document.body).ready(function() {
search_string: search_string,
},
success: function(data) {
+ if(data.length > 0 ){
$("#relevant").html('<h2>Relevant</h2><hr>');
for (var i = 0; i < data.length; i++) {
$("#relevant").append(
@@ -942,6 +945,9 @@ $(document.body).ready(function() {
' (Author: ' + data[i].author + ')</a><hr>');
}
+ }else{
+ $("#relevant").append('Oops! This book is not availabe!');
+ }
ajax_loader("clear");
}
}),
@@ -961,7 +967,6 @@ $(document.body).ready(function() {
}
ajax_loader("clear");
}
-
}),
$.ajax({
url: 'search_book/recent/',
@@ -977,9 +982,33 @@ $(document.body).ready(function() {
'<a href="?book_id=' + data[i].ids + '" class="">' + data[i].book +
' (Author: ' + data[i].author + ')</a><hr>');
}
- ajax_loader("clear");
}
});
+ } else{
+ ajax_loader(this);
+ $.ajax({
+ url: 'search_in_code/',
+ dataType: 'JSON',
+ type: 'GET',
+ data: {
+ search_string: search_string,
+ },
+ success: function(data) {
+ if(data.error != 'True'){
+ console.log(data.data);
+ $("#searched_code_data").html("");
+ $("#searched_code").html(data.data);
+ }else{
+ alert("Oops! Search engine is away, please try after some time!")
+ }
+ }
+ });
+ ajax_loader("clear");
+ }
+ }else{
+ alert("Please enter the search string!");
+ }
+ ajax_loader("clear");
});
$(document).on("click", "#search_book", function(e) {
@@ -1083,14 +1112,14 @@ function doSubmit(){
}
function checkserver(){
-
- $.ajax({url: api_url,
- dataType: "jsonp",
- statusCode: {
- 404: function (response) {
- alert("Oops! R cloud server is not available, please try after some time");
- }
- }
+ $.ajax({url: 'check_server/',
+ dataType: "json",
+ success : function (data) {
+ if((data.status == 200) || (data.error != 'True')){
+ }else{
+ alert("R cloud server is not available, please try after some time");
+ }
+ }
});
}
diff --git a/website/static/website/templates/index.html b/website/static/website/templates/index.html
index 27dbca9..6d68515 100644
--- a/website/static/website/templates/index.html
+++ b/website/static/website/templates/index.html
@@ -46,7 +46,7 @@
<ul class="navbar-nav ml-auto">
{% if user and not user.is_anonymous %} {% if not user.is_staff %}
<li class="nav-item">
- <a class="nav-link" href="" id="search_book" data-toggle="modal" data-target="#search_wrapper">Search Book</a>
+ <a class="nav-link" href="" id="search_book" data-toggle="modal" data-target="#search_wrapper"><i class="fa fa-search" aria-hidden="true"></i>Search Book</a>
</li>
<li class="nav-item">
<a class="nav-link" href="" id="about-us" data-toggle="modal" data-target="#about_wrapper">About</a>
@@ -70,7 +70,7 @@
</li>
{% else %}
<li class="nav-item">
- <a class="nav-link" href="" id="search_book" data-toggle="modal" data-target="#search_wrapper">Search</a>
+ <a class="nav-link" href="" id="search_book" data-toggle="modal" data-target="#search_wrapper"><i class="fa fa-search text-primary" aria-hidden="true"></i>Search</a>
</li>
<li class="nav-item">
<a class="nav-link" href="" id="about-us" data-toggle="modal" data-target="#about_wrapper">About</a>
@@ -514,8 +514,8 @@
</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>
+ <label class="radio-inline"><input id="bybook" type="radio" value="bybook" name="optradio" checked="checked"> <i class="fa fa-search" aria-hidden="true"></i>Search by book/author name <i class="fa fa-book" aria-hidden="true"></i></label>
+ <label class="radio-inline"><input id="bycode" type="radio" value="bycode" name="optradio"> <i class="fa fa-search" aria-hidden="true"></i>Search by code</label>
</div>
</div>
<div class="container">
@@ -528,12 +528,6 @@
</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;">
- </div>
- <div id="recent" class="col-4 small bg-light text-primary" style="word-wrap: break-word;overflow:auto;">
- </div>
</div>
</div>
<!-- Modal footer -->
diff --git a/website/static/website/templates/search_code.html b/website/static/website/templates/search_code.html
new file mode 100644
index 0000000..f5d082f
--- /dev/null
+++ b/website/static/website/templates/search_code.html
@@ -0,0 +1,28 @@
+<span class="badge badge-info">Search results count {{ data|length }}</span>
+<div id="searched_code_data" class="col-12 bg-light text-success" style="word-wrap: break-word;overflow:auto;">
+
+{% for result in data %}
+<div><span class="badge badge-info">{{ forloop.counter }}</span>
+ <i class="fa fa-book" aria-hidden="true"></i>
+ Book: <span class="text-info">{{ result.book }}</span></div>
+<div><i class="fa fa-file" aria-hidden="true"></i>
+ Chapter: <span class="text-info">{{result.chapter.0 }}</span></div>
+<div><i class="fa fa-bars" aria-hidden="true"></i>
+ Example: <span class="text-info">{{ result.example.0 }}</span></div>
+<div><i class="fa fa-external-link" aria-hidden="true"></i>
+ Links: <span class="text-success">
+ <a href="https://r.fossee.in/textbook-companion/download/book/{{ result.book_id.0 }}" target="_blank">Download book
+ <i class="fa fa-download" aria-hidden="true"></i>
+ </a>
+ </span> <i class="fa fa-expand" aria-hidden="true"></i>
+
+ <span class="text-success">
+ <a href="http://rcloud.fossee.in/index?eid={{ result.example_id }}" target="_blank"> View this example
+ <i class="fa fa-cloud" aria-hidden="true"></i>
+ </a>
+ </span>
+</div>
+<hr>
+{% endfor %}
+
+</div>
diff --git a/website/urls.py b/website/urls.py
index ba1d17b..5a13bb4 100644
--- a/website/urls.py
+++ b/website/urls.py
@@ -21,4 +21,6 @@ urlpatterns = [
path('search_book/', views.search_book, name='search_book'),
path('search_book/popular/', views.popular, name='popular'),
path('search_book/recent/', views.recent, name='recent'),
+ path('search_in_code/', views.solr_search_string, name='solr_search_string'),
+ path('check_server/', views.checkserver, name='checkserver'),
]
diff --git a/website/views.py b/website/views.py
index 383a8d6..b449b27 100644
--- a/website/views.py
+++ b/website/views.py
@@ -1,10 +1,10 @@
from django.shortcuts import render, redirect
-from django.http import HttpResponse
-from django.template import loader
+from django.http import HttpResponse, JsonResponse
+from django.template import loader, Context, Template
import requests
import uuid
from R_on_Cloud.config import (API_URL_UPLOAD, API_URL_RESET, AUTH_KEY,
- API_URL_SERVER)
+ API_URL_SERVER, URL)
from website.models import *
from django.db.models import Q
import json as simplejson
@@ -12,6 +12,8 @@ from . import utils
from django.db import connections
from collections import defaultdict
from .query import *
+import pysolr
+import json
def dictfetchall(cursor):
@@ -413,3 +415,56 @@ def update_pref_hits(pref_id):
insertcount = TextbookCompanionPreferenceHits.objects.using('r')\
.get_or_create(pref_id=pref_id, hitcount=1)
return
+
+
+def solr_search_string(request):
+
+ try:
+ requests.get(URL)
+ results = {}
+ context = []
+ response_dict = []
+ search_string = request.GET.get('search_string')
+ q = "content:'{0}'".format(search_string)
+ fl = "*"
+ qt = "select"
+ fq = "*"
+ rows = "100"
+ wt = 'json'
+ solr = pysolr.Solr(URL, search_handler="/"+qt, timeout=5)
+ results = solr.search(q, **{
+ 'rows': rows,
+ 'group': 'true',
+ 'group.field': 'example',
+ 'group.limit': '1',
+ 'group.main': 'true',
+ })
+ for obj in results:
+ response = {
+ 'example_id': obj['id'],
+ 'book_id': obj['book_id'],
+ 'book': obj['title'],
+ 'author': obj['author'],
+ 'chapter': obj['chapter'],
+ 'example': obj['example'],
+ }
+ response_dict.append(response)
+
+ print("Saw {0} result(s).".format(len(results)))
+ template = loader.get_template('search_code.html')
+ context = {'data': response_dict}
+ data = template.render(context)
+ return JsonResponse({'data': data})
+ except Exception:
+ context = {'data': ''}
+ template = loader.get_template('search_code.html')
+ data = template.render(context)
+ return JsonResponse({'data': '', 'error': 'True'})
+
+
+def checkserver(request):
+ try:
+ req_status = requests.get(API_URL_SERVER)
+ return JsonResponse({'status': req_status.status_code})
+ except Exception:
+ return JsonResponse({'error': 'True'})