1
|
|
|
import falcon |
2
|
|
|
from datetime import datetime, timedelta |
3
|
|
|
import mysql.connector |
4
|
|
|
import simplejson as json |
5
|
|
|
from core.useractivity import admin_control, access_control, api_key_control |
6
|
|
|
import config |
7
|
|
|
|
8
|
|
|
|
9
|
|
View Code Duplication |
class WorkingCalendarCollection: |
|
|
|
|
10
|
|
|
def __init__(self): |
11
|
|
|
""""Initializes WorkingCalendarCollection""" |
12
|
|
|
pass |
13
|
|
|
|
14
|
|
|
@staticmethod |
15
|
|
|
def on_options(req, resp): |
16
|
|
|
_ = req |
17
|
|
|
resp.status = falcon.HTTP_200 |
18
|
|
|
|
19
|
|
|
@staticmethod |
20
|
|
|
def on_get(req, resp): |
21
|
|
|
if 'API-KEY' not in req.headers or \ |
22
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
23
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
24
|
|
|
access_control(req) |
25
|
|
|
else: |
26
|
|
|
api_key_control(req) |
27
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
28
|
|
|
cursor = cnx.cursor() |
29
|
|
|
|
30
|
|
|
cursor.execute(" SELECT id, name, description" |
31
|
|
|
" FROM tbl_working_calendars ") |
32
|
|
|
|
33
|
|
|
rows_calendars = cursor.fetchall() |
34
|
|
|
|
35
|
|
|
result = list() |
36
|
|
|
if rows_calendars is not None and len(rows_calendars) > 0: |
37
|
|
|
for row in rows_calendars: |
38
|
|
|
meta_result = {"id": row[0], |
39
|
|
|
"name": row[1], |
40
|
|
|
"description": row[2]} |
41
|
|
|
result.append(meta_result) |
42
|
|
|
|
43
|
|
|
cursor.close() |
44
|
|
|
cnx.close() |
45
|
|
|
resp.text = json.dumps(result) |
46
|
|
|
|
47
|
|
|
@staticmethod |
48
|
|
|
def on_post(req, resp): |
49
|
|
|
"""Handles POST requests""" |
50
|
|
|
admin_control(req) |
51
|
|
|
try: |
52
|
|
|
raw_json = req.stream.read().decode('utf-8') |
53
|
|
|
new_values = json.loads(raw_json) |
54
|
|
|
except Exception as ex: |
55
|
|
|
print(str(ex)) |
56
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
57
|
|
|
title='API.BAD_REQUEST', |
58
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
59
|
|
|
|
60
|
|
|
if 'name' not in new_values['data'].keys() or \ |
61
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
62
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
63
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
64
|
|
|
description='API.INVALID_WORKING_CALENDAR_NAME') |
65
|
|
|
name = str.strip(new_values['data']['name']) |
66
|
|
|
|
67
|
|
|
if 'description' in new_values['data'].keys() and \ |
68
|
|
|
new_values['data']['description'] is not None and \ |
69
|
|
|
len(str(new_values['data']['description'])) > 0: |
70
|
|
|
description = str.strip(new_values['data']['description']) |
71
|
|
|
else: |
72
|
|
|
description = None |
73
|
|
|
|
74
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
75
|
|
|
cursor = cnx.cursor() |
76
|
|
|
|
77
|
|
|
cursor.execute(" SELECT name " |
78
|
|
|
" FROM tbl_working_calendars " |
79
|
|
|
" WHERE name = %s ", (name,)) |
80
|
|
|
if cursor.fetchone() is not None: |
81
|
|
|
cursor.close() |
82
|
|
|
cnx.close() |
83
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
84
|
|
|
description='API.WORKING_CALENDAR_NAME_IS_ALREADY_IN_USE') |
85
|
|
|
|
86
|
|
|
add_values = (" INSERT INTO tbl_working_calendars " |
87
|
|
|
" (name, description) " |
88
|
|
|
" VALUES (%s, %s) ") |
89
|
|
|
cursor.execute(add_values, (name, |
90
|
|
|
description)) |
91
|
|
|
new_id = cursor.lastrowid |
92
|
|
|
cnx.commit() |
93
|
|
|
cursor.close() |
94
|
|
|
cnx.close() |
95
|
|
|
|
96
|
|
|
resp.status = falcon.HTTP_201 |
97
|
|
|
resp.location = '/workingcalendar/' + str(new_id) |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
class WorkingCalendarItem: |
101
|
|
|
def __init__(self): |
102
|
|
|
""""Initializes WorkingCalendarItem""" |
103
|
|
|
pass |
104
|
|
|
|
105
|
|
|
@staticmethod |
106
|
|
|
def on_options(req, resp, id_): |
107
|
|
|
_ = req |
108
|
|
|
resp.status = falcon.HTTP_200 |
109
|
|
|
_ = id_ |
110
|
|
|
|
111
|
|
|
@staticmethod |
112
|
|
|
def on_get(req, resp, id_): |
113
|
|
|
if 'API-KEY' not in req.headers or \ |
114
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
115
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
116
|
|
|
access_control(req) |
117
|
|
|
else: |
118
|
|
|
api_key_control(req) |
119
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
120
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
121
|
|
|
description='API.INVALID_WORKING_CALENDAR_ID') |
122
|
|
|
|
123
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
124
|
|
|
cursor = cnx.cursor() |
125
|
|
|
|
126
|
|
|
cursor.execute(" SELECT id, name, description" |
127
|
|
|
" FROM tbl_working_calendars " |
128
|
|
|
" WHERE id = %s ", (id_,)) |
129
|
|
|
row = cursor.fetchone() |
130
|
|
|
cursor.close() |
131
|
|
|
cnx.close() |
132
|
|
|
|
133
|
|
|
if row is None: |
134
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
135
|
|
|
description='API.WORKING_CALENDAR_NOT_FOUND') |
136
|
|
|
|
137
|
|
|
meta_result = {"id": row[0], |
138
|
|
|
"name": row[1], |
139
|
|
|
"description": row[2]} |
140
|
|
|
resp.text = json.dumps(meta_result) |
141
|
|
|
|
142
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
143
|
|
|
def on_delete(req, resp, id_): |
144
|
|
|
admin_control(req) |
145
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
146
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
147
|
|
|
description='API.INVALID_WORKING_CALENDAR_ID') |
148
|
|
|
|
149
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
150
|
|
|
cursor = cnx.cursor() |
151
|
|
|
|
152
|
|
|
cursor.execute(" SELECT id " |
153
|
|
|
" FROM tbl_working_calendars " |
154
|
|
|
" WHERE id = %s ", (id_,)) |
155
|
|
|
if cursor.fetchone() is None: |
156
|
|
|
cursor.close() |
157
|
|
|
cnx.close() |
158
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
159
|
|
|
description='API.WORKING_CALENDAR_NOT_FOUND') |
160
|
|
|
|
161
|
|
|
# check relation with space |
162
|
|
|
cursor.execute(" SELECT id FROM tbl_spaces_working_calendars" |
163
|
|
|
" WHERE working_calendar_id = %s ", (id_,)) |
164
|
|
|
|
165
|
|
|
rows_non_working_days = cursor.fetchall() |
166
|
|
|
if rows_non_working_days is not None and len(rows_non_working_days) > 0: |
167
|
|
|
cursor.close() |
168
|
|
|
cnx.close() |
169
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
170
|
|
|
title='API.BAD_REQUEST', |
171
|
|
|
description='API.THERE_IS_RELATION_WITH_SPACES') |
172
|
|
|
|
173
|
|
|
# check relation with tenants |
174
|
|
|
cursor.execute(" SELECT tenant_id " |
175
|
|
|
" FROM tbl_tenants_working_calendars " |
176
|
|
|
" WHERE working_calendar_id = %s ", (id_,)) |
177
|
|
|
rows_tenants = cursor.fetchall() |
178
|
|
|
if rows_tenants is not None and len(rows_tenants) > 0: |
179
|
|
|
cursor.close() |
180
|
|
|
cnx.close() |
181
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
182
|
|
|
title='API.BAD_REQUEST', |
183
|
|
|
description='API.THERE_IS_RELATION_WITH_TENANTS') |
184
|
|
|
|
185
|
|
|
# check relation with stores |
186
|
|
|
cursor.execute(" SELECT store_id " |
187
|
|
|
" FROM tbl_stores_working_calendars " |
188
|
|
|
" WHERE working_calendar_id = %s ", (id_,)) |
189
|
|
|
rows_stores = cursor.fetchall() |
190
|
|
|
if rows_stores is not None and len(rows_stores) > 0: |
191
|
|
|
cursor.close() |
192
|
|
|
cnx.close() |
193
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
194
|
|
|
title='API.BAD_REQUEST', |
195
|
|
|
description='API.THERE_IS_RELATION_WITH_STORES') |
196
|
|
|
|
197
|
|
|
# check relation with shopfloors |
198
|
|
|
cursor.execute(" SELECT shopfloor_id " |
199
|
|
|
" FROM tbl_shopfloors_working_calendars " |
200
|
|
|
" WHERE working_calendar_id = %s ", (id_,)) |
201
|
|
|
rows_shopfloors = cursor.fetchall() |
202
|
|
|
if rows_shopfloors is not None and len(rows_shopfloors) > 0: |
203
|
|
|
cursor.close() |
204
|
|
|
cnx.close() |
205
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
206
|
|
|
title='API.BAD_REQUEST', |
207
|
|
|
description='API.THERE_IS_RELATION_WITH_SHOPFLOORS') |
208
|
|
|
|
209
|
|
|
cursor.execute(" DELETE FROM tbl_working_calendars_non_working_days " |
210
|
|
|
" WHERE working_calendar_id = %s ", (id_,)) |
211
|
|
|
cnx.commit() |
212
|
|
|
|
213
|
|
|
cursor.execute(" DELETE FROM tbl_working_calendars WHERE id = %s ", (id_,)) |
214
|
|
|
cnx.commit() |
215
|
|
|
|
216
|
|
|
cursor.close() |
217
|
|
|
cnx.close() |
218
|
|
|
|
219
|
|
|
resp.status = falcon.HTTP_204 |
220
|
|
|
|
221
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
222
|
|
|
def on_put(req, resp, id_): |
223
|
|
|
"""Handles PUT requests""" |
224
|
|
|
admin_control(req) |
225
|
|
|
try: |
226
|
|
|
raw_json = req.stream.read().decode('utf-8') |
227
|
|
|
except Exception as ex: |
228
|
|
|
print(str(ex)) |
229
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
230
|
|
|
title='API.BAD_REQUEST', |
231
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
232
|
|
|
|
233
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
234
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
235
|
|
|
description='API.INVALID_WORKING_CALENDAR_ID') |
236
|
|
|
|
237
|
|
|
new_values = json.loads(raw_json) |
238
|
|
|
|
239
|
|
|
if 'name' not in new_values['data'].keys() or \ |
240
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
241
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
242
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
243
|
|
|
description='API.INVALID_WORKING_CALENDAR_NAME') |
244
|
|
|
name = str.strip(new_values['data']['name']) |
245
|
|
|
|
246
|
|
|
if 'description' in new_values['data'].keys() and \ |
247
|
|
|
new_values['data']['description'] is not None and \ |
248
|
|
|
len(str(new_values['data']['description'])) > 0: |
249
|
|
|
description = str.strip(new_values['data']['description']) |
250
|
|
|
else: |
251
|
|
|
description = None |
252
|
|
|
|
253
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
254
|
|
|
cursor = cnx.cursor() |
255
|
|
|
|
256
|
|
|
cursor.execute(" SELECT name " |
257
|
|
|
" FROM tbl_working_calendars " |
258
|
|
|
" WHERE id = %s ", (id_,)) |
259
|
|
|
if cursor.fetchone() is None: |
260
|
|
|
cursor.close() |
261
|
|
|
cnx.close() |
262
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
263
|
|
|
description='API.WORKING_CALENDAR_NOT_FOUND') |
264
|
|
|
|
265
|
|
|
cursor.execute(" SELECT name " |
266
|
|
|
" FROM tbl_working_calendars " |
267
|
|
|
" WHERE name = %s AND id != %s ", (name, id_)) |
268
|
|
|
if cursor.fetchone() is not None: |
269
|
|
|
cursor.close() |
270
|
|
|
cnx.close() |
271
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
272
|
|
|
description='API.WORKING_CALENDAR_NAME_IS_ALREADY_IN_USE') |
273
|
|
|
|
274
|
|
|
update_row = (" UPDATE tbl_working_calendars " |
275
|
|
|
" SET name = %s, description = %s " |
276
|
|
|
" WHERE id = %s ") |
277
|
|
|
cursor.execute(update_row, (name, description, id_)) |
278
|
|
|
cnx.commit() |
279
|
|
|
|
280
|
|
|
cursor.close() |
281
|
|
|
cnx.close() |
282
|
|
|
|
283
|
|
|
resp.status = falcon.HTTP_200 |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
class NonWorkingDayCollection: |
287
|
|
|
def __init__(self): |
288
|
|
|
""""Initializes NonWorkingDayCollection""" |
289
|
|
|
pass |
290
|
|
|
|
291
|
|
|
@staticmethod |
292
|
|
|
def on_options(req, resp, id_): |
293
|
|
|
_ = req |
294
|
|
|
resp.status = falcon.HTTP_200 |
295
|
|
|
_ = id_ |
296
|
|
|
|
297
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
298
|
|
|
def on_get(req, resp, id_): |
299
|
|
|
if 'API-KEY' not in req.headers or \ |
300
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
301
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
302
|
|
|
access_control(req) |
303
|
|
|
else: |
304
|
|
|
api_key_control(req) |
305
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
306
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
307
|
|
|
description='API.INVALID_NON_WORKING_DAY_ID') |
308
|
|
|
|
309
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
310
|
|
|
cursor = cnx.cursor() |
311
|
|
|
|
312
|
|
|
cursor.execute(" SELECT id, working_calendar_id, date_local, description" |
313
|
|
|
" FROM tbl_working_calendars_non_working_days " |
314
|
|
|
" WHERE working_calendar_id = %s " |
315
|
|
|
" ORDER BY date_local DESC ", (id_,)) |
316
|
|
|
rows_date_local = cursor.fetchall() |
317
|
|
|
|
318
|
|
|
meta_result = list() |
319
|
|
|
if rows_date_local is not None and len(rows_date_local) > 0: |
320
|
|
|
for row in rows_date_local: |
321
|
|
|
date_local_dict = {'id': row[0], |
322
|
|
|
'working_calendar_id': row[1], |
323
|
|
|
'date_local': row[2].isoformat()[0:10], |
324
|
|
|
'description': row[3]} |
325
|
|
|
meta_result.append(date_local_dict) |
326
|
|
|
|
327
|
|
|
cursor.close() |
328
|
|
|
cnx.close() |
329
|
|
|
|
330
|
|
|
resp.text = json.dumps(meta_result) |
331
|
|
|
|
332
|
|
|
@staticmethod |
333
|
|
|
def on_post(req, resp, id_): |
334
|
|
|
"""Handles POST requests""" |
335
|
|
|
admin_control(req) |
336
|
|
|
try: |
337
|
|
|
raw_json = req.stream.read().decode('utf-8') |
338
|
|
|
new_values = json.loads(raw_json) |
339
|
|
|
except Exception as ex: |
340
|
|
|
print(str(ex)) |
341
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
342
|
|
|
title='API.BAD_REQUEST', |
343
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
344
|
|
|
|
345
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
346
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
347
|
|
|
description='API.INVALID_WORKING_CALENDAR_ID') |
348
|
|
|
working_calendar_id = id_ |
349
|
|
|
|
350
|
|
|
if 'date_local' not in new_values['data'].keys() or \ |
351
|
|
|
new_values['data']['date_local'] is None or \ |
352
|
|
|
len(str(new_values['data']['date_local'])) <= 0: |
353
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
354
|
|
|
description='API.INVALID_DATE_LOCAL') |
355
|
|
|
date_local = str.strip(new_values['data']['date_local']) |
356
|
|
|
|
357
|
|
|
if 'description' in new_values['data'].keys() and \ |
358
|
|
|
new_values['data']['description'] is not None and \ |
359
|
|
|
len(str(new_values['data']['description'])) > 0: |
360
|
|
|
description = str.strip(new_values['data']['description']) |
361
|
|
|
else: |
362
|
|
|
description = None |
363
|
|
|
|
364
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
365
|
|
|
cursor = cnx.cursor() |
366
|
|
|
|
367
|
|
|
cursor.execute(" SELECT id " |
368
|
|
|
" FROM tbl_working_calendars_non_working_days " |
369
|
|
|
" WHERE working_calendar_id = %s AND date_local = %s ", |
370
|
|
|
(working_calendar_id, date_local)) |
371
|
|
|
if cursor.fetchone() is not None: |
372
|
|
|
cursor.close() |
373
|
|
|
cnx.close() |
374
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
375
|
|
|
description='API.DATE_IS_ALREADY_IN_WORKING_CALENDAR') |
376
|
|
|
|
377
|
|
|
add_values = (" INSERT INTO tbl_working_calendars_non_working_days " |
378
|
|
|
" (working_calendar_id, date_local, description) " |
379
|
|
|
" VALUES (%s, %s, %s) ") |
380
|
|
|
cursor.execute(add_values, (working_calendar_id, date_local, description)) |
381
|
|
|
new_id = cursor.lastrowid |
382
|
|
|
cnx.commit() |
383
|
|
|
cursor.close() |
384
|
|
|
cnx.close() |
385
|
|
|
|
386
|
|
|
resp.status = falcon.HTTP_201 |
387
|
|
|
resp.location = '/nonworkingday/' + str(new_id) |
388
|
|
|
|
389
|
|
|
|
390
|
|
|
class NonWorkingDayItem: |
391
|
|
|
def __init__(self): |
392
|
|
|
""""Initializes NonWorkingDayItem""" |
393
|
|
|
pass |
394
|
|
|
|
395
|
|
|
@staticmethod |
396
|
|
|
def on_options(req, resp, id_): |
397
|
|
|
_ = req |
398
|
|
|
resp.status = falcon.HTTP_200 |
399
|
|
|
_ = id_ |
400
|
|
|
|
401
|
|
|
@staticmethod |
402
|
|
|
def on_get(req, resp, id_): |
403
|
|
|
if 'API-KEY' not in req.headers or \ |
404
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
405
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
406
|
|
|
access_control(req) |
407
|
|
|
else: |
408
|
|
|
api_key_control(req) |
409
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
410
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
411
|
|
|
description='API.INVALID_NON_WORKING_DAY_ID') |
412
|
|
|
|
413
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
414
|
|
|
cursor = cnx.cursor() |
415
|
|
|
|
416
|
|
|
cursor.execute(" SELECT id, working_calendar_id, date_local, description" |
417
|
|
|
" FROM tbl_working_calendars_non_working_days " |
418
|
|
|
" WHERE id = %s ", (id_,)) |
419
|
|
|
row = cursor.fetchone() |
420
|
|
|
cursor.close() |
421
|
|
|
cnx.close() |
422
|
|
|
|
423
|
|
|
if row is None: |
424
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
425
|
|
|
description='API.NON_WORKING_DAY_NOT_FOUND') |
426
|
|
|
else: |
427
|
|
|
meta_result = {"id": row[0], |
428
|
|
|
"working_calendar_id": row[1], |
429
|
|
|
"date_local": row[2].isoformat()[0:10], |
430
|
|
|
"description": row[3]} |
431
|
|
|
resp.text = json.dumps(meta_result) |
432
|
|
|
|
433
|
|
|
@staticmethod |
434
|
|
|
def on_delete(req, resp, id_): |
435
|
|
|
admin_control(req) |
436
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
437
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
438
|
|
|
description='API.INVALID_NON_WORKING_DAY_ID') |
439
|
|
|
|
440
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
441
|
|
|
cursor = cnx.cursor() |
442
|
|
|
|
443
|
|
|
cursor.execute(" SELECT id " |
444
|
|
|
" FROM tbl_working_calendars_non_working_days " |
445
|
|
|
" WHERE id = %s ", (id_,)) |
446
|
|
|
if cursor.fetchone() is None: |
447
|
|
|
cursor.close() |
448
|
|
|
cnx.close() |
449
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
450
|
|
|
description='API.NON_WORKING_DAY_NOT_FOUND') |
451
|
|
|
|
452
|
|
|
cursor.execute(" DELETE FROM tbl_working_calendars_non_working_days WHERE id = %s ", (id_,)) |
453
|
|
|
cnx.commit() |
454
|
|
|
|
455
|
|
|
cursor.close() |
456
|
|
|
cnx.close() |
457
|
|
|
|
458
|
|
|
resp.status = falcon.HTTP_204 |
459
|
|
|
|
460
|
|
|
@staticmethod |
461
|
|
|
def on_put(req, resp, id_): |
462
|
|
|
"""Handles PUT requests""" |
463
|
|
|
admin_control(req) |
464
|
|
|
try: |
465
|
|
|
raw_json = req.stream.read().decode('utf-8') |
466
|
|
|
except Exception as ex: |
467
|
|
|
print(str(ex)) |
468
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
469
|
|
|
title='API.BAD_REQUEST', |
470
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
471
|
|
|
|
472
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
473
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
474
|
|
|
description='API.INVALID_NON_WORKING_DAY_ID') |
475
|
|
|
|
476
|
|
|
new_values = json.loads(raw_json) |
477
|
|
|
|
478
|
|
|
if 'date_local' in new_values['data'].keys() and \ |
479
|
|
|
new_values['data']['date_local'] is not None and \ |
480
|
|
|
len(str(new_values['data']['date_local'])) > 0: |
481
|
|
|
date_local = str.strip(new_values['data']['date_local']) |
482
|
|
|
|
483
|
|
|
if 'description' in new_values['data'].keys() and \ |
484
|
|
|
new_values['data']['description'] is not None and \ |
485
|
|
|
len(str(new_values['data']['description'])) > 0: |
486
|
|
|
description = str.strip(new_values['data']['description']) |
487
|
|
|
else: |
488
|
|
|
description = None |
489
|
|
|
|
490
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
491
|
|
|
cursor = cnx.cursor() |
492
|
|
|
|
493
|
|
|
cursor.execute(" SELECT date_local " |
494
|
|
|
" FROM tbl_working_calendars_non_working_days " |
495
|
|
|
" WHERE id = %s ", (id_,)) |
496
|
|
|
if cursor.fetchone() is None: |
497
|
|
|
cursor.close() |
498
|
|
|
cnx.close() |
499
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
500
|
|
|
description='API.DATE_LOCAL_NOT_FOUND') |
501
|
|
|
|
502
|
|
|
cursor.execute(" SELECT id " |
503
|
|
|
" FROM tbl_working_calendars_non_working_days " |
504
|
|
|
" WHERE id != %s AND date_local = %s ", |
505
|
|
|
(id_, date_local)) |
|
|
|
|
506
|
|
|
if cursor.fetchone() is not None: |
507
|
|
|
cursor.close() |
508
|
|
|
cnx.close() |
509
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
510
|
|
|
description='API.DATE_IS_ALREADY_IN_WORKING_CALENDAR') |
511
|
|
|
|
512
|
|
|
update_row = (" UPDATE tbl_working_calendars_non_working_days " |
513
|
|
|
" SET date_local = %s, description = %s " |
514
|
|
|
" WHERE id = %s ") |
515
|
|
|
cursor.execute(update_row, (date_local, description, id_)) |
516
|
|
|
cnx.commit() |
517
|
|
|
|
518
|
|
|
cursor.close() |
519
|
|
|
cnx.close() |
520
|
|
|
|
521
|
|
|
resp.status = falcon.HTTP_200 |
522
|
|
|
|
523
|
|
|
|
524
|
|
View Code Duplication |
class WorkingCalendarExport: |
|
|
|
|
525
|
|
|
def __init__(self): |
526
|
|
|
""""Initializes WorkingCalendarExport""" |
527
|
|
|
pass |
528
|
|
|
|
529
|
|
|
@staticmethod |
530
|
|
|
def on_options(req, resp, id_): |
531
|
|
|
_ = req |
532
|
|
|
resp.status = falcon.HTTP_200 |
533
|
|
|
_ = id_ |
534
|
|
|
|
535
|
|
|
@staticmethod |
536
|
|
|
def on_get(req, resp, id_): |
537
|
|
|
if 'API-KEY' not in req.headers or \ |
538
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
539
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
540
|
|
|
access_control(req) |
541
|
|
|
else: |
542
|
|
|
api_key_control(req) |
543
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
544
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
545
|
|
|
description='API.INVALID_WORKING_CALENDAR_ID') |
546
|
|
|
|
547
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
548
|
|
|
cursor = cnx.cursor() |
549
|
|
|
|
550
|
|
|
cursor.execute(" SELECT id, name, description" |
551
|
|
|
" FROM tbl_working_calendars " |
552
|
|
|
" WHERE id = %s ", (id_,)) |
553
|
|
|
row = cursor.fetchone() |
554
|
|
|
|
555
|
|
|
if row is None: |
556
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
557
|
|
|
description='API.WORKING_CALENDAR_NOT_FOUND') |
558
|
|
|
|
559
|
|
|
meta_result = {"id": row[0], |
560
|
|
|
"name": row[1], |
561
|
|
|
"description": row[2], |
562
|
|
|
"non_working_days": None} |
563
|
|
|
|
564
|
|
|
cursor.execute(" SELECT id, working_calendar_id, date_local, description" |
565
|
|
|
" FROM tbl_working_calendars_non_working_days " |
566
|
|
|
" WHERE working_calendar_id = %s ", (id_,)) |
567
|
|
|
rows_date_local = cursor.fetchall() |
568
|
|
|
|
569
|
|
|
result = list() |
570
|
|
|
if rows_date_local is not None and len(rows_date_local) > 0: |
571
|
|
|
for row in rows_date_local: |
572
|
|
|
date_local_dict = {'id': row[0], |
573
|
|
|
'working_calendar_id': row[1], |
574
|
|
|
'date_local': row[2].isoformat()[0:10], |
575
|
|
|
'description': row[3]} |
576
|
|
|
result.append(date_local_dict) |
577
|
|
|
meta_result['non_working_days'] = result |
578
|
|
|
cursor.close() |
579
|
|
|
cnx.close() |
580
|
|
|
resp.text = json.dumps(meta_result) |
581
|
|
|
|
582
|
|
|
|
583
|
|
|
class WorkingCalendarImport: |
584
|
|
|
def __init__(self): |
585
|
|
|
""""Initializes WorkingCalendarImport""" |
586
|
|
|
pass |
587
|
|
|
|
588
|
|
|
@staticmethod |
589
|
|
|
def on_options(req, resp): |
590
|
|
|
_ = req |
591
|
|
|
resp.status = falcon.HTTP_200 |
592
|
|
|
|
593
|
|
|
@staticmethod |
594
|
|
|
def on_post(req, resp): |
595
|
|
|
"""Handles POST requests""" |
596
|
|
|
admin_control(req) |
597
|
|
|
try: |
598
|
|
|
raw_json = req.stream.read().decode('utf-8') |
599
|
|
|
new_values = json.loads(raw_json) |
600
|
|
|
except Exception as ex: |
601
|
|
|
print(str(ex)) |
602
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
603
|
|
|
title='API.BAD_REQUEST', |
604
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
605
|
|
|
|
606
|
|
|
if 'name' not in new_values.keys() or \ |
607
|
|
|
not isinstance(new_values['name'], str) or \ |
608
|
|
|
len(str.strip(new_values['name'])) == 0: |
609
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
610
|
|
|
description='API.INVALID_WORKING_CALENDAR_NAME') |
611
|
|
|
name = str.strip(new_values['name']) |
612
|
|
|
|
613
|
|
|
if 'description' in new_values.keys() and \ |
614
|
|
|
new_values['description'] is not None and \ |
615
|
|
|
len(str(new_values['description'])) > 0: |
616
|
|
|
description = str.strip(new_values['description']) |
617
|
|
|
else: |
618
|
|
|
description = None |
619
|
|
|
|
620
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
621
|
|
|
cursor = cnx.cursor() |
622
|
|
|
|
623
|
|
|
cursor.execute(" SELECT name " |
624
|
|
|
" FROM tbl_working_calendars " |
625
|
|
|
" WHERE name = %s ", (name,)) |
626
|
|
|
if cursor.fetchone() is not None: |
627
|
|
|
cursor.close() |
628
|
|
|
cnx.close() |
629
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
630
|
|
|
description='API.WORKING_CALENDAR_NAME_IS_ALREADY_IN_USE') |
631
|
|
|
|
632
|
|
|
add_values = (" INSERT INTO tbl_working_calendars " |
633
|
|
|
" (name, description) " |
634
|
|
|
" VALUES (%s, %s) ") |
635
|
|
|
cursor.execute(add_values, (name, |
636
|
|
|
description)) |
637
|
|
|
new_id = cursor.lastrowid |
638
|
|
|
working_calendar_id = new_id |
639
|
|
|
for values in new_values['non_working_days']: |
640
|
|
|
if 'date_local' not in values or \ |
641
|
|
|
values['date_local'] is None or \ |
642
|
|
|
len(str(values['date_local'])) <= 0: |
643
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
644
|
|
|
description='API.INVALID_DATE_LOCAL') |
645
|
|
|
date_local = str.strip(values['date_local']) |
646
|
|
|
|
647
|
|
|
if 'description' in values and \ |
648
|
|
|
values['description'] is not None and \ |
649
|
|
|
len(str(values['description'])) > 0: |
650
|
|
|
description = str.strip(values['description']) |
651
|
|
|
else: |
652
|
|
|
description = None |
653
|
|
|
|
654
|
|
|
cursor.execute(" SELECT id " |
655
|
|
|
" FROM tbl_working_calendars_non_working_days " |
656
|
|
|
" WHERE working_calendar_id = %s AND date_local = %s ", |
657
|
|
|
(working_calendar_id, date_local)) |
658
|
|
|
if cursor.fetchone() is not None: |
659
|
|
|
cursor.close() |
660
|
|
|
cnx.close() |
661
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
662
|
|
|
description='API.DATE_IS_ALREADY_IN_WORKING_CALENDAR') |
663
|
|
|
|
664
|
|
|
add_values = (" INSERT INTO tbl_working_calendars_non_working_days " |
665
|
|
|
" (working_calendar_id, date_local, description) " |
666
|
|
|
" VALUES (%s, %s, %s) ") |
667
|
|
|
cursor.execute(add_values, (working_calendar_id, date_local, description)) |
668
|
|
|
cnx.commit() |
669
|
|
|
cursor.close() |
670
|
|
|
cnx.close() |
671
|
|
|
|
672
|
|
|
resp.status = falcon.HTTP_201 |
673
|
|
|
resp.location = '/workingcalendar/' + str(new_id) |
674
|
|
|
|
675
|
|
|
|
676
|
|
|
class WorkingCalendarClone: |
677
|
|
|
def __init__(self): |
678
|
|
|
""""Initializes WorkingCalendarClone""" |
679
|
|
|
pass |
680
|
|
|
|
681
|
|
|
@staticmethod |
682
|
|
|
def on_options(req, resp, id_): |
683
|
|
|
_ = req |
684
|
|
|
resp.status = falcon.HTTP_200 |
685
|
|
|
_ = id_ |
686
|
|
|
|
687
|
|
|
@staticmethod |
688
|
|
|
def on_post(req, resp, id_): |
689
|
|
|
admin_control(req) |
690
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
691
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
692
|
|
|
description='API.INVALID_WORKING_CALENDAR_ID') |
693
|
|
|
|
694
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
695
|
|
|
cursor = cnx.cursor() |
696
|
|
|
|
697
|
|
|
cursor.execute(" SELECT id, name, description" |
698
|
|
|
" FROM tbl_working_calendars " |
699
|
|
|
" WHERE id = %s ", (id_,)) |
700
|
|
|
row = cursor.fetchone() |
701
|
|
|
|
702
|
|
|
if row is None: |
703
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
704
|
|
|
description='API.WORKING_CALENDAR_NOT_FOUND') |
705
|
|
|
|
706
|
|
|
meta_result = {"id": row[0], |
707
|
|
|
"name": row[1], |
708
|
|
|
"description": row[2], |
709
|
|
|
"non_working_days": None} |
710
|
|
|
cursor.execute(" SELECT id, working_calendar_id, date_local, description" |
711
|
|
|
" FROM tbl_working_calendars_non_working_days " |
712
|
|
|
" WHERE working_calendar_id = %s ", (id_,)) |
713
|
|
|
rows_date_local = cursor.fetchall() |
714
|
|
|
|
715
|
|
|
result = list() |
716
|
|
|
if rows_date_local is not None and len(rows_date_local) > 0: |
717
|
|
|
for row in rows_date_local: |
718
|
|
|
date_local_dict = {'id': row[0], |
719
|
|
|
'working_calendar_id': row[1], |
720
|
|
|
'date_local': row[2].isoformat()[0:10], |
721
|
|
|
'description': row[3]} |
722
|
|
|
result.append(date_local_dict) |
723
|
|
|
meta_result['non_working_days'] = result |
724
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
725
|
|
|
if config.utc_offset[0] == '-': |
726
|
|
|
timezone_offset = -timezone_offset |
727
|
|
|
new_name = (str.strip(meta_result['name']) + |
728
|
|
|
(datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
729
|
|
|
add_values = (" INSERT INTO tbl_working_calendars " |
730
|
|
|
" (name, description) " |
731
|
|
|
" VALUES (%s, %s) ") |
732
|
|
|
cursor.execute(add_values, (new_name, |
733
|
|
|
meta_result['description'])) |
734
|
|
|
new_id = cursor.lastrowid |
735
|
|
|
for values in meta_result['non_working_days']: |
736
|
|
|
add_values = (" INSERT INTO tbl_working_calendars_non_working_days " |
737
|
|
|
" (working_calendar_id, date_local, description) " |
738
|
|
|
" VALUES (%s, %s, %s) ") |
739
|
|
|
cursor.execute(add_values, (new_id, values['date_local'], values['description'])) |
740
|
|
|
cnx.commit() |
741
|
|
|
cursor.close() |
742
|
|
|
cnx.close() |
743
|
|
|
|
744
|
|
|
resp.status = falcon.HTTP_201 |
745
|
|
|
resp.location = '/workingcalendar/' + str(new_id) |
746
|
|
|
|
747
|
|
|
|