views.py 3.54 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

Sylvain Coulange's avatar
Sylvain Coulange committed
10
11
12
13
14
15
16
17
18
19
20
nlp = spacy.load('fr')

def redirApp(request):
    return HttpResponseRedirect('/app/')

def main(request):
    if request.POST.get('inText'):
        text = request.POST['inText']
        nlpText = nlp(text)
        data = Data()
        data.text = text
21
        data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
22
        data.colorType = request.POST.get('colorType')
23
        data.defaultBg = request.POST.get('defaultBg')
Sylvain Coulange's avatar
Sylvain Coulange committed
24
25
        data.pphh = []

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

Sylvain Coulange's avatar
Sylvain Coulange committed
28
        for token in nlpText:
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
            sdl = re.findall(r'\r\n',token.text)
            print("sdl =",sdl)
            if len(sdl) > 0:
                for s in sdl:
                    print("Saut de ligne.")
                    data.pphh.append('§')
            elif token.text in punctuation:
                data.pphh.append(token.text)
            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 = Phonograph()
                        ph.phon = i[0]
                        ph.graph = i[1]
                        phonographie.append(ph)
                    phonographieList.append(phonographie)
                data.pphh.append(phonographieList)
Sylvain Coulange's avatar
Sylvain Coulange committed
52
53
54
    else:
        data = Data()
        data.text = ''
Sylvain Coulange's avatar
Sylvain Coulange committed
55
        data.pphh = [[[]]]
56
        data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
57
        data.colorType = 'sw'
58
        data.defaultBg = 'black'
Sylvain Coulange's avatar
Sylvain Coulange committed
59
60
    return render(request, 'coloriseur.html', {'data': data})

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

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


Sylvain Coulange's avatar
Sylvain Coulange committed
76
77
def dicoView(request):
    data = Entree()
78
    data.update = updateTime()
Sylvain Coulange's avatar
Sylvain Coulange committed
79
80
    return render(request, 'editDico.html', {'data': data})

81
82
83
84
85
86
87
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
88
89
90
91
92
93
94
95
96
97
98
99
    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
100

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


111
112
113
114
""" def commits(request):
    data = commitData()
    data.update = updateTime()
    data.listCommits = subprocess.check_output(["git", "log", "--pretty=full"]).decode()
Sylvain Coulange's avatar
Sylvain Coulange committed
115
    return render(request, 'commits.html', {'data': data}) """