[FIX] pms-api-rest: fix precommit

This commit is contained in:
miguelpadin
2022-05-23 12:10:53 +01:00
committed by Darío Lodeiros
parent fca2521b72
commit d8bd44cc8e
3 changed files with 45 additions and 51 deletions

View File

@@ -1,12 +1,29 @@
from werkzeug.exceptions import InternalServerError, Unauthorized, NotFound, Forbidden, BadRequest, HTTPException from werkzeug.exceptions import (
BadRequest,
Forbidden,
HTTPException,
InternalServerError,
NotFound,
Unauthorized,
)
import odoo import odoo
from odoo.addons.base_rest.http import HttpRestRequest, wrapJsonException from odoo.exceptions import (
from odoo.addons.base_rest.http import _rest_services_routes AccessDenied,
from odoo.exceptions import MissingError, AccessError, AccessDenied, UserError, ValidationError AccessError,
from odoo.http import Root, SessionExpiredException, HttpRequest MissingError,
UserError,
ValidationError,
)
from odoo.http import HttpRequest, Root, SessionExpiredException
from odoo.loglevels import ustr from odoo.loglevels import ustr
from odoo.addons.base_rest.http import (
HttpRestRequest,
_rest_services_routes,
wrapJsonException,
)
class HttpRestRequestPms(HttpRestRequest): class HttpRestRequestPms(HttpRestRequest):
def __init__(self, httprequest): def __init__(self, httprequest):
@@ -14,61 +31,49 @@ class HttpRestRequestPms(HttpRestRequest):
def _handle_exception(self, exception): def _handle_exception(self, exception):
"""Called within an except block to allow converting exceptions """Called within an except block to allow converting exceptions
to abitrary responses. Anything returned (except None) will to abitrary responses. Anything returned (except None) will
be used as response.""" be used as response."""
if isinstance(exception, SessionExpiredException): if isinstance(exception, SessionExpiredException):
# we don't want to return the login form as plain html page # we don't want to return the login form as plain html page
# we want to raise a proper exception # we want to raise a proper exception
print('session expired exception') print("session expired exception")
return wrapJsonException(Unauthorized(ustr(exception))) return wrapJsonException(Unauthorized(ustr(exception)))
try: try:
return super(HttpRequest, self)._handle_exception(exception) return super(HttpRequest, self)._handle_exception(exception)
except MissingError as e: except MissingError as e:
extra_info = getattr(e, "rest_json_info", None) extra_info = getattr(e, "rest_json_info", None)
return wrapJsonException( return wrapJsonException(
NotFound(ustr(e)), NotFound(ustr(e)), include_description=True, extra_info=extra_info
include_description=True,
extra_info=extra_info
) )
except (AccessError, AccessDenied) as e: except (AccessError, AccessDenied) as e:
print('access error / access denied exception') print("access error / access denied exception")
extra_info = getattr(e, "rest_json_info", None) extra_info = getattr(e, "rest_json_info", None)
return wrapJsonException( return wrapJsonException(
Forbidden(ustr(e)), Forbidden(ustr(e)), include_description=True, extra_info=extra_info
include_description=True,
extra_info=extra_info
) )
except (UserError, ValidationError) as e: except (UserError, ValidationError) as e:
extra_info = getattr(e, "rest_json_info", None) extra_info = getattr(e, "rest_json_info", None)
return wrapJsonException( return wrapJsonException(
BadRequest(e.args[0]), BadRequest(e.args[0]), include_description=True, extra_info=extra_info
include_description=True,
extra_info=extra_info
) )
except HTTPException as e: except HTTPException as e:
extra_info = getattr(e, "rest_json_info", None) extra_info = getattr(e, "rest_json_info", None)
return wrapJsonException( return wrapJsonException(e, include_description=True, extra_info=extra_info)
e,
include_description=True,
extra_info=extra_info
)
except Unauthorized as e: except Unauthorized as e:
print('Unauthorized exception') print("Unauthorized exception")
extra_info = getattr(e, "rest_json_info", None) extra_info = getattr(e, "rest_json_info", None)
return wrapJsonException( return (
e, wrapJsonException(e, include_description=True, extra_info=extra_info),
include_description=True, )
extra_info=extra_info),
except Exception as e: # flake8: noqa: E722 except Exception as e: # flake8: noqa: E722
extra_info = getattr(e, "rest_json_info", None) extra_info = getattr(e, "rest_json_info", None)
return wrapJsonException( return wrapJsonException(InternalServerError(e), extra_info=extra_info)
InternalServerError(e),
extra_info=extra_info
)
ori_get_request = Root.get_request ori_get_request = Root.get_request
def get_request(self, httprequest): def get_request(self, httprequest):
db = httprequest.session.db db = httprequest.session.db
if db and odoo.service.db.exp_db_exist(db): if db and odoo.service.db.exp_db_exist(db):

View File

@@ -4,7 +4,7 @@ import werkzeug.exceptions
from jose import jwt from jose import jwt
from odoo import _ from odoo import _
from odoo.exceptions import AccessDenied, UserError from odoo.exceptions import AccessDenied
from odoo.addons.base_rest import restapi from odoo.addons.base_rest import restapi
from odoo.addons.base_rest_datamodel.restapi import Datamodel from odoo.addons.base_rest_datamodel.restapi import Datamodel

View File

@@ -1,8 +1,9 @@
from odoo import _ from odoo import _
from odoo.exceptions import MissingError
from odoo.addons.base_rest import restapi from odoo.addons.base_rest import restapi
from odoo.addons.base_rest_datamodel.restapi import Datamodel from odoo.addons.base_rest_datamodel.restapi import Datamodel
from odoo.addons.component.core import Component from odoo.addons.component.core import Component
from odoo.exceptions import MissingError
class PmsRoomService(Component): class PmsRoomService(Component):
@@ -67,11 +68,7 @@ class PmsRoomService(Component):
auth="jwt_api_pms", auth="jwt_api_pms",
) )
def get_room(self, room_id): def get_room(self, room_id):
room = self.env['pms.room'].search( room = self.env["pms.room"].search([("id", "=", room_id)])
[
('id', '=', room_id)
]
)
if room: if room:
PmsRoomInfo = self.env.datamodels["pms.room.info"] PmsRoomInfo = self.env.datamodels["pms.room.info"]
return PmsRoomInfo( return PmsRoomInfo(
@@ -97,11 +94,7 @@ class PmsRoomService(Component):
auth="jwt_api_pms", auth="jwt_api_pms",
) )
def update_room(self, room_id, pms_room_info_data): def update_room(self, room_id, pms_room_info_data):
room = self.env['pms.room'].search( room = self.env["pms.room"].search([("id", "=", room_id)])
[
('id', '=', room_id)
]
)
if room: if room:
room.name = pms_room_info_data.name room.name = pms_room_info_data.name
else: else:
@@ -120,11 +113,7 @@ class PmsRoomService(Component):
) )
def delete_room(self, room_id): def delete_room(self, room_id):
# esto tb podría ser con un browse # esto tb podría ser con un browse
room = self.env['pms.room'].search( room = self.env["pms.room"].search([("id", "=", room_id)])
[
('id', '=', room_id)
]
)
if room: if room:
room.active = False room.active = False
else: else:
@@ -143,12 +132,12 @@ class PmsRoomService(Component):
auth="jwt_api_pms", auth="jwt_api_pms",
) )
def create_room(self, pms_room_info_param): def create_room(self, pms_room_info_param):
room = self.env['pms.room'].create( room = self.env["pms.room"].create(
{ {
"name": pms_room_info_param.name, "name": pms_room_info_param.name,
"room_type_id": pms_room_info_param.roomTypeId, "room_type_id": pms_room_info_param.roomTypeId,
"capacity": pms_room_info_param.capacity, "capacity": pms_room_info_param.capacity,
"short_name": pms_room_info_param.shortName "short_name": pms_room_info_param.shortName,
} }
) )
return room.id return room.id