-
Notifications
You must be signed in to change notification settings - Fork 1
/
production.py
213 lines (179 loc) · 5.29 KB
/
production.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import sys
import logging
from gevent import monkey, pywsgi
from flask import Flask, jsonify, request
from logstash_formatter import LogstashFormatterV1
from ldap_api.api import API
from ldap_api.settings import SiteSettings
from ldap_api.ldap_auth import requires_auth
debug_mode = '--debug' in sys.argv
app = Flask(__name__, static_folder='build', static_url_path='')
config = SiteSettings()
logging.basicConfig(level=logging.DEBUG)
if not debug_mode:
try:
file_handler = logging.FileHandler('/var/log/lightbook/lightbook.log')
file_handler.setFormatter(LogstashFormatterV1())
app.logger.addHandler(file_handler)
except:
pass
# Just keep going
# @app.errorhandler(Exception)
# def ldap_error_handler(e):
# app.logger.error(f'Ldap error: {e}')
#
# return jsonify({
# 'status': 'error',
# 'message': str(e)
# }), 200
#
@app.route('/')
@requires_auth
def root():
app.logger.debug("The index page was accessed.")
return app.send_static_file('index.html')
@app.route('/data/people/view/<int:person_id>')
@requires_auth
def view_person(person_id):
return jsonify({
"status": "success",
"output": {
"people": API.get_person(person_id)
}
})
@app.route('/data/people/update/<int:person_id>', methods=['PATCH', 'OPTIONS'])
@requires_auth
def update_person(person_id):
result = API.modify_person(person_id, request.get_json())
if result is None:
return jsonify({
"status": "error",
"message": "Person not found"
})
return jsonify({
"status": "success",
"output": {
"message": "Person updated successfully",
"people": result
}
})
@app.route('/data/companies/update/<int:company_id>', methods=['PATCH', 'OPTIONS'])
@requires_auth
def update_company(company_id):
result = API.modify_company(company_id, request.get_json())
if result is None:
return jsonify({
"status": "error",
"message": "Company not found"
})
return jsonify({
"status": "success",
"output": {
"message": "Company updated successfully",
"people": result
}
})
@app.route('/data/people/delete/<int:person_id>', methods=['DELETE', 'OPTIONS'])
@requires_auth
def delete_person(person_id):
if API.delete_person(person_id):
return jsonify({
'status': "success",
'output': {
'message': "Person %s deleted successfully" % person_id
}
})
else:
return jsonify({
'status': "error",
'output': {
'message': "Person %s wasn't deleted" % person_id
}
})
@app.route('/data/companies/delete/<int:company_id>', methods=['DELETE', 'OPTIONS'])
@requires_auth
def delete_company(company_id):
if API.delete_company(company_id):
return jsonify({
'status': "success",
'output': {
'message': "Company %s deleted successfully" % company_id
}
})
else:
return jsonify({
'status': "error",
'output': {
'message': "Company %s wasn't deleted" % company_id
}
})
@app.route('/data/search/get/<search>')
@requires_auth
def search_entry(search):
get_disabled = request.args.get('disabled') is not None
return jsonify({
"status": "success",
"output": {
'peoples': API.search(search, 'people', get_disabled),
'companies': API.search(search, 'companies', get_disabled)
}
})
@app.route('/data/company/<int:company_id>/people')
@requires_auth
def company_people(company_id):
only_disabled = request.args.get('only_disabled') is not None
result = API.get_people(company_id, only_disabled)
if result is None:
return jsonify({
"status": "error",
"message": "Company not found"
})
else:
return jsonify({
"status": "success",
"peoples": result
})
@app.route('/data/company/view/<int:company_id>')
@requires_auth
def view_company(company_id):
return jsonify({
"status": "success",
"output": {
"company": API.get_company(company_id)
}
})
@app.route('/data/people/add', methods=['POST', 'OPTIONS'])
@requires_auth
def add_person():
return jsonify({
"status": "success",
"output": {
"people": API.add_person(request.get_json())
}
})
@app.route('/data/company/add', methods=['POST', 'OPTIONS'])
@requires_auth
def add_company():
return jsonify({
"status": "success",
"output": {
"company": API.add_company(request.get_json())
}
})
@app.route('/data/companies/search/<search>')
@requires_auth
def search_company(search):
get_disabled = request.args.get('disabled') is not None
return jsonify({
"status": "success",
"output": {
'companies': API.search(search, 'companies', get_disabled)
}
})
if __name__ == '__main__':
if debug_mode:
app.run(debug=True)
else:
monkey.patch_all()
server = pywsgi.WSGIServer((config.get_bind_ip(), config.get_bind_port()), app)
server.serve_forever()