Commit df9e976a authored by sim's avatar sim

[FIX] Quick'n'dirty termlists overwrite feature

parent d4f65542
...@@ -568,7 +568,8 @@ def import_ngramlists(the_file, delimiter=DEFAULT_CSV_DELIM, ...@@ -568,7 +568,8 @@ def import_ngramlists(the_file, delimiter=DEFAULT_CSV_DELIM,
'map': UnweightedList(), 'map': UnweightedList(),
'main': UnweightedList(), 'main': UnweightedList(),
'stop': UnweightedList(), 'stop': UnweightedList(),
'groupings' : Translations() 'groupings' : Translations(),
'new_ngram_count': n_added_ng,
} }
for list_type in imported_nodes_ngrams: for list_type in imported_nodes_ngrams:
...@@ -664,12 +665,13 @@ def merge_ngramlists(new_lists={}, onto_corpus=None, del_originals=[]): ...@@ -664,12 +665,13 @@ def merge_ngramlists(new_lists={}, onto_corpus=None, del_originals=[]):
for ng_id in new_lists[list_type].items: for ng_id in new_lists[list_type].items:
collect(ng_id) collect(ng_id)
from gargantext.util.toolchain.main import t if new_lists.get('new_ngram_count', 0) > 0:
print("MERGE DEBUG: starting index_new_ngrams", t()) from gargantext.util.toolchain.main import t
n_added = index_new_ngrams(all_possibly_new_ngram_ids, onto_corpus) print("MERGE DEBUG: starting index_new_ngrams", t())
print("MERGE DEBUG: finished 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 ========= # ======== Get the old lists =========
old_lists = {} old_lists = {}
...@@ -828,7 +830,7 @@ def merge_ngramlists(new_lists={}, onto_corpus=None, del_originals=[]): ...@@ -828,7 +830,7 @@ def merge_ngramlists(new_lists={}, onto_corpus=None, del_originals=[]):
@shared_task @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 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): ...@@ -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() corpus_node = session.query(Node).filter(Node.id == onto_corpus_id).first()
# merge the new_lists onto those of the target corpus # 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 return log_msg
...@@ -90,10 +90,11 @@ class CSVLists(APIView): ...@@ -90,10 +90,11 @@ class CSVLists(APIView):
# import the csv # import the csv
# try: # try:
log_msg = "Async generation" log_msg = "Async generation"
corpus_node_id = corpus_node.id 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({ return JsonHttpResponse({
'log': log_msg, 'log': log_msg,
}, 200) }, 200)
...@@ -153,7 +154,8 @@ class CSVLists(APIView): ...@@ -153,7 +154,8 @@ class CSVLists(APIView):
# attempt to merge and send response # attempt to merge and send response
try: try:
# merge the source_lists onto those of the target corpus # 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({ return JsonHttpResponse({
'log': log_msg, 'log': log_msg,
}, 200) }, 200)
......
...@@ -250,6 +250,23 @@ em { ...@@ -250,6 +250,23 @@ em {
<br/> <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" /> <input type="submit" class="btn btn-xs btn-info" id="importsubmit" value="Import and merge with current table" />
</form> </form>
</div> </div>
...@@ -372,6 +389,8 @@ function listmergeUpdate(aFormData){ ...@@ -372,6 +389,8 @@ function listmergeUpdate(aFormData){
// all params are added in the url like a GET // all params are added in the url like a GET
theUrl += "&from_corpus="+sourceCorpusId theUrl += "&from_corpus="+sourceCorpusId
theUrl += "&todo="+todoLists.join(',') 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 // result url looks like this : /api/ngramlists/import?onto_corpus=2&from=13308&todo=map,stop
// console.log(theUrl) // console.log(theUrl)
...@@ -424,7 +443,7 @@ function listmergeCsvPost(theFile){ ...@@ -424,7 +443,7 @@ function listmergeCsvPost(theFile){
//postCorpusFile //postCorpusFile
$.ajax({ $.ajax({
url: "{{importroute | safe}}", url: "{{importroute | safe}}" + ($('#importoverwrite').is(':checked') ? '&overwrite=1' : ''),
type: 'POST', type: 'POST',
async: true, async: true,
contentType: false, contentType: false,
...@@ -436,11 +455,11 @@ function listmergeCsvPost(theFile){ ...@@ -436,11 +455,11 @@ function listmergeCsvPost(theFile){
success: function(response) { success: function(response) {
my_html = '<h3 style="color:green">File upload, you will receive a notification email</h3>' 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 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); $('#formanswer').html(my_html);
console.log(response) ; console.log(response) ;
// reload after 3s // reload after 3s
setTimeout("location.reload(true)", 3000); setTimeout("location.reload(true)", 30000);
}, },
error: function(result, t) { error: function(result, t) {
if (t != 'timeout') { 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