From 60568bd17d1a1f50cea53027026a5df4f633ea7f Mon Sep 17 00:00:00 2001 From: miguelpadin Date: Thu, 5 Aug 2021 23:06:32 +0200 Subject: [PATCH] [IMP] pms: add add exp date to jwt data --- pms_api_rest/__init__.py | 1 - pms_api_rest/controllers/pms_rest.py | 1 - .../datamodels/pms_reservation_short_info.py | 7 ++- pms_api_rest/lib_jwt/jwt_http.py | 14 +++-- pms_api_rest/lib_jwt/validator.py | 3 +- pms_api_rest/models/__init__.py | 1 + pms_api_rest/models/jwt_access_token.py | 5 +- pms_api_rest/models/pms_reservation.py | 61 +++++++++++++++++++ pms_api_rest/models/res_users.py | 4 +- pms_api_rest/security/ir.model.access.csv | 2 +- pms_api_rest/services/reservation_services.py | 22 +++++-- requirements.txt | 3 + setup/pms_api_rest/odoo/addons/pms_api_rest | 1 + setup/pms_api_rest/setup.py | 6 ++ 14 files changed, 106 insertions(+), 25 deletions(-) create mode 100644 pms_api_rest/models/pms_reservation.py create mode 120000 setup/pms_api_rest/odoo/addons/pms_api_rest create mode 100644 setup/pms_api_rest/setup.py diff --git a/pms_api_rest/__init__.py b/pms_api_rest/__init__.py index d9df8e2dc39..e14ece83df0 100644 --- a/pms_api_rest/__init__.py +++ b/pms_api_rest/__init__.py @@ -2,4 +2,3 @@ from . import datamodels from . import models from . import services - diff --git a/pms_api_rest/controllers/pms_rest.py b/pms_api_rest/controllers/pms_rest.py index 1669215d05d..0c914527bbc 100644 --- a/pms_api_rest/controllers/pms_rest.py +++ b/pms_api_rest/controllers/pms_rest.py @@ -1,5 +1,4 @@ from odoo.addons.base_rest.controllers import main - from ..lib_jwt.jwt_http import jwt_http from ..lib_jwt.validator import validator diff --git a/pms_api_rest/datamodels/pms_reservation_short_info.py b/pms_api_rest/datamodels/pms_reservation_short_info.py index 3daa347ed89..db0555842f6 100644 --- a/pms_api_rest/datamodels/pms_reservation_short_info.py +++ b/pms_api_rest/datamodels/pms_reservation_short_info.py @@ -10,7 +10,8 @@ class PmsReservationShortInfo(Datamodel): partner = fields.String(required=True, allow_none=False) checkin = fields.String(required=True, allow_none=False) checkout = fields.String(required=True, allow_none=False) - preferred_room_id = fields.String(required=True, allow_none=False) - room_type_id = fields.String(required=True, allow_none=False) + preferredRoomId = fields.String(required=True, allow_none=False) + roomTypeId = fields.String(required=True, allow_none=False) name = fields.String(required=True, allow_none=False) - partner_requests = fields.String(required=False, allow_none=True) + partnerRequests = fields.String(required=False, allow_none=True) + pwaActionButtons = fields.Dict(required=False, allow_none=True) diff --git a/pms_api_rest/lib_jwt/jwt_http.py b/pms_api_rest/lib_jwt/jwt_http.py index 3ed66b34021..adb20956ff1 100644 --- a/pms_api_rest/lib_jwt/jwt_http.py +++ b/pms_api_rest/lib_jwt/jwt_http.py @@ -1,3 +1,5 @@ +import datetime + import simplejson as json from odoo import http @@ -61,10 +63,7 @@ def response(self, success=True, message=None, data=None, code=200): :param data=None data to return :param code=200 http status code """ - print('response') - print('response') - print('response') - print('response') + payload = json.dumps( { "success": success, @@ -108,9 +107,12 @@ def do_login(self, login, password): # login success, generate token user = request.env.user.read(return_fields)[0] - token = validator.create_token(user) + exp = datetime.datetime.utcnow() + datetime.timedelta(minutes=3) + token = validator.create_token(user, exp) - return self.response(data={"user": user, "token": token}) + return self.response( + data={"user": user, "exp": json.dumps(exp.isoformat()), "token": token} + ) def do_logout(self, token): request.session.logout() diff --git a/pms_api_rest/lib_jwt/validator.py b/pms_api_rest/lib_jwt/validator.py index 9a4bbfa7bc6..b838e3d5a1d 100644 --- a/pms_api_rest/lib_jwt/validator.py +++ b/pms_api_rest/lib_jwt/validator.py @@ -26,9 +26,8 @@ def key(self): # (in form company?) return "CHANGE THIS KEY" - def create_token(self, user): + def create_token(self, user, exp): try: - exp = datetime.datetime.utcnow() + datetime.timedelta(days=30) payload = { "exp": exp, "iat": datetime.datetime.utcnow(), diff --git a/pms_api_rest/models/__init__.py b/pms_api_rest/models/__init__.py index eb199289992..6cc1cdcbb31 100644 --- a/pms_api_rest/models/__init__.py +++ b/pms_api_rest/models/__init__.py @@ -1,2 +1,3 @@ from . import res_users from . import jwt_access_token +from . import pms_reservation diff --git a/pms_api_rest/models/jwt_access_token.py b/pms_api_rest/models/jwt_access_token.py index 54f9fdd365c..4350e359e66 100644 --- a/pms_api_rest/models/jwt_access_token.py +++ b/pms_api_rest/models/jwt_access_token.py @@ -7,10 +7,7 @@ class JwtAccessToken(models.Model): _name = "jwt_provider.access_token" _description = "Store user access token for one-time-login" - token = fields.Char( - "Access Token", - required=True - ) + token = fields.Char("Access Token", required=True) user_id = fields.Many2one( comodel_name="res.users", string="User", diff --git a/pms_api_rest/models/pms_reservation.py b/pms_api_rest/models/pms_reservation.py new file mode 100644 index 00000000000..ae37a480c7e --- /dev/null +++ b/pms_api_rest/models/pms_reservation.py @@ -0,0 +1,61 @@ +import json + +from odoo import fields, models + + +class PmsReservation(models.Model): + _inherit = "pms.reservation" + + pwa_action_buttons = fields.Char(compute="_compute_pwa_action_buttons") + + def _compute_pwa_action_buttons(self): + """Return ordered button list, where the first button is + the preditive action, the next are active actions: + - "Assign": Predictive: Reservation by assign + Active- Idem + - "checkin": Predictive- state 'confirm' and checkin day + Active- Idem and assign + - "checkout": Predictive- Pay, onboard and checkout day + Active- Onboard and checkout day + - "Paymen": Predictive- Onboard and pending amount > 0 + Active- pending amount > 0 + - "Invoice": Predictive- qty invoice > 0, onboard, pending amount = 0 + Active- qty invoice > 0 + - "Cancel": Predictive- Never + Active- state in draft, confirm, onboard, full onboard + """ + for reservation in self: + active_buttons = {} + for k in ["Assign", "Checkin", "Checkout", "Payment", "Invoice", "Cancel"]: + if k == "Assign": + if reservation.to_assign: + active_buttons[k] = True + else: + active_buttons[k] = False + elif k == "Checkin": + if reservation.allowed_checkin: + active_buttons[k] = True + else: + active_buttons[k] = False + elif k == "Checkout": + if reservation.allowed_checkout: + active_buttons[k] = True + else: + active_buttons[k] = False + elif k == "Payment": + if reservation.folio_pending_amount > 0: + active_buttons[k] = True + else: + active_buttons[k] = False + elif k == "Invoice": + if reservation.invoice_status == "to invoice": + active_buttons[k] = True + else: + active_buttons[k] = False + elif k == "Cancel": + if reservation.allowed_cancel: + active_buttons[k] = True + else: + active_buttons[k] = False + + reservation.pwa_action_buttons = json.dumps(active_buttons) diff --git a/pms_api_rest/models/res_users.py b/pms_api_rest/models/res_users.py index 43230f6c32f..b7203be4010 100644 --- a/pms_api_rest/models/res_users.py +++ b/pms_api_rest/models/res_users.py @@ -1,7 +1,7 @@ import logging -from odoo import _, api, fields, models -from odoo.exceptions import AccessDenied, ValidationError +from odoo import api, fields, models +from odoo.exceptions import AccessDenied from ..lib_jwt.validator import validator diff --git a/pms_api_rest/security/ir.model.access.csv b/pms_api_rest/security/ir.model.access.csv index b343baac4c8..e97ddc130ed 100644 --- a/pms_api_rest/security/ir.model.access.csv +++ b/pms_api_rest/security/ir.model.access.csv @@ -1,2 +1,2 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink -access_jwt_access_token,Read jwt access token,model_jwt_provider_access_token,,1,0,0,0 \ No newline at end of file +access_jwt_access_token,Read jwt access token,model_jwt_provider_access_token,,1,0,0,0 diff --git a/pms_api_rest/services/reservation_services.py b/pms_api_rest/services/reservation_services.py index 67536903cab..71f20c56710 100644 --- a/pms_api_rest/services/reservation_services.py +++ b/pms_api_rest/services/reservation_services.py @@ -1,3 +1,5 @@ +import json + from odoo.addons.base_rest import restapi from odoo.addons.base_rest_datamodel.restapi import Datamodel from odoo.addons.component.core import Component @@ -15,7 +17,7 @@ class PmsReservationService(Component): [ "/", ], - "GET" + "GET", ) ], input_param=Datamodel("pms.reservation.search.param"), @@ -30,8 +32,12 @@ def search(self, reservation_search_param): domain.append(("id", "=", reservation_search_param.id)) res = [] PmsReservationShortInfo = self.env.datamodels["pms.reservation.short.info"] - for reservation in self.env["pms.reservation"].sudo().search( - domain, + for reservation in ( + self.env["pms.reservation"] + .sudo() + .search( + domain, + ) ): res.append( PmsReservationShortInfo( @@ -39,13 +45,19 @@ def search(self, reservation_search_param): partner=reservation.partner_id.name, checkin=str(reservation.checkin), checkout=str(reservation.checkout), - preferred_room_id=reservation.preferred_room_id.name + preferredRoomId=reservation.preferred_room_id.name if reservation.preferred_room_id else "", - room_type_id=reservation.room_type_id.name + roomTypeId=reservation.room_type_id.name if reservation.room_type_id else "", name=reservation.name, + partnerRequests=reservation.partner_requests + if reservation.partner_requests + else "", + pwaActionButtons=json.loads(reservation.pwa_action_buttons) + if reservation.pwa_action_buttons + else {}, ) ) return res diff --git a/requirements.txt b/requirements.txt index 1fb3c55d7a2..ba49ce19685 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,7 @@ # generated from manifests external_dependencies bs4 +jwt +marshmallow pycountry +simplejson xlrd diff --git a/setup/pms_api_rest/odoo/addons/pms_api_rest b/setup/pms_api_rest/odoo/addons/pms_api_rest new file mode 120000 index 00000000000..1758ce0ed1f --- /dev/null +++ b/setup/pms_api_rest/odoo/addons/pms_api_rest @@ -0,0 +1 @@ +../../../../pms_api_rest \ No newline at end of file diff --git a/setup/pms_api_rest/setup.py b/setup/pms_api_rest/setup.py new file mode 100644 index 00000000000..28c57bb6403 --- /dev/null +++ b/setup/pms_api_rest/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)