Skip to content

Commit

Permalink
[IMP] pms: add add exp date to jwt data
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpadin committed Aug 5, 2021
1 parent 1043c1c commit 60568bd
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 25 deletions.
1 change: 0 additions & 1 deletion pms_api_rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
from . import datamodels
from . import models
from . import services

1 change: 0 additions & 1 deletion pms_api_rest/controllers/pms_rest.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 4 additions & 3 deletions pms_api_rest/datamodels/pms_reservation_short_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
14 changes: 8 additions & 6 deletions pms_api_rest/lib_jwt/jwt_http.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import datetime

import simplejson as json

from odoo import http
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions pms_api_rest/lib_jwt/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions pms_api_rest/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import res_users
from . import jwt_access_token
from . import pms_reservation
5 changes: 1 addition & 4 deletions pms_api_rest/models/jwt_access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
61 changes: 61 additions & 0 deletions pms_api_rest/models/pms_reservation.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions pms_api_rest/models/res_users.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion pms_api_rest/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -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
access_jwt_access_token,Read jwt access token,model_jwt_provider_access_token,,1,0,0,0
22 changes: 17 additions & 5 deletions pms_api_rest/services/reservation_services.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,7 +17,7 @@ class PmsReservationService(Component):
[
"/",
],
"GET"
"GET",
)
],
input_param=Datamodel("pms.reservation.search.param"),
Expand All @@ -30,22 +32,32 @@ 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(
id=reservation.id,
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
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# generated from manifests external_dependencies
bs4
jwt
marshmallow
pycountry
simplejson
xlrd
1 change: 1 addition & 0 deletions setup/pms_api_rest/odoo/addons/pms_api_rest
6 changes: 6 additions & 0 deletions setup/pms_api_rest/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 60568bd

Please sign in to comment.