Commit 86820641 authored by Romain Loth's avatar Romain Loth

import/export terms table: only *label* column is mandatory + better css

parent 29ff4b0c
......@@ -444,30 +444,53 @@ def import_ngramlists(fname, delimiter=DEFAULT_CSV_DELIM,
n_added_ng = 0
n_group_relations = 0
# columntype => int
columns = {}
# load CSV + initial checks
for i, csv_row in enumerate(ngrams_csv_rows):
# fyi
n_read_lines +=1
# print("---------------READ LINE %i" % i)
# headers
if i == 0:
n_cols = len(csv_row)
for j, colname in enumerate(csv_row):
if colname in ['label', 'status', 'forms']:
columns[colname] = j
else:
raise ValueError('Wrong header "%s" on line %i (only possible headers are "label", "forms" and "status")' % (colname, n_read_lines))
if 'label' not in columns:
raise ValueError('CSV must contain at least one column with the header "label"')
if not len(csv_row):
continue
try:
# £TODO this_list_type optionnel => default="map"
# £TODO pré-diagnostic => retrouver les col_id
this_list_type = str(csv_row[0])
this_row_label = str(csv_row[1])
this_row_forms = str(csv_row[2])
# try:
# mandatory column
this_row_label = str(csv_row[columns['label']])
# string normalizations
this_row_label = normalize_terms(normalize_chars(this_row_label))
# other columns or their default values
if 'status' in columns:
this_list_type = str(csv_row[columns['status']])
else:
this_list_type = 'map'
except:
if i == 0:
print("IMPORT WARN: (skip line) probable header line at CSV %s:l.0" % fname)
continue
else:
raise ValueError("Error on CSV read line %i" %n_read_lines)
if 'forms' in columns:
this_row_forms = str(csv_row[columns['forms']])
else:
this_row_forms = ''
# string normalizations
this_row_label = normalize_terms(normalize_chars(this_row_label))
# except:
# if i == 0:
# print("IMPORT WARN: (skip line) probable header line at CSV %s:l.0" % fname)
# continue
# else:
# raise ValueError("Error on CSV read line %i" % i)
# --- term checking
if not len(this_row_label) > 0:
......@@ -475,7 +498,6 @@ def import_ngramlists(fname, delimiter=DEFAULT_CSV_DELIM,
continue
# --- check correct list type
# £TODO this_list_type optionnel => default="map"
if not this_list_type in ['stop','main','map']:
print("IMPORT WARN: (skip line) wrong list type at CSV %s:l.%i" % (fname, i))
continue
......
......@@ -80,15 +80,20 @@ class CSVLists(APIView):
csv_file = request.data['csvfile']
# import the csv
new_lists = import_ngramlists(csv_file)
del csv_file
# merge the new_lists onto those of the target corpus
log_msg = merge_ngramlists(new_lists, onto_corpus=corpus_node)
return JsonHttpResponse({
'log': log_msg,
}, 200)
try:
new_lists = import_ngramlists(csv_file)
del csv_file
# merge the new_lists onto those of the target corpus
log_msg = merge_ngramlists(new_lists, onto_corpus=corpus_node)
return JsonHttpResponse({
'log': log_msg,
}, 200)
except Exception as e:
return JsonHttpResponse({
'err': str(e),
}, 400)
......
......@@ -87,7 +87,7 @@ p.note > label {
opacity: 0.3;
}
tr:hover {
#my-ajax-table tr:hover {
cursor: pointer;
font-weight: bold;
}
......
......@@ -7,6 +7,53 @@
<link rel="stylesheet" type="text/css" href="{% static "lib/gargantext/tables.css"%}"/>
<link rel="stylesheet" type="text/css" href="{% static "lib/gargantext/charts.css"%}"/>
<style>
#formatinfos-announce {
font-size: 12px ;
padding-top: .5em;
}
#formatinfos {
background-color: #CCC ;
font-size: 12px ;
padding: 1em;
border-radius: 1em;
margin: 1.5em;
}
#formatinfos table {
margin-left: 2.5em;
margin-bottom: 1em;
margin-top: 1em;
}
#formatinfos tr:hover {
font-weight: normal
}
#formatinfos td {
color: inherit ;
hover: none ;
}
#formatinfos h4 {
font-size: 14px;
color: #777;
font-weight: bold;
}
#formatinfos p {
font-size: 14px;
}
#formatinfos em {
font-weight:bold ;
}
</style>
<script type="text/javascript" src="{% static "lib/d3/d3.js"%}"></script>
<script type="text/javascript" src="{% static "lib/d3/crossfilter.js"%}"></script>
<script type="text/javascript" src="{% static "lib/d3/dc.js"%}"></script>
......@@ -126,7 +173,7 @@
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Import a CSV term list</h3>
<h3 id="myModalLabel">Import a Termlist</h3>
</div>
<div class="modal-body" id="uploadform">
<form id="csvimportform"
......@@ -134,8 +181,36 @@
enctype="multipart/form-data"
method="post">
{% csrf_token %}
<label>From your disk:</label>
<label>From a CSV on your disk:</label>
<input type="file" id="csvfile" accept="text/csv">
<p id="formatinfos-announce">
<span
id="formatinfos-icon"
class="glyphicon glyphicon-triangle-right"
onclick="toggleFormatInfos()"></span>
More infos about CSV expected format
</p>
<div id="formatinfos" style="display:none;">
<h4>Example table</h4>
<table class="table-condensed note">
<tr><th>status</th> <th>label</th> <th>forms</th></tr>
<tr><td>map</td> <td>barograph</td> <td></td></tr>
<tr><td>map</td> <td>seafaring</td> <td>seafarer|&amp;|ocean travel</td></tr>
<tr><td>main</td> <td>electromagnetic</td> <td>electro-magnetic</td></tr>
</table>
<h4>Remarks</h4>
<ul>
<li>Tabulation is the expected delimiter between columns.</li>
<li>The only mandatory column is <em>label</em>.</li>
<li>If <em>status</em> is absent, default target status is "map"</li>
<li>When a column is there, always add its header on the 1st line: <em>status</em>, <em>label</em>, <em>forms</em></li>
<li>The label will be always added as a form, even if it's not in 'forms' column</li>
<li>The string <em>|&amp;|</em> (3 characters) is the expected delimiter between forms.</li>
</ul>
</div>
<br/>
<label>From another corpus:</label>
<p>TODO</p>
......@@ -153,6 +228,28 @@
<script type="text/javascript" src="{% static "lib/gargantext/NGrams_dyna_chart_and_table.js" %}"></script>
<script type="text/javascript">
var formatInfosOpen = false;
function toggleFormatInfos() {
// when already open => we close
if (formatInfosOpen) {
// hide div
$('#formatinfos').hide()
// change icon
$('#formatinfos-icon')[0].classList.remove('glyphicon-triangle-bottom')
$('#formatinfos-icon')[0].classList.add('glyphicon-triangle-right')
// toggle flag
formatInfosOpen = false;
}
else {
// opposite case
$('#formatinfos').show()
$('#formatinfos-icon')[0].classList.remove('glyphicon-triangle-right')
$('#formatinfos-icon')[0].classList.add('glyphicon-triangle-bottom')
formatInfosOpen = true;
}
}
/* merci c24b !
* Uses csvimportroute variable from the django template
......@@ -202,7 +299,7 @@ function postCSV(e){
xhr.setRequestHeader("X-CSRFToken", getCookie("csrftoken"));
},
success: function(response) {
my_html = "<h2 color='green'>IMPORT OK ! </h2>"
my_html = '<h3 style="color:green">IMPORT OK</h3>'
my_html += "<p class='note'>" + response['log'].replace(/\n/g, '<br/>') + "</p>"
my_html += "<p'>(this page will reload in 3s)</p>"
$('#formanswer').html(my_html);
......@@ -211,7 +308,10 @@ function postCSV(e){
setTimeout("location.reload(true)", 3000);
},
error: function(result) {
$('#formanswer').html('Erreur');
my_html = '<h3 style="color:red">Error</h3>'
my_html += "<p class='note'>please correct your CSV file and retry</p>"
my_html += "<p>"+ result.responseJSON['err']+"</p>"
$('#formanswer').html(my_html);
console.error(result);
},
});
......
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