From 77d8ca3288fabd34cf082b726645c5f8ad8a5667 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 22 Apr 2020 11:37:20 +0800 Subject: [PATCH] add dbadm --- ahserver/dbadmin.py | 61 +++++++++++++++++++++++++++++++++++ ahserver/processorResource.py | 12 ++++++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 ahserver/dbadmin.py diff --git a/ahserver/dbadmin.py b/ahserver/dbadmin.py new file mode 100644 index 0000000..4934f7d --- /dev/null +++ b/ahserver/dbadmin.py @@ -0,0 +1,61 @@ +import os +import re +import traceback + +from aiohttp.web_urldispatcher import StaticResource, _WebHandler, PathLike +from aiohttp.web_urldispatcher import Optional, _ExpectHandler +from aiohttp.web_urldispatcher import Path +from aiohttp.web_response import Response, StreamResponse +from aiohttp.web_exceptions import ( + HTTPException, + HTTPExpectationFailed, + HTTPForbidden, + HTTPMethodNotAllowed, + HTTPNotFound, +) +from aiohttp import web +from aiohttp.web_fileresponse import FileResponse +from aiohttp.web_request import Request +from aiohttp.web_response import Response, StreamResponse +from aiohttp.web_routedef import AbstractRouteDef +from aiohttp.web import json_response + + +from sqlor.crud import CRUD + +from appPublic.dictObject import multiDict2Dict +from appPublic.jsonConfig import getConfig + +from .error import Error,Success +actions = [ + "browse", + "add", + "update", + "filter" +] + +class DBAdmin: + def __init__(self, request,dbname,tablename, action): + self.dbname = dbname + self.tablename = tablename + self.request = request + self.action = action + if action not in actions: + print('action not defined',action) + raise HTTPNotFound + try: + self.crud = CRUD(dbname,tablename) + except Exception as e: + print('e=',e) + traceback.print_exc() + raise HTTPNotFound + + async def render(self) -> Response: + try: + d = await self.crud.I() + return json_response(Success(d)) + except Exception as e: + print(e) + traceback.print_exc() + return json_response(Error(errno='metaerror',msg='get metadata error')) + diff --git a/ahserver/processorResource.py b/ahserver/processorResource.py index 14c9f87..b22a1c6 100644 --- a/ahserver/processorResource.py +++ b/ahserver/processorResource.py @@ -29,6 +29,7 @@ from .serverenv import ServerEnv from .url2file import Url2File from .filestorage import FileStorage from .restful import DBCrud +from .dbadmin import DBAdmin from .filedownload import file_download, path_decode def getHeaderLang(request): @@ -172,6 +173,15 @@ class ProcessorResource(StaticResource,Url2File): self.y_env.gethost = gethost path = request.path config = getConfig() + if config.website.dbadm and path.startswith(config.website.dbadm): + pp = path.split('/')[2:] + if len(pp)<3: + raise HTTPNotFound + dbname = pp[0] + tablename = pp[1] + action = pp[2] + adm = DBAdmin(request,dbname,tablename,action) + return await adm.render() if config.website.dbrest and path.startswith(config.website.dbrest): pp = path.split('/')[2:] if len(pp)<2: @@ -206,7 +216,7 @@ class ProcessorResource(StaticResource,Url2File): if self.isFolder(path): config = getConfig() if not config.website.allowListFolder: - Raise HTTPNotFound + raise HTTPNotFound return await super()._handle(request) def absUrl(self,request,url):