Commit 9e18f3d2 authored by Sylvain Coulange's avatar Sylvain Coulange
Browse files

fonction modif dico OK

parent e5e990f2
......@@ -2,7 +2,6 @@ from django.shortcuts import render, HttpResponseRedirect
from static.scripts import textPhonographer as txtphono
from .models import Phonograph, Data, Entree, commitData
from django.http import JsonResponse
import json
import spacy
import subprocess
......@@ -86,40 +85,27 @@ def dicoReq(request):
motCond = request.GET.get('motCond', None)
transCond = request.GET.get('transCond', None)
listeEntrees = txtphono.getEntryByWord(m=mot,mc=motCond,t=trans,tc=transCond)
return JsonResponse(listeEntrees, safe=False)
def editEntry(request,Type):
mot = request.POST['entreeMot']# if request.POST.get('mot') else ''
trans = request.POST['entreeTrans']# if request.POST.get('trans') else ''
motOrigin = request.POST['motOrigin'] if request.POST.get('motOrigin') else ''
searchMot = request.POST['inMot'] if request.POST.get('inMot') else mot
searchTrans = request.POST['inTrans'] if request.POST.get('inTrans') else trans
condition_mot = request.POST['condition-mot'] if request.POST.get('inMot') else 'contient'
condition_trans = request.POST['condition-trans'] if request.POST.get('inTrans') else 'contient'
if Type == 'MODIF':
message = txtphono.modifEntry(motOrigin,mot,trans)
elif Type == 'ADD':
if mot == '' or trans == '':
message = 'Entrée incomplète ! Indiquez un mot et sa transcription.'
else:
message = txtphono.addEntry(mot,trans)
elif Type == 'DELETE':
message = txtphono.delEntry(mot,trans)
rep = {
"listeEntrees" : txtphono.getEntryByWord(m=mot,mc=motCond,t=trans,tc=transCond)
}
return JsonResponse(rep)
def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
return ip
data = Entree()
data.update = updateTime()
data.mot = searchMot
data.trans = searchTrans
data.motcond = condition_mot
data.transcond = condition_trans
data.result = txtphono.getEntryByWord(m=data.mot, mc=data.motcond, t=data.trans, tc=data.transcond)
data.nb = len(data.result)
data.message = message
return render(request, 'editDico.html', {'data': data})
def editEntry(request):
mot = request.GET.get('mot', None)
transList = request.GET.get('transList', None)
ipCli = get_client_ip(request)
rep = {
'reponse': txtphono.modifEntry(ipCli,mot,transList)
}
return JsonResponse(rep)
""" def commits(request):
......
......@@ -27,5 +27,5 @@ urlpatterns = [
path('app/0', main0),
path('app/dico/', dicoView),
path('dicoSearch/', dicoReq),
path('app/dico/edit/<str:Type>/', editEntry)
path('modifEntry/', editEntry)
]
......@@ -8,6 +8,7 @@ $(document).ready(function(){
var trans = document.getElementById('inTrans').value;
var motCond = document.getElementById('condition-mot').value;
var transCond = document.getElementById('condition-trans').value;
var lastEntrShowed = 0;
if (mot.length > 0 || trans.length > 0) {
document.getElementById('resultTable').innerHTML = '<colgroup><col width="40%"><col width="40%"><col width="20%"></colgroup><tr><th>Mot</th><th>Transcription (API)</th><th>Actions</th></tr>';
......@@ -46,30 +47,41 @@ $(document).ready(function(){
document.body.style.cursor = "default";
document.getElementById('resultTable').innerHTML = '<colgroup><col width="40%"><col width="40%"><col width="20%"></colgroup><tr><th>Mot</th><th>Transcription (API)</th><th>Actions</th></tr>';
console.log(data.length + ' résultats.');
document.getElementById('nbres').innerHTML = '('+data.length+')';
document.getElementById('nbres').innerHTML = '('+Object.keys(data.listeEntrees).length+')';
// on n'affiche que 50 résultats par page pour pas surcharger le navigateur
showPart(lastEntrShowed,50,data.listeEntrees);
document.getElementById('btnShowMore').style.display = '';
document.getElementById('btnShowMore').addEventListener('click',event => {
showPart(lastEntrShowed,50,data.listeEntrees);
});
}
function showPart(start,nb,dico) {
var cptEntr = 0;
for (i in data) {
nb = start + nb;
for (i in dico) {
cptEntr += 1;
var entr = data[i][0];
var lienWiki = '<button role="button" class="btn btn-sm" title="Consulter le Wiktionnaire"><a href="https://fr.wiktionary.org/wiki/'+entr.replace(' ','_')+'" target="_blank"><svg class="wikiLienIcon" aria-hidden="true" focusable="false" data-prefix="fab" data-icon="wikipedia-w" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512" data-fa-i2svg=""><path fill="currentColor" d="M640 51.2l-.3 12.2c-28.1.8-45 15.8-55.8 40.3-25 57.8-103.3 240-155.3 358.6H415l-81.9-193.1c-32.5 63.6-68.3 130-99.2 193.1-.3.3-15 0-15-.3C172 352.3 122.8 243.4 75.8 133.4 64.4 106.7 26.4 63.4.2 63.7c0-3.1-.3-10-.3-14.2h161.9v13.9c-19.2 1.1-52.8 13.3-43.3 34.2 21.9 49.7 103.6 240.3 125.6 288.6 15-29.7 57.8-109.2 75.3-142.8-13.9-28.3-58.6-133.9-72.8-160-9.7-17.8-36.1-19.4-55.8-19.7V49.8l142.5.3v13.1c-19.4.6-38.1 7.8-29.4 26.1 18.9 40 30.6 68.1 48.1 104.7 5.6-10.8 34.7-69.4 48.1-100.8 8.9-20.6-3.9-28.6-38.6-29.4.3-3.6 0-10.3.3-13.6 44.4-.3 111.1-.3 123.1-.6v13.6c-22.5.8-45.8 12.8-58.1 31.7l-59.2 122.8c6.4 16.1 63.3 142.8 69.2 156.7L559.2 91.8c-8.6-23.1-36.4-28.1-47.2-28.3V49.6l127.8 1.1.2.5z"></path></svg></a></button>';
var transList = '';
for (j in data[i][1]) {
transList += data[i][1][j];
if (j < data[i][1].length-1) transList += ', '
if(cptEntr > start && cptEntr <= nb) {
var entr = i;
var lienWiki = '<a href="https://fr.wiktionary.org/wiki/'+entr.replace(' ','_')+'" target="_blank"><button role="button" class="btn btn-sm" title="Consulter le Wiktionnaire">W</button></a>';
//<button role="button" class="btn btn-sm" title="Consulter le Wiktionnaire"><a href="https://fr.wiktionary.org/wiki/'+entr.replace(' ','_')+'" target="_blank"><svg class="wikiLienIcon" aria-hidden="true" focusable="false" data-prefix="fab" data-icon="wikipedia-w" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512" data-fa-i2svg=""><path fill="currentColor" d="M640 51.2l-.3 12.2c-28.1.8-45 15.8-55.8 40.3-25 57.8-103.3 240-155.3 358.6H415l-81.9-193.1c-32.5 63.6-68.3 130-99.2 193.1-.3.3-15 0-15-.3C172 352.3 122.8 243.4 75.8 133.4 64.4 106.7 26.4 63.4.2 63.7c0-3.1-.3-10-.3-14.2h161.9v13.9c-19.2 1.1-52.8 13.3-43.3 34.2 21.9 49.7 103.6 240.3 125.6 288.6 15-29.7 57.8-109.2 75.3-142.8-13.9-28.3-58.6-133.9-72.8-160-9.7-17.8-36.1-19.4-55.8-19.7V49.8l142.5.3v13.1c-19.4.6-38.1 7.8-29.4 26.1 18.9 40 30.6 68.1 48.1 104.7 5.6-10.8 34.7-69.4 48.1-100.8 8.9-20.6-3.9-28.6-38.6-29.4.3-3.6 0-10.3.3-13.6 44.4-.3 111.1-.3 123.1-.6v13.6c-22.5.8-45.8 12.8-58.1 31.7l-59.2 122.8c6.4 16.1 63.3 142.8 69.2 156.7L559.2 91.8c-8.6-23.1-36.4-28.1-47.2-28.3V49.6l127.8 1.1.2.5z"></path></svg></a></button>';
var transList = '';
for (j in dico[i]) {
transList += dico[i][j];
if (j < dico[i].length-1) transList += ', '
}
var btnModifier = '<button id="btnModifier'+cptEntr+'" class="btn btn-primary btn-perso" onclick="modifEntry('+cptEntr+',\''+entr+'\')"><span class="glyphicon glyphicon-edit" area-hidden="true" title="Modifier"></span></button>';
var btnValider = '<button id="btnValider'+cptEntr+'" class="btn btn-success btn-perso" style="display: none;" onclick="validEntry(\''+entr+'\','+cptEntr+')"><span class="glyphicon glyphicon-ok" area-hidden="true" title="Valider"></span></button>';
var btnAnnuler = '<button id="btnAnnuler'+cptEntr+'" class="btn btn-secondary btn-perso" style="display: none;" onclick="annuler('+cptEntr+')"><span class="glyphicon glyphicon-remove" area-hidden="true" title="Annuler"></span></button>';
document.getElementById('resultTable').innerHTML += '<tr id="tr'+cptEntr+'" class="tr"><td><span class="numEntr">'+cptEntr+'</span>'+lienWiki+' '+entr+'</td><td><input class="api-kb" disabled="true" type="text" id="td'+cptEntr+'" value="'+transList+'"></td><td style="text-align:center">'+btnModifier+btnValider+btnAnnuler+'</td></tr>';
getAPIkeyboard();
}
var btnModifier = '<button id="btnModifier'+cptEntr+'" class="btn btn-primary btn-perso" onclick="modifEntry('+cptEntr+',\''+entr+'\')"><span class="glyphicon glyphicon-edit" area-hidden="true" title="Modifier"></span></button>';
var btnValider = '<button id="btnValider'+cptEntr+'" class="btn btn-success btn-perso" style="display: none;"><span class="glyphicon glyphicon-ok" area-hidden="true" title="Valider"></span></button>';
var btnAnnuler = '<button id="btnAnnuler'+cptEntr+'" class="btn btn-secondary btn-perso" style="display: none;" onclick="annuler('+cptEntr+')"><span class="glyphicon glyphicon-remove" area-hidden="true" title="Annuler"></span></button>';
document.getElementById('resultTable').innerHTML += '<tr id="tr'+cptEntr+'" class="tr"><td>'+lienWiki+' '+entr+'</td><td><input class="api-kb" disabled="true" type="text" id="td'+cptEntr+'" value="'+transList+'"></td><td style="text-align:center">'+btnModifier+btnValider+btnAnnuler+'</td></tr>';
getAPIkeyboard();
}
if (cptEntr >= 300) window.alert('Trop de résultats ! Merci de préciser un peu plus votre recherche :)')
lastEntrShowed = nb;
}
......@@ -107,6 +119,38 @@ function modifEntry(cptEntr,entr) {
thisBtnAnnuler.style.display = '';
}
function validEntry(entr, cptEntr) {
document.getElementById('actionBlock').style.display = '';
var thisMot = entr;
var thisTd = 'td'+cptEntr;
thisTransList = document.getElementById(thisTd).value;
document.body.style.cursor = "wait";
$.ajax({
url: '/modifEntry/',
data: {
mot: thisMot,
transList: thisTransList
},
dataType: 'json',
success: function(data) {
document.body.style.cursor = "default";
console.log("Modification réussie !");
window.alert(data.reponse);
document.getElementById('actionBlock').style.display = 'none';
annuler(cptEntr);
},
error: function(){
// document.getElementById('loader').style.display = 'none';
document.body.style.cursor = "default";
window.alert("La modification n'a pas pu être effectuée !");
document.getElementById('actionBlock').style.display = 'none';
}
});
}
function annuler(cptEntr) {
var thisTr = 'tr'+cptEntr;
var thisTd = 'td'+cptEntr;
......
......@@ -19,6 +19,7 @@ import json
from colorapp.models import Entree, DicEntry
import tempfile
import csv
from collections import OrderedDict
from sys import path as pylib #im naming it as pylib so that we won't get confused between os.path and sys.path
pylib += [os.path.relpath(r'../phon2graph')]
......@@ -120,7 +121,7 @@ def getEntryByWord(m,mc,t,tc):
if len(m) == 0: m = r'.*'
if len(t) == 0: t = r'.*'
result = []
result = {}
mc1 = r'.*'
mc2 = r'.*'
......@@ -137,26 +138,44 @@ def getEntryByWord(m,mc,t,tc):
tc2 = r'$'
for entree,transList in word2trans.items():
if len(result) >= 300:
break
# if len(result) >= 1000:
# break
if re.match(mc1+m+mc2,entree):
transOK = False
for i,trans in enumerate(transList):
if re.match(tc1+t+tc2,trans.replace('.','')):
transOK = True
if transOK: result.append((entree,transList))
return result
'''
def modifEntry(motOrigin,mot,trans):
mot = mot.replace("'",'’')
if motOrigin != mot:
del word2trans[motOrigin]
word2trans[mot] = trans
rewriteDico()
writeLog(Type="MODIF",motOrigin=motOrigin,mot=mot,trans=trans)
print("Modification du dictionnaire : mot d'origine =",motOrigin,"; mot =",mot,"; trans =",trans)
if transOK: result[entree] = transList
return OrderedDict(sorted(result.items()))
def modifEntry(user,mot,newTransList):
newTransList = newTransList.split(',')
for ind,i in enumerate(newTransList):
newTransList[ind] = i.strip()
oldTransList = word2trans[mot]
if newTransList != oldTransList:
word2trans[mot] = newTransList
rewriteDico()
writeLog(Type="MODIF", user=user, mot=mot, oldTransList=oldTransList, newTransList=newTransList)
print("Modification du dictionnaire par",user,": mot =",mot,"; oldTransList =",oldTransList,"; newTransList = ",newTransList)
return "Modification effectuée avec succès !"
def rewriteDico():
with tempfile.NamedTemporaryFile(dir='.', delete=False, mode="w") as tmp:
json.dump(word2trans, tmp, ensure_ascii=False)
os.replace(tmp.name, dicFile)
print('Dico rewritten.')
def writeLog(Type,user,mot,oldTransList,newTransList):
with open(logFile, mode="a") as logf:
now = datetime.datetime.now()
date = "{}-{}-{} {}:{}:{}".format(now.year, now.month, now.day, now.hour, now.minute, now.second)
logf.write("{},{},{},{},{},{}\n".format(date,user,mot,oldTransList,newTransList,Type))
print("Log file updated.")
'''
def delEntry(mot,trans):
del word2trans[mot]
rewriteDico()
......@@ -177,21 +196,5 @@ def addEntry(mot,trans):
print(message)
return message
def rewriteDico():
with tempfile.NamedTemporaryFile(dir='.', delete=False,mode="w") as tmp:
for wo,tr in sorted(word2trans.items(),key=lambda t:t[0], reverse=False):
tmp.write("{},{}\n".format(wo,tr))
os.replace(tmp.name, dicFile)
print('Dico rewritten.')
def writeLog(Type,mot,trans,motOrigin=''):
with open(logFile, mode="a") as logf:
user = 'userX'
now = datetime.datetime.now()
date = "{}-{}-{} {}:{}:{}".format(now.year, now.month, now.day, now.hour, now.minute, now.second)
logf.write("{},{},{},{},{},{}\n".format(date,user,motOrigin,mot,trans,Type))
print("Log file updated.")
'''
\ No newline at end of file
......@@ -202,10 +202,32 @@ textarea {
margin: 20px auto;
padding: 5px 5px;
}
.warning {
background-color: lightsalmon;
}
.numEntr {
position: absolute;
color: lightslategray;
transform: translate(-200%,70%);
font-style: italic;
font-size: .5em;
}
#actionBlock {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 10;
background-color: rgba(119, 136, 153, 0.596);
}
.mainLoad {
position: fixed;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
}
#resultTable {
width: 90%;
......
......@@ -50,7 +50,7 @@
<button id="rechercher" title="Rechercher dans le dictionnaire">Rechercher</button>
<div class="info">La dictionnaire fait actuellement <span id="dicoNbEntr">1 414 634</span> entrées.</div>
<div class="info warning">La modification du dictionnaire n'est pas encore possible.</div>
<!-- <div class="info warning">La modification du dictionnaire n'est pas encore possible.</div> -->
<div class="result">
<h3>Résultats de la recherche <span id="nbres"></span></h3>
<table id="resultTable">
......@@ -67,6 +67,8 @@
</tr>
</table>
<center><div id="loader" class="loader" style="display: none;"></div></center>
<button id="btnShowMore" class="btn btn-success" style="margin: 10px;display: none;" title="afficher plus de résultats"><span class="glyphicon glyphicon-plus" area-hidden="true"></span></button>
<div id="actionBlock" style="display: none;"><div class="loader mainLoad"></div></div>
</div>
</div>
<div>
......@@ -80,13 +82,13 @@
<script>if ("{{ data.message }}".length > 0){window.alert("{{ data.message }}");};</script>
<script>
var btnAdd = document.getElementById('btnAddEntry');
btnAdd.addEventListener('click', addEntry);
document.getElementById('hiddenFields').style.display='none';
function addEntry(){
document.getElementById('hiddenFields').style.display='';
btnAdd.style.display='none';
}
// var btnAdd = document.getElementById('btnAddEntry');
// btnAdd.addEventListener('click', addEntry);
// document.getElementById('hiddenFields').style.display='none';
// function addEntry(){
// document.getElementById('hiddenFields').style.display='';
// btnAdd.style.display='none';
// }
</script>
<script src="{% static 'scripts/editDico.js' %}"></script>
<script src="{% static 'scripts/keyboard.js' %}"></script>
......
Supports Markdown
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