Commit 4c7b4733 authored by sim's avatar sim

[FIX] Quick'n'dirty termlists overwrite feature

parent 7e5fd1da
......@@ -568,7 +568,8 @@ def import_ngramlists(the_file, delimiter=DEFAULT_CSV_DELIM,
'map': UnweightedList(),
'main': UnweightedList(),
'stop': UnweightedList(),
'groupings' : Translations()
'groupings' : Translations(),
'new_ngram_count': n_added_ng,
}
for list_type in imported_nodes_ngrams:
......@@ -664,12 +665,13 @@ def merge_ngramlists(new_lists={}, onto_corpus=None, del_originals=[]):
for ng_id in new_lists[list_type].items:
collect(ng_id)
from gargantext.util.toolchain.main import t
print("MERGE DEBUG: starting index_new_ngrams", t())
n_added = index_new_ngrams(all_possibly_new_ngram_ids, onto_corpus)
print("MERGE DEBUG: finished index_new_ngrams", t())
if new_lists.get('new_ngram_count', 0) > 0:
from gargantext.util.toolchain.main import t
print("MERGE DEBUG: starting index_new_ngrams", t())
n_added = index_new_ngrams(all_possibly_new_ngram_ids, onto_corpus)
print("MERGE DEBUG: finished index_new_ngrams", t())
my_log.append("MERGE: added %i new ngram occurrences in docs" % n_added)
my_log.append("MERGE: added %i new ngram occurrences in docs" % n_added)
# ======== Get the old lists =========
old_lists = {}
......@@ -828,7 +830,7 @@ def merge_ngramlists(new_lists={}, onto_corpus=None, del_originals=[]):
@shared_task
def import_and_merge_ngramlists(file_contents, onto_corpus_id):
def import_and_merge_ngramlists(file_contents, onto_corpus_id, overwrite=False):
"""
A single function to run import_ngramlists and merge_ngramlists together
"""
......@@ -838,6 +840,7 @@ def import_and_merge_ngramlists(file_contents, onto_corpus_id):
corpus_node = session.query(Node).filter(Node.id == onto_corpus_id).first()
# merge the new_lists onto those of the target corpus
log_msg = merge_ngramlists(new_lists, onto_corpus=corpus_node)
del_originals = ['stop', 'main', 'map'] if overwrite else []
log_msg = merge_ngramlists(new_lists, onto_corpus=corpus_node, del_originals=del_originals)
return log_msg
......@@ -90,10 +90,11 @@ class CSVLists(APIView):
# import the csv
# try:
log_msg = "Async generation"
corpus_node_id = corpus_node.id
scheduled(import_and_merge_ngramlists)(csv_contents, corpus_node_id)
scheduled(import_and_merge_ngramlists)(csv_contents, corpus_node_id,
overwrite=bool(params.get('overwrite')))
return JsonHttpResponse({
'log': log_msg,
}, 200)
......@@ -153,7 +154,8 @@ class CSVLists(APIView):
# attempt to merge and send response
try:
# merge the source_lists onto those of the target corpus
log_msg = merge_ngramlists(source_lists, onto_corpus=corpus_node)
delete = todo_lists if bool(params.get('overwrite')) else []
log_msg = merge_ngramlists(source_lists, onto_corpus=corpus_node, del_originals=delete)
return JsonHttpResponse({
'log': log_msg,
}, 200)
......
......@@ -250,6 +250,23 @@ em {
<br/>
<div class="checkbox">
<label>
<input type="checkbox" id="importoverwrite"> Overwrite old lists
<script>
function updateSubmitLabel() {
$('#importsubmit').val($(this).is(':checked') ? 'Overwrite current table' : 'Import and merge with current table');
}
$(function() {
updateSubmitLabel.call($('#importoverwrite'));
$('#importoverwrite').change(updateSubmitLabel);
});
</script>
</label>
</div>
<br/>
<input type="submit" class="btn btn-xs btn-info" id="importsubmit" value="Import and merge with current table" />
</form>
</div>
......@@ -372,6 +389,8 @@ function listmergeUpdate(aFormData){
// all params are added in the url like a GET
theUrl += "&from_corpus="+sourceCorpusId
theUrl += "&todo="+todoLists.join(',')
if ($('#importoverwrite').is(':checked'))
theUrl += "&overwrite=1"
// result url looks like this : /api/ngramlists/import?onto_corpus=2&from=13308&todo=map,stop
// console.log(theUrl)
......@@ -424,7 +443,7 @@ function listmergeCsvPost(theFile){
//postCorpusFile
$.ajax({
url: "{{importroute | safe}}",
url: "{{importroute | safe}}" + ($('#importoverwrite').is(':checked') ? '&overwrite=1' : ''),
type: 'POST',
async: true,
contentType: false,
......@@ -436,11 +455,11 @@ function listmergeCsvPost(theFile){
success: function(response) {
my_html = '<h3 style="color:green">File upload, you will receive a notification email</h3>'
my_html += "<p class='note'>" + response['log'].replace(/\n/g, '<br/>') + "</p>"
my_html += "<p'>(this page will reload in 3s)</p>"
my_html += "<p'>(this page will reload in 30s)</p>"
$('#formanswer').html(my_html);
console.log(response) ;
// reload after 3s
setTimeout("location.reload(true)", 3000);
setTimeout("location.reload(true)", 30000);
},
error: function(result, t) {
if (t != 'timeout') {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment