mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[IMP]pms_api_rest: Imrpovement API logs, PUT folio service and action_confirm reservation
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
from odoo import _, api, fields, models
|
||||
from datetime import timedelta
|
||||
|
||||
from odoo import _, fields, models
|
||||
|
||||
|
||||
class PmsApiLog(models.Model):
|
||||
@@ -72,10 +74,39 @@ class PmsApiLog(models.Model):
|
||||
string="Response URL",
|
||||
help="Response URL",
|
||||
)
|
||||
model_id = fields.Many2one(
|
||||
string="Model",
|
||||
help="Model",
|
||||
comodel_name="ir.model",
|
||||
request_type = fields.Selection(
|
||||
string="Request Type",
|
||||
help="Request Type",
|
||||
selection=[
|
||||
("folios", "Folios"),
|
||||
("availability", "Availability"),
|
||||
("restrictions", "Restrictions rules"),
|
||||
("prices", "Prices"),
|
||||
],
|
||||
)
|
||||
target_date_from = fields.Date(
|
||||
string="Target Date From",
|
||||
help="Target Date From",
|
||||
)
|
||||
target_date_to = fields.Date(
|
||||
string="Target Date To",
|
||||
help="Target Date To",
|
||||
)
|
||||
folio_ids = fields.Many2many(
|
||||
string="Folios",
|
||||
help="Folios",
|
||||
comodel_name="pms.folio",
|
||||
relation="pms_folio_pms_api_log_rel",
|
||||
column1="pms_api_log_ids",
|
||||
column2="folio_ids",
|
||||
)
|
||||
room_type_ids = fields.Many2many(
|
||||
string="Room Types",
|
||||
help="Room Types",
|
||||
comodel_name="pms.room.type",
|
||||
relation="pms_room_type_pms_api_log_rel",
|
||||
column1="pms_api_log_ids",
|
||||
column2="room_type_ids",
|
||||
)
|
||||
|
||||
def related_action_open_record(self):
|
||||
@@ -90,10 +121,7 @@ class PmsApiLog(models.Model):
|
||||
|
||||
"""
|
||||
self.ensure_one()
|
||||
if "pms_api_log_id" in self.env[self.model_id.model]._fields:
|
||||
records = self.env[self.model_id.model].search(
|
||||
[("pms_api_log_id", "=", self.id)]
|
||||
)
|
||||
records = self.folio_ids
|
||||
if not records:
|
||||
return None
|
||||
action = {
|
||||
@@ -114,21 +142,15 @@ class PmsApiLog(models.Model):
|
||||
)
|
||||
return action
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
def clean_log_data(self, offset=60):
|
||||
"""Clean log data older than the offset.
|
||||
|
||||
:param int offset: The number of days to keep the log data.
|
||||
|
||||
"""
|
||||
set pms_api_log_id and origin_json in related records
|
||||
if record_ids id present in context
|
||||
"""
|
||||
log_record = super().create(vals)
|
||||
if self.env.context.get("record_ids"):
|
||||
records = self.env[self.env.context.get("model")].browse(
|
||||
self.env.context.get("record_ids")
|
||||
)
|
||||
records.write(
|
||||
{
|
||||
"pms_api_log_id": log_record.id,
|
||||
"origin_json": log_record.request,
|
||||
}
|
||||
)
|
||||
return log_record
|
||||
self.sudo().search(
|
||||
[
|
||||
("status", "=", "success"),
|
||||
("create_date", "<", fields.Datetime.now() - timedelta(days=offset)),
|
||||
]
|
||||
).unlink()
|
||||
|
||||
@@ -4,12 +4,11 @@ from odoo import fields, models
|
||||
class PmsFolio(models.Model):
|
||||
_inherit = "pms.folio"
|
||||
|
||||
pms_api_log_id = fields.Many2one(
|
||||
string="PMS API Log",
|
||||
help="PMS API Log",
|
||||
pms_api_log_ids = fields.Many2many(
|
||||
string="API Logs",
|
||||
help="API Logs",
|
||||
comodel_name="pms.api.log",
|
||||
)
|
||||
origin_json = fields.Text(
|
||||
string="Origin JSON",
|
||||
help="Origin JSON",
|
||||
relation="pms_folio_pms_api_log_rel",
|
||||
column1="folio_ids",
|
||||
column2="pms_api_log_ids",
|
||||
)
|
||||
|
||||
@@ -573,6 +573,9 @@ class PmsProperty(models.Model):
|
||||
clients = client
|
||||
else:
|
||||
clients = self.env["res.users"].search([("pms_api_client", "=", True)])
|
||||
room_type_ids = []
|
||||
endpoint = ""
|
||||
response = None
|
||||
_logger.info("PMS API push batch")
|
||||
if isinstance(date_from, str):
|
||||
date_from = datetime.datetime.strptime(date_from, "%Y-%m-%d").date()
|
||||
@@ -593,73 +596,112 @@ class PmsProperty(models.Model):
|
||||
]
|
||||
)
|
||||
for pms_property in pms_properties:
|
||||
property_client_conf = self.env["ota.property.settings"].search(
|
||||
[
|
||||
("pms_property_id", "=", pms_property.id),
|
||||
("agency_id", "=", client.partner_id.id),
|
||||
]
|
||||
)
|
||||
pms_property_id = pms_property.id
|
||||
room_type_ids = (
|
||||
[filter_room_type_id]
|
||||
if filter_room_type_id
|
||||
else self.env["pms.room"]
|
||||
.search([("pms_property_id", "=", pms_property_id)])
|
||||
.mapped("room_type_id")
|
||||
.filtered(
|
||||
lambda r: r.id
|
||||
not in property_client_conf.excluded_room_type_ids.ids
|
||||
try:
|
||||
property_client_conf = (
|
||||
self.env["ota.property.settings"]
|
||||
.sudo()
|
||||
.search(
|
||||
[
|
||||
("pms_property_id", "=", pms_property.id),
|
||||
("agency_id", "=", client.partner_id.id),
|
||||
]
|
||||
)
|
||||
)
|
||||
.ids
|
||||
)
|
||||
payload = {
|
||||
"pmsPropertyId": pms_property_id,
|
||||
}
|
||||
data = []
|
||||
for room_type_id in room_type_ids:
|
||||
if call_type == "availability":
|
||||
endpoint = client.url_endpoint_availability
|
||||
data.extend(
|
||||
pms_property.generate_availability_json(
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
pms_property_id=pms_property_id,
|
||||
room_type_id=room_type_id,
|
||||
client=client,
|
||||
)
|
||||
pms_property_id = pms_property.id
|
||||
room_type_ids = (
|
||||
[filter_room_type_id]
|
||||
if filter_room_type_id
|
||||
else self.env["pms.room"]
|
||||
.search([("pms_property_id", "=", pms_property_id)])
|
||||
.mapped("room_type_id")
|
||||
.filtered(
|
||||
lambda r: r.id
|
||||
not in property_client_conf.excluded_room_type_ids.ids
|
||||
)
|
||||
key_data = "avails"
|
||||
elif call_type == "restrictions":
|
||||
endpoint = client.url_endpoint_rules
|
||||
data.extend(
|
||||
pms_property.generate_restrictions_json(
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
pms_property_id=pms_property_id,
|
||||
room_type_id=room_type_id,
|
||||
client=client,
|
||||
)
|
||||
)
|
||||
key_data = "rules"
|
||||
elif call_type == "prices":
|
||||
endpoint = client.url_endpoint_prices
|
||||
data.extend(
|
||||
pms_property.generate_prices_json(
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
pms_property_id=pms_property_id,
|
||||
room_type_id=room_type_id,
|
||||
client=client,
|
||||
)
|
||||
)
|
||||
key_data = "prices"
|
||||
else:
|
||||
raise ValidationError(_("Invalid call type"))
|
||||
if data:
|
||||
payload[key_data] = data
|
||||
response = self.pms_api_push_payload(payload, endpoint, client)
|
||||
_logger.info(
|
||||
f"""PMS API push batch response to
|
||||
{endpoint}: {response.status_code} - {response.text}"""
|
||||
.ids
|
||||
)
|
||||
payload = {
|
||||
"pmsPropertyId": pms_property_id,
|
||||
}
|
||||
data = []
|
||||
for room_type_id in room_type_ids:
|
||||
if call_type == "availability":
|
||||
endpoint = client.url_endpoint_availability
|
||||
data.extend(
|
||||
pms_property.generate_availability_json(
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
pms_property_id=pms_property_id,
|
||||
room_type_id=room_type_id,
|
||||
client=client,
|
||||
)
|
||||
)
|
||||
key_data = "avails"
|
||||
elif call_type == "restrictions":
|
||||
endpoint = client.url_endpoint_rules
|
||||
data.extend(
|
||||
pms_property.generate_restrictions_json(
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
pms_property_id=pms_property_id,
|
||||
room_type_id=room_type_id,
|
||||
client=client,
|
||||
)
|
||||
)
|
||||
key_data = "rules"
|
||||
elif call_type == "prices":
|
||||
endpoint = client.url_endpoint_prices
|
||||
data.extend(
|
||||
pms_property.generate_prices_json(
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
pms_property_id=pms_property_id,
|
||||
room_type_id=room_type_id,
|
||||
client=client,
|
||||
)
|
||||
)
|
||||
key_data = "prices"
|
||||
else:
|
||||
raise ValidationError(_("Invalid call type"))
|
||||
if data:
|
||||
payload[key_data] = data
|
||||
response = self.pms_api_push_payload(payload, endpoint, client)
|
||||
_logger.info(
|
||||
f"""PMS API push batch response to
|
||||
{endpoint}: {response.status_code} - {response.text}"""
|
||||
)
|
||||
self.invalidate_cache()
|
||||
self.env["pms.api.log"].sudo().create(
|
||||
{
|
||||
"pms_property_id": pms_property_id,
|
||||
"client_id": client.id,
|
||||
"request": payload,
|
||||
"response": str(response),
|
||||
"status": "success" if response.ok else "error",
|
||||
"request_date": fields.Datetime.now(),
|
||||
"method": "PUSH",
|
||||
"endpoint": endpoint,
|
||||
"target_date_from": date_from,
|
||||
"target_date_to": date_to,
|
||||
"request_type": call_type,
|
||||
"room_type_ids": room_type_ids,
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
_logger.error(f"""PMS API push batch error: {e}""")
|
||||
self.env["pms.api.log"].sudo().create(
|
||||
{
|
||||
"pms_property_id": pms_property_id,
|
||||
"client_id": client.id,
|
||||
"request": payload,
|
||||
"response": str(e),
|
||||
"status": "error",
|
||||
"request_date": fields.Datetime.now(),
|
||||
"method": "PUSH",
|
||||
"endpoint": endpoint,
|
||||
"target_date_from": date_from,
|
||||
"target_date_to": date_to,
|
||||
"request_type": call_type,
|
||||
"room_type_ids": room_type_ids,
|
||||
}
|
||||
)
|
||||
self.invalidate_cache()
|
||||
|
||||
Reference in New Issue
Block a user