views.py 3.59 KB
Newer Older
Sylvain Coulange's avatar
Sylvain Coulange committed
1
2
from django.shortcuts import render, HttpResponseRedirect
from static.scripts import textPhonographer as txtphono
3
from .models import Phonograph, Data, Entree, commitData
4
5
from django.http import JsonResponse
import json
Sylvain Coulange's avatar
Sylvain Coulange committed
6
import spacy
7
import subprocess
8
import re
9

10
11
nlpFr = spacy.load('fr')
#nlpEn = spacy.load('en')
12
logFile = "../logs/dico_frwiktionary-20200301_v2.log"
Sylvain Coulange's avatar
Sylvain Coulange committed
13
14

def redirApp(request):
Sylvain Coulange's avatar
Sylvain Coulange committed
15
    return HttpResponseRedirect('/')
Sylvain Coulange's avatar
Sylvain Coulange committed
16
17

def main(request):
18
19
    data = Data()
    data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
20
21
    return render(request, 'coloriseur.html', {'data': data})

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def colorize(request):
    colis = json.loads(request.body)
    text = colis['inText']
    lang = colis['lang']
    nlpText = nlpFr(text)
    outText = []

    #punctuation = [',','.'] # ')',']','}','%','>',"'",'(','[','{'

    for token in nlpText:
        sdl = re.findall(r'\r\n',token.text)
        print("sdl =",sdl)
        if len(sdl) > 0:
            for s in sdl:
                print("Saut de ligne.")
                outText.append('§')
        else:
            print("Mot en entrée :",token.text)
            result = txtphono.mimi(token.text)
            print(result)
        
            phonographieList = []
            for r in result:      
                phonographie = []
                for i in r:
                    ph = {}
                    ph['phon'] = i[0]
                    ph['graph'] = i[1]
                    phonographie.append(ph)
                phonographieList.append(phonographie)
            outText.append(phonographieList)
    rep = {
        'outText': outText
    }
    return JsonResponse(rep)

58
59
def updateTime():
    upd = str(subprocess.check_output(["git", "log", "-1", "--format=%cd", "--date=short"]))
60
    #ver = str(subprocess.check_output(["git", "rev-list", "--all", "--count"]))
61
    return 'Version 3.0 (mis à jour le '+upd[2:-3]+')'
Sylvain Coulange's avatar
Sylvain Coulange committed
62

63
64
65
def main0(request):
    data = Data()
    data.text = ''
Sylvain Coulange's avatar
Sylvain Coulange committed
66
    data.pphh = [[[]]]
67
    data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
68
    data.colorType = 'sw'
69
    data.defaultBg = 'black'
70
71
72
    return render(request, 'coloriseur.html', {'data': data})


Sylvain Coulange's avatar
Sylvain Coulange committed
73
74
def dicoView(request):
    data = Entree()
75
    data.update = updateTime()
76
    data.lenDic = txtphono.getLenDic()
77
    data.logStat = txtphono.getLogStat()
Sylvain Coulange's avatar
Sylvain Coulange committed
78
79
    return render(request, 'editDico.html', {'data': data})

80
81
82
83
84
85
86
def dicoReq(request):
    listeEntrees = []
    mot = request.GET.get('mot', None)
    trans = request.GET.get('trans', None)
    motCond = request.GET.get('motCond', None)
    transCond = request.GET.get('transCond', None)
    
Sylvain Coulange's avatar
Sylvain Coulange committed
87
88
89
90
91
92
93
94
95
96
97
98
    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
99

Sylvain Coulange's avatar
Sylvain Coulange committed
100
101
102
103
104
105
106
107
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)
Sylvain Coulange's avatar
Sylvain Coulange committed
108

109
def newEntry(request):
110
111
112
113
114
115
116
117
    mot = request.GET.get('mot', None)
    trans = request.GET.get('trans', None)
    ipCli = get_client_ip(request)
    rep = {
        'reponse': txtphono.addEntry(ipCli,mot,trans)
    }
    return JsonResponse(rep)

118
119
120
121
122
123
124
125
def supprEntry(request):
    mot = request.GET.get('mot', None)
    ipCli = get_client_ip(request)
    rep = {
        'reponse': txtphono.delEntry(ipCli,mot)
    }
    return JsonResponse(rep)

126
127
def getLog(request):
    rep = txtphono.sendMeLogPlease()
128
    return JsonResponse(rep)