This commit is contained in:
yumoqing 2021-12-13 12:52:28 +08:00
parent bd32b8205e
commit 61f0b52f0f

View File

@ -21,7 +21,7 @@ from aiohttp.web_routedef import AbstractRouteDef
from aiohttp.web import json_response from aiohttp.web import json_response
from sqlor.crud import CRUD from sqlor.dbpools import DBPools
from appPublic.dictObject import multiDict2Dict from appPublic.dictObject import multiDict2Dict
from appPublic.jsonConfig import getConfig from appPublic.jsonConfig import getConfig
@ -44,7 +44,7 @@ class RestEndpoint:
self.methods[method_name.upper()] = method self.methods[method_name.upper()] = method
async def dispatch(self): async def dispatch(self):
method = self.methods.get(self.request.method.upper()) method = self.methods.get(self.request.method.lower())
if not method: if not method:
raise HTTPMethodNotAllowed('', DEFAULT_METHODS) raise HTTPMethodNotAllowed('', DEFAULT_METHODS)
@ -57,18 +57,14 @@ class DBCrud(RestEndpoint):
self.dbname = dbname self.dbname = dbname
self.tablename = tablename self.tablename = tablename
self.request = request self.request = request
self.db = DBPools()
self.id = id self.id = id
try:
self.crud = CRUD(dbname,tablename)
except Exception as e:
print('e=',e)
traceback.print_exc()
raise HTTPNotFound
async def options(self) -> Response: async def options(self) -> Response:
try: try:
d = await self.crud.I() with self.db.sqlorContext(self.dbname) as sor:
return json_response(Success(d)) d = await sor.I(self.tablename)
return json_response(Success(d))
except Exception as e: except Exception as e:
print(e) print(e)
traceback.print_exc() traceback.print_exc()
@ -80,10 +76,9 @@ class DBCrud(RestEndpoint):
""" """
try: try:
ns = multiDict2Dict(self.request.query) ns = multiDict2Dict(self.request.query)
if self.id is not None: with self.db.sqlorContext(self.dbname) as sor:
ns['__id'] = self.id d = await sor.R(self.tablename, ns)
d = await self.crud.R(NS=ns) return json_response(Success(d))
return json_response(Success(d))
except Exception as e: except Exception as e:
print(e) print(e)
traceback.print_exc() traceback.print_exc()
@ -95,8 +90,9 @@ class DBCrud(RestEndpoint):
""" """
try: try:
ns = multiDict2Dict(await self.request.post()) ns = multiDict2Dict(await self.request.post())
d = await self.crud.C(ns) with self.db.sqlorContext(self.dbname) as sor:
return json_response(Success(d)) d = await sor.C(self.tablename, ns)
return json_response(Success(d))
except Exception as e: except Exception as e:
print(e) print(e)
traceback.print_exc() traceback.print_exc()
@ -108,8 +104,9 @@ class DBCrud(RestEndpoint):
""" """
try: try:
ns = multiDict2Dict(await self.request.post()) ns = multiDict2Dict(await self.request.post())
d = await self.crud.U(ns) with self.db.sqlorContext(self.dbname) as sor:
return json_response(Success('')) d = await sor.U(self.tablename, ns)
return json_response(Success(' '))
except Exception as e: except Exception as e:
print(e) print(e)
traceback.print_exc() traceback.print_exc()
@ -121,8 +118,9 @@ class DBCrud(RestEndpoint):
""" """
try: try:
ns = multiDict2Dict(self.request.query) ns = multiDict2Dict(self.request.query)
d = await self.crud.D(ns) with self.db.sqlorContext(self.dbname) as sor:
return json_response(Success(d)) d = await sor.D(self.tablename, ns)
return json_response(Success(d))
except Exception as e: except Exception as e:
print(e) print(e)
traceback.print_exc() traceback.print_exc()