diff --git a/website/db.py b/website/db.py index a279c77..a685e4b 100644 --- a/website/db.py +++ b/website/db.py @@ -25,7 +25,7 @@ def to_json_list(cursor: Any) -> Optional[List[Row]]: column_names = list(cursor.keys()) result = cursor.fetchall() if not result: - return None + return [] return [Row(itertools.zip_longest(column_names, row)) for row in result] @@ -35,7 +35,7 @@ def to_json(cursor): result = cursor.fetchone() if not result: - return None + return {} return Row(itertools.zip_longest(column_names, result)) diff --git a/website/handlers/enterprise_device/__init__.py b/website/handlers/enterprise_device/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/website/handlers/enterprise_device/db.py b/website/handlers/enterprise_device/db.py new file mode 100644 index 0000000..7c68785 --- /dev/null +++ b/website/handlers/enterprise_device/db.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- \ No newline at end of file diff --git a/website/handlers/enterprise_device/handler.py b/website/handlers/enterprise_device/handler.py new file mode 100644 index 0000000..aa1b70d --- /dev/null +++ b/website/handlers/enterprise_device/handler.py @@ -0,0 +1,132 @@ +# -*- coding: utf-8 -*- + +from sqlalchemy import text + +from website import errors, db +from website.handler import APIHandler, authenticated +from website.util import shortuuid + + +class DeviceClassificationAddHandler(APIHandler): + """ + - 描述:添加设备分类 + - 请求方式:post + - 请求参数: + >- entity_id, int, 企业id + >- name, string, 设备分类名称 + - 返回值: + ``` + { + "id": 123 + } + ``` + """ + + @authenticated + def post(self): + name = self.get_escaped_argument('name', '') + with self.app_mysql.connect() as conn: + cur = conn.execute( + text("SELECT id FROM device_classification WHERE name=:name"), {"name": name} + ) + ex = cur.fetchone() + if ex: + raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '设备分类已存在') + + conn.execute( + text("INSERT INTO device_classification (name, suid) VALUES (:name, :suid)"), + {"name": name, "suid": shortuuid.ShortUUID().random(10)} + ) + + self.finish() + + +class DeviceClassificationHandler(APIHandler): + """ + - 描述:设备分类列表 + - 请求方式:post + - 请求参数: + >- 无 + - 返回值: + ``` + { + "data": [ + { + "id": 123, + "name": "xxx" + }, + ... + ] + } + ``` + """ + + @authenticated + def post(self): + with self.app_mysql.connect() as conn: + cur = conn.execute( + text("SELECT id, name FROM device_classification where del=0 ORDER BY id DESC") + ) + res = db.to_json_list(cur) + res = res and res or [] + + self.finish({"data": res}) + + +class DeviceClassificationDeleteHandler(APIHandler): + """ + - 描述:删除设备分类 + - 请求方式:post + - 请求参数: + >- id + - 返回值:无 + """ + + @authenticated + def post(self): + did = self.get_int_argument('id') + with self.app_mysql.connect() as conn: + conn.execute( + text("update device_classification set del=1 WHERE id=:id"), {"id": did} + ) + self.finish() + + +class DeviceAddHandler(APIHandler): + """ + + """ + + @authenticated + def post(self): + self.finish() + + +class DeviceEditHandler(APIHandler): + """ + + """ + + @authenticated + def post(self): + self.finish() + + +class DeviceDeleteHandler(APIHandler): + """ + + """ + + @authenticated + def post(self): + self.finish() + + +class DeviceListHandler(APIHandler): + """ + + """ + + @authenticated + def post(self): + self.finish() diff --git a/website/handlers/enterprise_device/url.py b/website/handlers/enterprise_device/url.py new file mode 100644 index 0000000..fa9f702 --- /dev/null +++ b/website/handlers/enterprise_device/url.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- + +from website.handlers.enterprise_device import handler + + +handlers = [ + ("/enterprise/device/classification/add", handler.DeviceClassificationAddHandler), + ("/enterprise/device/classification", handler.DeviceClassificationHandler), + ("/enterprise/device/classification/delete", handler.DeviceClassificationDeleteHandler), + + ("/enterprise/entity/nodes/device/add", handler.DeviceAddHandler), + ("/enterprise/entity/nodes/device/edit", handler.DeviceEditHandler), + ("/enterprise/entity/nodes/device/delete", handler.DeviceDeleteHandler), + ("/enterprise/entity/nodes/device/list", handler.DeviceListHandler), + +] +page_handlers = [ + +] \ No newline at end of file diff --git a/website/handlers/enterprise_entity/handler.py b/website/handlers/enterprise_entity/handler.py index 4d8f2a0..ef69aa2 100644 --- a/website/handlers/enterprise_entity/handler.py +++ b/website/handlers/enterprise_entity/handler.py @@ -2,13 +2,14 @@ import logging from sqlalchemy import text -from website import errors -from website import settings + from website import consts from website import db -from website.util import shortuuid, aes -from website.service import enterprise +from website import errors +from website import settings from website.handler import APIHandler, authenticated +from website.service import enterprise +from website.util import shortuuid, aes class EntityIndexHandler(APIHandler): @@ -77,7 +78,6 @@ class EntityIndexBasecountHandler(APIHandler): model = enterprise.get_enterprise_model_count() device = enterprise.get_enterprise_device_count() - self.finish({"entity": entity, "model": model, "device": device}) @@ -172,7 +172,7 @@ class EntityEditHandler(APIHandler): # "insert into enterprise(name, province, city, addr, industry, contact, phone, summary, logo, account, pwd) " # "values(:name, :province, :city, :addr, :industry, :contact, :phone, :summary, :logo, :account, :pwd)" - "update enterprise set name=:name, province=:province, city=:city, addr=:addr, industry=:industry, contact" + "update enterprise set name=:name, province=:province, city=:city, addr=:addr, industry=:industry, contact" "=:contact, phone=:phone, summary=:summary, logo=:logo, account=:account where id=:id", ), { @@ -190,7 +190,6 @@ class EntityEditHandler(APIHandler): ) conn.commit() - self.finish() @@ -248,9 +247,8 @@ class EntityDeleteHandler(APIHandler): ) conn.commit() - - self.finish() + self.finish() class EntityPwdcheckHandler(APIHandler): diff --git a/website/handlers/enterprise_node/handler.py b/website/handlers/enterprise_node/handler.py index 856971b..e76fbd7 100644 --- a/website/handlers/enterprise_node/handler.py +++ b/website/handlers/enterprise_node/handler.py @@ -58,7 +58,7 @@ class AddHandler(APIHandler): self.finish() -class UpdateHandler(APIHandler): +class EditHandler(APIHandler): """ - 描述:更新企业节点 - 请求方式:post diff --git a/website/handlers/enterprise_node/url.py b/website/handlers/enterprise_node/url.py index e7367bb..78b7015 100644 --- a/website/handlers/enterprise_node/url.py +++ b/website/handlers/enterprise_node/url.py @@ -3,7 +3,7 @@ from website.handlers.enterprise_node import handler handlers = [ ('/enterprise/entity/nodes/add', handler.AddHandler), - ('/enterprise/entity/nodes/update', handler.UpdateHandler), + ('/enterprise/entity/nodes/edit', handler.EditHandler), ('/enterprise/entity/nodes', handler.TreeHandler), ('/enterprise/entity/nodes/info', handler.InfoHandler), ('/enterprise/entity/nodes/delete', handler.DeleteHandler),