Skip to content

Commit

Permalink
Merge pull request #258 from phenobarbital/dev
Browse files Browse the repository at this point in the history
fix model_response for PUT, POST and DELETE methods on ModelView
  • Loading branch information
phenobarbital authored Jun 2, 2024
2 parents f2d20b3 + 967d7eb commit 4b63438
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion navigator/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__description__ = (
"Navigator Web Framework based on aiohttp, " "with batteries included."
)
__version__ = "2.8.48"
__version__ = "2.8.49"
__author__ = "Jesus Lara"
__author_email__ = "jesuslarag@gmail.com"
__license__ = "BSD"
33 changes: 20 additions & 13 deletions navigator/views/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ async def patch(self):
**error
)

async def _post_response(self, result, status: int = 200) -> web.Response:
async def _post_response(self, result, status: int = 200, fields: list = None) -> web.Response:
"""_post_data.
Post-processing data after saved and before summit.
Expand Down Expand Up @@ -728,7 +728,7 @@ async def put(self):
self.model.Meta.connection = conn
try:
result = await self.model.create(data)
return await self._model_response(
return await self._post_response(
result,
status=201,
fields=fields
Expand Down Expand Up @@ -778,7 +778,7 @@ async def put(self):
)
result = await obj.insert()
status = 201
return await self._post_response(result, status=status)
return await self._post_response(result, status=status, fields=fields)
except StatementError as ex:
err = str(ex)
if 'duplicate' in err:
Expand Down Expand Up @@ -874,7 +874,7 @@ async def post(self):
"error": str(ex),
}
return self.error(response=error, status=406)
return await self._model_response(
return await self._post_response(
result,
status=202,
fields=fields
Expand All @@ -900,7 +900,7 @@ async def post(self):
if isinstance(objid, list):
try:
result = await self.model.updating(_filter=objid, **data)
return await self._model_response(
return await self._post_response(
result,
status=202,
fields=fields
Expand All @@ -924,7 +924,7 @@ async def post(self):
result = await self.model.updating(
_filter=args, **result.to_dict()
)
return await self._model_response(
return await self._post_response(
result,
status=202,
fields=fields
Expand Down Expand Up @@ -1003,6 +1003,13 @@ async def set_column_value(value):
data = None
return data

async def _delete_response(self, result, status: int = 200) -> web.Response:
"""_delete_response.
Post-processing data after deleted and before summit.
"""
return self.json_response(result, status=status)

@service_auth
async def delete(self):
""" "
Expand Down Expand Up @@ -1040,7 +1047,7 @@ async def delete(self):
# Delete them this Client
result = await self.model.get(**args)
data = await result.delete(_filter=args)
return await self._model_response(data, status=202)
return await self._delete_response(data, status=202)
except DriverError as exc:
error = {
"message": f"Error on {self.__name__}",
Expand Down Expand Up @@ -1081,7 +1088,7 @@ async def delete(self):
results.append(result)
except Exception:
continue
return await self._model_response(results, status=202)
return await self._delete_response(results, status=202)
elif data is not None:
# delete with data.
try:
Expand All @@ -1103,15 +1110,15 @@ async def delete(self):
result = await self.model.get(**args)
data = await result.delete(_filter=objid)
except DriverError as exc:
return await self._model_response(
return await self._delete_response(
{
"error": f"Error on {self.__name__}",
"message": str(exc)
},
status=400
)
except NoDataFound:
return await self._model_response(
return await self._delete_response(
{
"error": f"{self.__name__} Not Found"
},
Expand All @@ -1123,7 +1130,7 @@ async def delete(self):
objid = data
data = await self.model.remove(**data)
except NoDataFound:
return await self._model_response(
return await self._delete_response(
{
"error": f"{self.__name__} Not Found"
},
Expand All @@ -1133,7 +1140,7 @@ async def delete(self):
"deleted": data,
"data": objid
}
return await self._model_response(result, status=202)
return await self._delete_response(result, status=202)
except (TypeError, KeyError, ValueError) as exc:
error = {
"message": f"We don't found POST data for deletion on {self.__name__}, exc",
Expand All @@ -1149,7 +1156,7 @@ async def delete(self):
result = {
"deleted": result
}
return await self._model_response(result, status=202)
return await self._delete_response(result, status=202)
else:
self.error(
reason=f"Cannot Delete {self.__name__} with Empy Query",
Expand Down

0 comments on commit 4b63438

Please sign in to comment.