Added possibility to choose the index in search + Updated search page with jinja2 iter0

pull/114/head
Mokaddem 2017-03-15 09:39:48 +01:00
parent 248469d61e
commit e4757f5ceb
3 changed files with 90 additions and 14 deletions

View File

@ -20,7 +20,34 @@ cfg = Flask_config.cfg
r_serv_pasteName = Flask_config.r_serv_pasteName
max_preview_char = Flask_config.max_preview_char
max_preview_modal = Flask_config.max_preview_modal
baseindexpath = os.path.join(os.environ['AIL_HOME'], cfg.get("Indexer", "path"))
indexRegister_path = os.path.join(os.environ['AIL_HOME'],
cfg.get("Indexer", "register"))
# ============ FUNCTIONS ============
def get_current_index():
with open(indexRegister_path, "r") as f:
allIndex = f.read()
allIndex = allIndex.split(',')
allIndex.sort()
indexnum = int(allIndex[-1])
indexpath = os.path.join(baseindexpath, "index_"+str(indexnum))
return indexpath
def get_index_list(selected_index=""):
index_list = []
for dirs in os.listdir(baseindexpath):
if os.path.isdir(os.path.join(baseindexpath, dirs)):
index_list.append([ dirs, dirs + " - " + str(get_dir_size(dirs) / (1000*1000)) + " Mb", dirs==selected_index.split('/')[-1]])
return index_list
def get_dir_size(directory):
cur_sum = 0
for directory, subdirs, files in os.walk(os.path.join(baseindexpath,directory)):
cur_sum += sum(os.path.getsize(os.path.join(directory, name)) for name in files)
return cur_sum
# ============ ROUTES ============
@ -34,8 +61,15 @@ def search():
c = [] #preview of the paste content
paste_date = []
paste_size = []
index_num = request.form['index_num']
num_elem_to_get = 50
# select correct index
if index_num is None or index_num == "0":
selected_index = get_current_index()
else:
selected_index = os.path.join(baseindexpath, index_num)
# Search filename
for path in r_serv_pasteName.smembers(q[0]):
r.append(path)
@ -53,8 +87,7 @@ def search():
from whoosh.fields import Schema, TEXT, ID
schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)
indexpath = os.path.join(os.environ['AIL_HOME'], cfg.get("Indexer", "path"))
ix = index.open_dir(indexpath)
ix = index.open_dir(selected_index)
from whoosh.qparser import QueryParser
with ix.searcher() as searcher:
query = QueryParser("content", ix.schema).parse(" ".join(q))
@ -72,7 +105,14 @@ def search():
results = searcher.search(query)
num_res = len(results)
return render_template("search.html", r=r, c=c, query=request.form['query'], paste_date=paste_date, paste_size=paste_size, char_to_display=max_preview_modal, num_res=num_res)
index_min = 1
index_max = len(get_index_list())
return render_template("search.html", r=r, c=c,
query=request.form['query'], paste_date=paste_date,
paste_size=paste_size, char_to_display=max_preview_modal,
num_res=num_res, index_min=index_min, index_max=index_max,
index_list=get_index_list(selected_index)
)
@app.route("/get_more_search_result", methods=['POST'])
@ -81,8 +121,15 @@ def get_more_search_result():
q = []
q.append(query)
page_offset = int(request.form['page_offset'])
index_num = request.form['index_num']
num_elem_to_get = 50
# select correct index
if index_num is None or index_num == "0":
selected_index = get_current_index()
else:
selected_index = os.path.join(baseindexpath, index_num)
path_array = []
preview_array = []
date_array = []
@ -92,8 +139,7 @@ def get_more_search_result():
from whoosh.fields import Schema, TEXT, ID
schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)
indexpath = os.path.join(os.environ['AIL_HOME'], cfg.get("Indexer", "path"))
ix = index.open_dir(indexpath)
ix = index.open_dir(selected_index)
from whoosh.qparser import QueryParser
with ix.searcher() as searcher:
query = QueryParser("content", ix.schema).parse(" ".join(q))
@ -113,7 +159,6 @@ def get_more_search_result():
to_return["preview_array"] = preview_array
to_return["date_array"] = date_array
to_return["size_array"] = size_array
print "len(path_array)="+str(len(path_array))
if len(path_array) < num_elem_to_get: #pagelength
to_return["moreData"] = False
else:

View File

@ -89,6 +89,16 @@
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<strong style="">Index: </strong>
<select class="form-control" id="index_num" style="display: inline-block; margin-bottom: 5px; width: 15%">
{% for indexElem in index_list %}
<option {% if indexElem[2] %} selected="selected" {% endif %} value="{{ indexElem[0] }}" >{{ indexElem[1] }}</option>
{% endfor %}
</select>
</div>
</div>
<table class="table table-striped table-bordered table-hover" id="myTable">
<thead>
<tr>
@ -100,16 +110,14 @@
</tr>
</thead>
<tbody id="table_body">
{% set i = 0 %}
{% for path in r %}
<tr>
<td>{{ i + 1 }}</td>
<td><a target="_blank" href="{{ url_for('showsavedpaste') }}?paste={{ path }}&num={{ i+1 }}"> {{ path }}</a></td>
<td>{{ paste_date[i] }}</td>
<td>{{ paste_size[i] }}</td>
<td><p><span class="glyphicon glyphicon-info-sign" data-toggle="tooltip" data-placement="left" title="{{ c[i] }}"></span> <button type="button" class="btn-link" data-num="{{ i + 1 }}" data-toggle="modal" data-target="#mymodal" data-url="{{ url_for('showsavedpaste') }}?paste={{ path }}&num={{ i+1 }}" data-path="{{ path }}"><span class="fa fa-search-plus"></span></button></p></td>
<td>{{ loop.index0 + 1 }}</td>
<td><a target="_blank" href="{{ url_for('showsavedpaste') }}?paste={{ path }}&num={{ loop.index0+1 }}"> {{ path }}</a></td>
<td>{{ paste_date[loop.index0] }}</td>
<td>{{ paste_size[loop.index0] }}</td>
<td><p><span class="glyphicon glyphicon-info-sign" data-toggle="tooltip" data-placement="left" title="{{ c[loop.index0] }}"></span> <button type="button" class="btn-link" data-num="{{ loop.index0 + 1 }}" data-toggle="modal" data-target="#mymodal" data-url="{{ url_for('showsavedpaste') }}?paste={{ path }}&num={{ loop.index0+1 }}" data-path="{{ path }}"><span class="fa fa-search-plus"></span></button></p></td>
</tr>
{% set i = i + 1 %}
{% endfor %}
</tbody>
</table>
@ -157,6 +165,28 @@
if (init_num_of_elements_in_table == pagelen) {
$("#load_more_json_button1").show();
}
$('#index_num').on('change', function() {
var form = document.createElement('form');
form.setAttribute("method", 'post');
form.setAttribute("action", "{{ url_for('search') }}");
var input1 = document.createElement('input');
input1.setAttribute("type", "hidden");
input1.setAttribute("name", "index_num");
input1.setAttribute("value", this.value);
form.appendChild(input1);
var input2 = document.createElement('input');
input2.setAttribute("type", "hidden");
input2.setAttribute("name", "query");
input2.setAttribute("value", "{{ query }}");
form.appendChild(input2);
document.body.appendChild(form);
form.submit();
})
});
</script>
@ -171,7 +201,7 @@
}
function load_search_50_data() {
var options = { query: query, page_offset: page_offset };
var options = { query: query, page_offset: page_offset, index_num: $("#index_num").val() };
$.post( "{{ url_for('get_more_search_result') }}", options).done(function( data ) {
for(i=0; i<data.path_array.length; i++) {

View File

@ -1,6 +1,7 @@
<div class="input-group custom-search-form">
<form action="/search" id="form-search" method=POST>
<input type="text" name="query" class="form-control" placeholder="Search Paste">
<input type="hidden" name="index_num" class="form-control" value="0" placeholder="Index Num">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">
<i class="fa fa-search"></i>