-
Notifications
You must be signed in to change notification settings - Fork 0
/
journal-new.py
executable file
·76 lines (68 loc) · 2.57 KB
/
journal-new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import os, sys, codecs, json, cgi, cgitb
# les cors
cgitb.enable()
print ('Content-type: text/html; charset=utf-8')
print ('Access-Control-Allow-Origin: *')
print ('Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS')
print ('Access-Control-Allow-Headers: Content-Type')
print ("")
# récupérer les champs du formulaire
dayDict ={
"date": "",
"place": "",
"title": "",
"tags": [],
"peoples": [],
"content": []
}
form = cgi.FieldStorage()
if form.getvalue ('date'): dayDict['date'] = form.getvalue ('date').replace ('-', '/')
if form.getvalue ('place'): dayDict['place'] = form.getvalue ('place')
if form.getvalue ('persons'): dayDict['peoples'] = form.getvalue ('persons').split (', ')
if form.getvalue ('tags'): dayDict['tags'] = form.getvalue ('tags').split (', ')
if form.getvalue ('title'): dayDict['title'] = form.getvalue ('title')
if form.getvalue ('message'): dayDict['content'] = form.getvalue ('message').split ('\n')
dayDict['peoples'].sort()
dayDict['tags'].sort()
dayDict['date'] = dayDict['date'].replace ('-','/')
dayJson = json.dumps (dayDict)
print (dayJson)
# écrire l'article dans le json temporaire
jsonName = os.path.dirname (os.path.abspath (__file__)) + os.sep + 'journal-new.json'
def readFile (fileName):
text =""
if os.path.exists (fileName):
textBrut = open (fileName, 'rb')
tmpByte = textBrut.read()
encodingList =('utf-8', 'ISO-8859-1', 'ascii')
for encoding in encodingList:
try: text = codecs.decode (tmpByte, encoding=encoding)
except UnicodeDecodeError: pass
else: break;
textBrut.close()
return text
def writeFile (fileName, text):
textBrut = open (fileName, 'w')
textBrut.write (text)
textBrut.close()
def shapeJson (jsonObj):
dayString = str (jsonObj).replace ('], "', '],\n\t"')
dayString = dayString.replace ('{"', '{\n\t"')
dayString = dayString.replace (', "place', ',\n\t"place')
dayString = dayString.replace (', "title', ',\n\t"title')
dayString = dayString.replace (', "peoples', ',\n\t"peoples')
dayString = dayString.replace (', "tags', ',\n\t"tags')
dayString = dayString.replace (', "content', ',\n\t"content')
dayString = dayString.replace ('["', '[ "')
dayString = dayString.replace ('"]', '" ]')
dayString = dayString.replace ('content": [ "', 'content": [\n\t\t"')
dayString = dayString.replace ('" ]}', '"\n\t]\n}')
dayString = dayString.replace (']}', ']\n}')
dayString = dayString.replace ('}', '},\n')
return dayString
articleList = readFile (jsonName)
articleList = articleList[1:]
articleList ='['+ shapeJson (dayJson) + articleList
writeFile (jsonName, articleList)