ahserver_g/ahserver/crudProcessor.py

46 lines
1.1 KiB
Python
Raw Normal View History

2021-12-30 10:37:06 +08:00
import os
import codecs
from aiohttp.web_exceptions import (
HTTPException
)
2021-12-13 15:21:40 +08:00
import ujson as json
from .baseProcessor import BaseProcessor
from crud_engine.crud_engine import CRUDEngine
class CrudProcessor(BaseProcessor):
@classmethod
def isMe(self, name):
return name == 'crud'
def get_default_filter_data(self):
subffix = '.filterdata'
user = self.run_evn.get('user')
if user:
subffix = f'{subffix}.{user}'
f = f'{self.real_path}{subffix}'
if os.path.exists(f):
with codecs.open(f, 'r', 'utf-8') as ff:
d = json.load(ff)
return d
return None
async def path_call(self, request, params={}):
await self.set_run_env(request)
dic = {}
2021-12-30 10:37:06 +08:00
with codecs.open(self.real_path, 'r', 'utf-8') as f:
2021-12-13 15:21:40 +08:00
dic = json.load(f)
x = request.path.split('/')
2021-12-30 10:37:06 +08:00
if len(x) >= 2:
2021-12-13 15:21:40 +08:00
act = x[-2]
2021-12-30 10:37:06 +08:00
if not CRUDEngine.is_legal_cmd(act):
act = CRUDEngine.defaultcmd()
2021-12-13 15:21:40 +08:00
default_filter_data = self.get_default_filter_data()
2021-12-30 10:37:06 +08:00
ce = CRUDEngine(self.resource, dic, default_filter_data)
2021-12-13 15:21:40 +08:00
return await ce.dispatch(act)
2021-12-30 10:37:06 +08:00
raise HttpException(555)
2021-12-13 15:21:40 +08:00
async def datahandle(self, request):
self.content = await self.path_call(request)