-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
186 lines (160 loc) · 6.09 KB
/
server.py
File metadata and controls
186 lines (160 loc) · 6.09 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import asyncio
import json
import os
from crypt import Bcrypt
from datetime import datetime
import numpy as np
import pandas as pd
import cherrypy
def convert(o):
if isinstance(o, np.int64):
return int(o)
if isinstance(o, np.float64):
return float(o)
class HomePage(object):
@cherrypy.expose
def index(self):
return open('./static/client.html')
@cherrypy.expose
class APIv1(object):
def __init__(self):
with open("./config.json") as f, open("./default-config.json") as df:
user_config = json.loads(f.read())
default_config = json.loads(df.read())
self.config = {**default_config, **user_config}
self.inputFile = self.config['inputFile']
self.outputFile = self.config['outputFile']
self.fieldNames = [self.config['idField']
] + [f['fieldName'] for f in self.config['fields']]
self.fieldAliases = ['id'] + [
f['fieldAlias'] for f in self.config['fields']
]
if self.config['updateTime']:
self.fieldNames.append("updatedAt")
self.ntoa = dict(zip(self.fieldNames, self.fieldAliases))
self.aton = dict(zip(self.fieldAliases, self.fieldNames))
self.ntoa[self.config['idField']] = 'id'
self.aton['id'] = self.config['idField']
self.inputDf = pd.read_excel(self.inputFile)
if os.path.exists(self.outputFile):
self.outputDf = pd.read_excel(self.outputFile)
else:
self.outputDf = pd.DataFrame(columns=self.fieldNames)
self.allColumns = list(
set(self.inputDf.columns) | set(self.outputDf.columns))
idField = self.config['idField']
self.inputDf.set_index(idField, inplace=True)
self.outputDf.set_index(idField, inplace=True)
self.inputDf.index = self.inputDf.index.astype(str)
self.outputDf.index = self.outputDf.index.astype(str)
@cherrypy.tools.accept(media='application/json')
@cherrypy.tools.json_in()
def POST(self):
data = cherrypy.request.json
action = data['action']
variables = data['variables']
if action == "AUTH_STUDENT_QUERY":
try:
res = self.authStudentQuery(variables)
except Exception as e:
res = self.done(None, [{
"type": "Exception",
"message": str(e),
}])
return res
if action == "UPDATE_STUDENT_MUTATION":
try:
res = self.updateStudentMutation(variables)
except Exception as e:
res = self.done(None, [{
"type": "Exception",
"message": str(e),
}])
return res
def authStudentQuery(self, data):
pwField = self.config['passwordField']
inputDfAlias = self.inputDf.rename(self.ntoa, axis=1)
outputDfAlias = self.outputDf.rename(self.ntoa, axis=1)
outputFields = self.ntoa.values()
errors = []
if data['id'] in inputDfAlias.index:
student = inputDfAlias.loc[data['id']].copy()
if data['id'] in outputDfAlias.index: # If record exists, combine
student = outputDfAlias.loc[data['id']].combine_first(student)
student['id'] = data['id']
else:
errors.append({"type": "NotExistError", "message": "ID not exist"})
return self.done(None, errors)
if (self.checkPassword(data['pw'], student[pwField])):
# TODO: Simplify Series -> json -> dict -> json procedure
data = json.loads(student.reindex(outputFields).to_json())
return self.done(data)
else:
errors.append({
"type": "PasswordError",
"message": "WrongPassword"
})
return self.done(None, errors)
def updateStudentMutation(self, data):
ser = pd.Series(data)
ser.name = ser.id
del ser['id']
if self.config['updateTime']:
ser['updatedAt'] = datetime.now().isoformat()
if ser.name not in self.inputDf.index:
return self.done(None, [{
"type": "NotExistError",
"message": "ID not exist",
}])
if ser.name in self.outputDf.index:
self.outputDf.loc[ser.name] = ser.rename(self.aton)
else:
self.outputDf = self.outputDf.append(ser.rename(self.aton))
self.save_output()
return self.done(data)
def checkPassword(self, pwraw, pwenc):
encryption = self.config['encryption']
if encryption == 'none':
return pwraw == pwenc
if encryption == 'bcrypt':
return Bcrypt.check_password(pwraw, pwenc)
def done(self, data, errors=None):
return json.dumps(
{
"data": data,
"errors": errors,
}, default=convert).encode("utf-8")
def save_output(self):
if (self.config['mergeInput']):
outputIndex = self.inputDf.index
outputDf = self.outputDf.reindex(
index=outputIndex, columns=self.allColumns)
inputDf = self.inputDf.reindex(
index=outputIndex, columns=self.allColumns)
output = outputDf.combine_first(inputDf)
output.to_excel(self.outputFile)
else:
self.outputDf.to_excel(self.outputFile)
if __name__ == '__main__':
conf = {
'/': {
'tools.staticdir.root': os.path.abspath(os.getcwd()),
},
'/apiv1': {
'request.dispatch':
cherrypy.dispatch.MethodDispatcher(),
'tools.response_headers.on':
True,
'tools.response_headers.headers': [('Content-Type',
'application/json')],
'tools.encode.on':
True,
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './static',
},
}
webapp = HomePage()
webapp.apiv1 = APIv1()
cherrypy.quickstart(webapp, '/', conf)