add the menu api

pull/57/head
hyh123a 2021-07-30 15:08:20 +08:00
parent 322a1e94cc
commit 8611c8d799
2 changed files with 250 additions and 1 deletions

View File

@ -5,7 +5,7 @@ from core import energyflowdiagram, privilege, textmessage, distributioncircuit,
costcenter, point, knowledgefile, meter, gsmmodem, tariff, user, storetype, timezone, \ costcenter, point, knowledgefile, meter, gsmmodem, tariff, user, storetype, timezone, \
costfile, offlinemeterfile, version, contact, emailserver, combinedequipment, datasource, equipment, tenant, shopfloor, \ costfile, offlinemeterfile, version, contact, emailserver, combinedequipment, datasource, equipment, tenant, shopfloor, \
webmessage, distributionsystem, store, emailmessage, tenanttype, wechatmessage, space, gateway, offlinemeter, \ webmessage, distributionsystem, store, emailmessage, tenanttype, wechatmessage, space, gateway, offlinemeter, \
rule, energycategory, sensor, energyitem, notification rule, energycategory, sensor, energyitem, notification, menu
from reports import advancedreport from reports import advancedreport
from reports import distributionsystem as distributionsystemreport from reports import distributionsystem as distributionsystemreport
from reports import energyflowdiagram as energyflowdiagramreport from reports import energyflowdiagram as energyflowdiagramreport
@ -244,6 +244,15 @@ api.add_route('/knowledgefiles/{id_}',
api.add_route('/knowledgefiles/{id_}/restore', api.add_route('/knowledgefiles/{id_}/restore',
knowledgefile.KnowledgeFileRestore()) knowledgefile.KnowledgeFileRestore())
api.add_route('/menus',
menu.MenuCollection())
api.add_route('/menus/{id_}',
menu.MenuItem())
api.add_route('/menus/{id_}/children',
menu.MenuChildrenCollection())
api.add_route('/menus/web',
menu.MenuWebCollection())
api.add_route('/meters', api.add_route('/meters',
meter.MeterCollection()) meter.MeterCollection())
api.add_route('/meters/{id_}', api.add_route('/meters/{id_}',

240
myems-api/core/menu.py Normal file
View File

@ -0,0 +1,240 @@
import falcon
import simplejson as json
import mysql.connector
import config
import uuid
from datetime import datetime
from anytree import AnyNode
from anytree.exporter import JsonExporter
class MenuCollection:
@staticmethod
def __init__():
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(dictionary=True)
query = (" SELECT id, path, name, parent_menu_id, is_hidden "
" FROM tbl_menus ")
cursor.execute(query)
rows_menus = cursor.fetchall()
result = list()
if rows_menus is not None and len(rows_menus) > 0:
for row in rows_menus:
temp = {"id": row['id'],
"path": row['path'],
"name": row['name'],
"parent_menu_id": row['parent_menu_id'],
"is_hidden": row['is_hidden']}
result.append(temp)
cursor.close()
cnx.disconnect()
resp.body = json.dumps(result)
class MenuItem:
@staticmethod
def __init__():
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_MENU_ID')
cnx = mysql.connector.connect(**config.myems_system_db)
cursor = cnx.cursor(dictionary=True)
query = (" SELECT id, path, name, parent_menu_id, is_hidden "
" FROM tbl_menus "
" WHERE id=%s ")
cursor.execute(query, (id_,))
rows_menu = cursor.fetchone()
result = None
if rows_menu is not None and len(rows_menu) > 0:
result = {"id": rows_menu['id'],
"path": rows_menu['path'],
"name": rows_menu['name'],
"parent_menu_id": rows_menu['parent_menu_id'],
"is_hidden": rows_menu['is_hidden']}
cursor.close()
cnx.disconnect()
resp.body = json.dumps(result)
@staticmethod
def on_put(req, resp, id_):
"""Handles PUT requests"""
try:
raw_json = req.stream.read().decode('utf-8')
except Exception as ex:
raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_MENU_ID')
new_values = json.loads(raw_json)
if 'is_hidden' not in new_values['data'].keys() or \
not isinstance(new_values['data']['is_hidden'], str) or \
len(str.strip(new_values['data']['is_hidden'])) == 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_IS_HIDDEN')
is_hidden = str.strip(new_values['data']['is_hidden'])
cnx = mysql.connector.connect(**config.myems_system_db)
cursor = cnx.cursor()
update_row = (" UPDATE tbl_menus "
" SET is_hidden = %s "
" WHERE id = %s ")
cursor.execute(update_row, (is_hidden,
id_))
cnx.commit()
cursor.close()
cnx.disconnect()
resp.status = falcon.HTTP_200
class MenuChildrenCollection:
@staticmethod
def __init__():
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_MENU_ID')
cnx = mysql.connector.connect(**config.myems_system_db)
cursor = cnx.cursor(dictionary=True)
query = (" SELECT id, path, name, parent_menu_id, is_hidden "
" FROM tbl_menus "
" WHERE id = %s ")
cursor.execute(query, (id_,))
row_current_menu = cursor.fetchone()
if row_current_menu is None:
cursor.close()
cnx.disconnect()
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
description='API.MENU_NOT_FOUND')
current_menu = None
current_menu_id = None
if row_current_menu is not None and len(row_current_menu) > 0:
for row in row_current_menu:
current_menu = {"id": row['id'],
"path": row['path'],
"name": row['name'],
"parent_menu_id": row['parent_menu_id'],
"is_hidden": row['is_hidden']}
current_menu_id = row['id']
query = (" SELECT id, path, name, parent_menu_id, is_hidden"
" FROM tbl_menus "
" WHERE parent_menu_id = %s")
cursor.execute(query, (current_menu_id,))
rows_menus = cursor.fetchall()
children_menus = []
if rows_menus is not None and len(rows_menus) > 0:
for row in rows_menus:
children_menus.append(
{
"id": row['id'],
"path": row['path'],
"name": row['name'],
"parent_menu": current_menu,
"is_hidden": row['is_hidden']
}
)
result = {
"current": current_menu,
"children": children_menus,
}
cursor.close()
cnx.disconnect()
resp.body = json.dumps(result)
class MenuWebCollection:
@staticmethod
def __init__():
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(dictionary=True)
query = (" SELECT id, path, parent_menu_id, is_hidden "
" FROM tbl_menus "
" WHERE parent_menu_id is NULL "
" AND is_hidden=0 ")
cursor.execute(query)
rows_menus = cursor.fetchall()
first_paths = {}
if rows_menus is not None and len(rows_menus) > 0:
for row in rows_menus:
_id = row['id']
path = row['path']
first_paths[_id] = {
'path': path,
'children': []
}
query = (" SELECT id, path, parent_menu_id, is_hidden "
" FROM tbl_menus "
" WHERE parent_menu_id is not NULL "
" AND is_hidden=0 ")
cursor.execute(query)
rows_menus = cursor.fetchall()
if rows_menus is not None and len(rows_menus) > 0:
for row in rows_menus:
_id = row['id']
parent_menu_id = row['parent_menu_id']
path = row['path']
if parent_menu_id in first_paths.keys():
first_paths[parent_menu_id]['children'].append(path)
result = dict()
for _id, item in first_paths.items():
first_path = item['path']
children = item['children']
result[first_path] = children
cursor.close()
cnx.disconnect()
resp.body = json.dumps(result)