-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheditor_metadata_service.py
executable file
·139 lines (125 loc) · 4.18 KB
/
editor_metadata_service.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import cgi
import os
import platform
from modules.gitdox_sql import *
from modules.logintools import login
import modules.redis_cache as cache
parameter = cgi.FieldStorage()
action = parameter.getvalue("action")
id = parameter.getvalue("id")
docid = parameter.getvalue("docid")
key = parameter.getvalue("key")
value = parameter.getvalue("value")
corpus = parameter.getvalue("corpus")
if platform.system() == "Windows":
prefix = "transc\\"
else:
prefix = ""
def read_options(**kwargs):
if "file" in kwargs:
kwargs["file"] = prefix + kwargs["file"]
names = open(kwargs["file"],'r').read().replace("\r","").split("\n")
names = list(name[:name.find("\t")] for name in names)
elif "names" in kwargs:
names = kwargs[names]
selected = kwargs["selected"] if "selected" in kwargs else None
return names
def row_to_dict(row):
return {'id': row[1],
'docid': row[0],
'key': row[2],
'value': row[3]}
def get_metadata():
resp = {}
try:
resp['Result'] = 'OK'
resp['Records'] = [row_to_dict(r) for r in get_doc_meta(docid, corpus=corpus)]
print json.dumps(resp)
except:
resp['Result'] = 'Error'
resp['Message'] = 'Could not fetch metadata'
print json.dumps(resp)
def get_default_key_options():
resp = {}
try:
resp['Result'] = 'OK'
if not corpus:
resp['Options'] = read_options(file='..' + os.sep + 'metadata_fields.tab')
else:
resp['Options'] = read_options(file='..' + os.sep + 'corpus_metadata_fields.tab')
print json.dumps(resp)
except:
resp['Result'] = 'Error'
resp['Message'] = 'Could not fetch metadata key options'
print json.dumps(resp)
def create_metadata():
resp = {}
try:
id = save_meta(int(docid), key.decode("utf8"), value.decode("utf8"), corpus=corpus)
resp['Result'] = 'OK'
resp['Record'] = {'id': id,
'docid': docid,
'key': key,
'value': value}
cache.invalidate_by_doc(docid, "meta")
print json.dumps(resp)
except:
resp['Result'] = 'Error'
resp['Message'] = 'Could not create metadata'
print json.dumps(resp)
def update_metadata():
resp = {}
try:
update_meta(int(id), int(docid), key.decode("utf8"), value.decode("utf8"), corpus=corpus)
resp['Result'] = 'OK'
resp['Record'] = {'id': id,
'docid': docid,
'key': key,
'value': value}
cache.invalidate_by_doc(docid, "meta")
print json.dumps(resp)
except:
resp['Result'] = 'Error'
resp['Message'] = 'Could not update metadata'
print json.dumps(resp)
def delete_metadata():
resp = {}
try:
delete_meta(int(id), int(docid), corpus=corpus)
resp['Result'] = 'OK'
cache.invalidate_by_doc(docid, "meta")
print json.dumps(resp)
except:
resp['Result'] = 'Error'
resp['Message'] = 'Could not delete metadata'
print json.dumps(resp)
def open_main_server():
thisscript = os.environ.get('SCRIPT_NAME', '')
loginaction = None
theform = cgi.FieldStorage()
scriptpath = os.path.dirname(os.path.realpath(__file__)) + os.sep
userdir = scriptpath + "users" + os.sep
loginaction, userconfig = login(theform, userdir, thisscript, loginaction)
user = userconfig["username"]
admin = userconfig["admin"]
print "Content-type:application/json\r\n\r\n"
if action == "list":
get_metadata()
elif action == "keys":
get_default_key_options()
elif user == "demo":
print json.dumps({'Result': 'Error', 'Message': 'Demo user may not make changes.'})
elif action == "create":
create_metadata()
elif action == "update":
update_metadata()
elif action == "delete":
delete_metadata()
else:
print json.dumps({'Result': 'Error',
'Message': 'Unknown action: "' + str(action) + '"'})
if __name__ == '__main__':
open_main_server()