myems/myems-api/core/timezone.py

117 lines
3.6 KiB
Python

import falcon
import simplejson as json
import mysql.connector
import config
from core.useractivity import user_logger, access_control
class TimezoneCollection:
@staticmethod
def __init__():
""""Initializes TimezoneCollection"""
pass
@staticmethod
def on_options(req, resp):
resp.status = falcon.HTTP_200
@staticmethod
def on_get(req, resp):
cnx = mysql.connector.connect(**config.myems_system_db)
cursor = cnx.cursor()
query = (" SELECT id, name, description, utc_offset "
" FROM tbl_timezones ")
cursor.execute(query)
rows = cursor.fetchall()
cursor.close()
cnx.disconnect()
result = list()
if rows is not None and len(rows) > 0:
for row in rows:
meta_result = {"id": row[0], "name": row[1], "description": row[2], "utc_offset": row[3]}
result.append(meta_result)
resp.text = json.dumps(result)
class TimezoneItem:
@staticmethod
def __init__():
""""Initializes TimezoneItem"""
pass
@staticmethod
def on_options(req, resp, id_):
resp.status = falcon.HTTP_200
@staticmethod
def on_get(req, resp, id_):
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_TIMEZONE_ID')
cnx = mysql.connector.connect(**config.myems_system_db)
cursor = cnx.cursor()
query = (" SELECT id, name, description, utc_offset "
" FROM tbl_timezones "
" WHERE id = %s ")
cursor.execute(query, (id_,))
row = cursor.fetchone()
if row is None:
cursor.close()
cnx.disconnect()
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
description='API.TIMEZONE_NOT_FOUND')
result = {"id": row[0], "name": row[1], "description": row[2], "utc_offset": row[3]}
cursor.close()
cnx.disconnect()
resp.text = json.dumps(result)
@staticmethod
@user_logger
def on_put(req, resp, id_):
"""Handles PUT requests"""
access_control(req)
try:
raw_json = req.stream.read().decode('utf-8')
except Exception as ex:
raise falcon.HTTPError(falcon.HTTP_400, 'API.ERROR', ex)
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_TIMEZONE_ID')
new_values = json.loads(raw_json)
cnx = mysql.connector.connect(**config.myems_system_db)
cursor = cnx.cursor()
cursor.execute(" SELECT name "
" FROM tbl_timezones "
" WHERE id = %s ", (id_,))
if cursor.fetchone() is None:
cursor.close()
cnx.disconnect()
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
description='API.TIMEZONE_NOT_FOUND')
update_row = (" UPDATE tbl_timezones "
" SET name = %s, description = %s, utc_offset = %s "
" WHERE id = %s ")
cursor.execute(update_row, (new_values['data']['name'],
new_values['data']['description'],
new_values['data']['utc_offset'],
id_,))
cnx.commit()
cursor.close()
cnx.disconnect()
resp.status = falcon.HTTP_200