mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[ADD]pms_ocr_klippa: Add log requests
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
"data": [
|
||||
"data/pms_ocr_klippa_data.xml",
|
||||
"views/res_partner_id_category_views.xml",
|
||||
"views/klippa_log_views.xml",
|
||||
"security/ir.model.access.csv",
|
||||
],
|
||||
"installable": True,
|
||||
}
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
from . import pms_property
|
||||
from . import res_partner_id_category
|
||||
from . import klippa_log
|
||||
|
||||
99
pms_ocr_klippa/models/klippa_log.py
Normal file
99
pms_ocr_klippa/models/klippa_log.py
Normal file
@@ -0,0 +1,99 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class KlippaLog(models.Model):
|
||||
_name = "klippa.log"
|
||||
|
||||
pms_property_id = fields.Many2one(
|
||||
string="PMS Property",
|
||||
help="PMS Property",
|
||||
comodel_name="pms.property",
|
||||
required=True,
|
||||
)
|
||||
request_id = fields.Text(
|
||||
string="Klippa Request ID",
|
||||
help="Request Klippa ID",
|
||||
)
|
||||
image_base64_front = fields.Text(
|
||||
string="Front Image",
|
||||
help="Front Image",
|
||||
)
|
||||
image_base64_back = fields.Text(
|
||||
string="Back Image",
|
||||
help="Back Image",
|
||||
)
|
||||
klippa_response = fields.Text(
|
||||
string="Klippa Response",
|
||||
help="Response",
|
||||
)
|
||||
klippa_status = fields.Char(
|
||||
string="Status",
|
||||
help="Status",
|
||||
)
|
||||
request_datetime = fields.Datetime(
|
||||
string="Request Date",
|
||||
help="Request Date",
|
||||
)
|
||||
response_datetime = fields.Datetime(
|
||||
string="Response Date",
|
||||
help="Response Date",
|
||||
)
|
||||
request_duration = fields.Float(
|
||||
string="Request Duration",
|
||||
help="Request Duration",
|
||||
)
|
||||
mapped_duration = fields.Float(
|
||||
string="Mapped Duration",
|
||||
help="Mapped Duration",
|
||||
)
|
||||
total_duration = fields.Float(
|
||||
string="Total Duration",
|
||||
help="Total Duration",
|
||||
)
|
||||
endpoint = fields.Char(
|
||||
string="Endpoint",
|
||||
help="Endpoint",
|
||||
)
|
||||
request_size = fields.Integer(
|
||||
string="Request Size",
|
||||
help="Request Size",
|
||||
)
|
||||
response_size = fields.Integer(
|
||||
string="Response Size",
|
||||
help="Response Size",
|
||||
)
|
||||
request_headers = fields.Text(
|
||||
string="Request Headers",
|
||||
help="Request Headers",
|
||||
)
|
||||
request_url = fields.Char(
|
||||
string="Request URL",
|
||||
help="Request URL",
|
||||
)
|
||||
service_response = fields.Text(
|
||||
string="Resvice Response",
|
||||
help="Resvice Response",
|
||||
)
|
||||
final_status = fields.Char(
|
||||
string="Final Status",
|
||||
help="Final Status",
|
||||
)
|
||||
error = fields.Text(
|
||||
string="Error",
|
||||
help="Error",
|
||||
)
|
||||
|
||||
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.
|
||||
|
||||
"""
|
||||
self.sudo().search(
|
||||
[
|
||||
("final_status", "=", "success"),
|
||||
("create_date", "<", fields.Datetime.now() - timedelta(days=offset)),
|
||||
]
|
||||
).unlink()
|
||||
@@ -20,38 +20,88 @@ class PmsProperty(models.Model):
|
||||
|
||||
# flake8: noqa: C901
|
||||
def _klippa_document_process(self, image_base_64_front, image_base_64_back=False):
|
||||
ocr_klippa_url = (
|
||||
self.env["ir.config_parameter"].sudo().get_param("ocr_klippa_url")
|
||||
)
|
||||
ocr_klippa_api_key = (
|
||||
self.env["ir.config_parameter"].sudo().get_param("ocr_klippa_api_key")
|
||||
)
|
||||
document = []
|
||||
if image_base_64_front:
|
||||
document.append(image_base_64_front)
|
||||
if image_base_64_back:
|
||||
document.append(image_base_64_back)
|
||||
if not document:
|
||||
raise ValidationError(_("No document image found"))
|
||||
try:
|
||||
ocr_klippa_url = (
|
||||
self.env["ir.config_parameter"].sudo().get_param("ocr_klippa_url")
|
||||
)
|
||||
ocr_klippa_api_key = (
|
||||
self.env["ir.config_parameter"].sudo().get_param("ocr_klippa_api_key")
|
||||
)
|
||||
document = []
|
||||
if image_base_64_front:
|
||||
document.append(image_base_64_front)
|
||||
if image_base_64_back:
|
||||
document.append(image_base_64_back)
|
||||
if not document:
|
||||
raise ValidationError(_("No document image found"))
|
||||
|
||||
headers = {
|
||||
"X-Auth-Key": ocr_klippa_api_key,
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
payload = {
|
||||
"document": document,
|
||||
}
|
||||
headers = {
|
||||
"X-Auth-Key": ocr_klippa_api_key,
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
payload = {
|
||||
"document": document,
|
||||
}
|
||||
log_data = {
|
||||
"pms_property_id": self.id,
|
||||
"image_base64_front": image_base_64_front,
|
||||
"image_base64_back": image_base_64_back,
|
||||
"request_datetime": datetime.now(),
|
||||
"endpoint": ocr_klippa_url,
|
||||
"request_size": len(image_base_64_front) + len(image_base_64_back),
|
||||
"request_headers": str(headers),
|
||||
}
|
||||
|
||||
# Call Klippa OCR API
|
||||
result = requests.post(
|
||||
ocr_klippa_url,
|
||||
headers=headers,
|
||||
json=payload,
|
||||
)
|
||||
json_data = result.json()
|
||||
if json_data.get("result") != "success":
|
||||
raise ValidationError(_("Error calling Klippa OCR API"))
|
||||
document_data = json_data["data"]["parsed"]
|
||||
# Call Klippa OCR API
|
||||
result = requests.post(
|
||||
ocr_klippa_url,
|
||||
headers=headers,
|
||||
json=payload,
|
||||
)
|
||||
json_data = result.json()
|
||||
log_data.extend(
|
||||
{
|
||||
"klippa_response": json_data,
|
||||
"klippa_status": json_data.get("result", "error"),
|
||||
"response_datetime": datetime.now(),
|
||||
"response_size": len(str(json_data)),
|
||||
"request_duration": (
|
||||
datetime.now() - log_data["request_datetime"]
|
||||
).seconds,
|
||||
}
|
||||
)
|
||||
if json_data.get("result") != "success":
|
||||
raise ValidationError(_("Error calling Klippa OCR API"))
|
||||
document_data = json_data["data"]["parsed"]
|
||||
init_mapped_datetime = datetime.now()
|
||||
mapped_data = self._map_klippa_data(document_data)
|
||||
log_data.extend(
|
||||
{
|
||||
"service_response": mapped_data,
|
||||
"mapped_duration": (datetime.now() - init_mapped_datetime).seconds,
|
||||
"total_duration": (
|
||||
datetime.now() - log_data["request_datetime"]
|
||||
).seconds,
|
||||
"final_status": "success",
|
||||
}
|
||||
)
|
||||
self.env["klippa.log"].sudo().create(log_data)
|
||||
return mapped_data
|
||||
except Exception as e:
|
||||
log_data.extend(
|
||||
{
|
||||
"error": str(e),
|
||||
"final_status": "error",
|
||||
"total_duration": (
|
||||
datetime.now() - log_data["request_datetime"]
|
||||
).seconds,
|
||||
}
|
||||
)
|
||||
self.env["klippa.log"].sudo().create(log_data)
|
||||
_logger.error(e)
|
||||
raise ValidationError(_("Error processing Klippa document"))
|
||||
|
||||
def _map_klippa_data(self, document_data):
|
||||
mapped_data = {}
|
||||
found_partner = False
|
||||
if document_data.get("personal_number", False):
|
||||
@@ -225,7 +275,7 @@ class PmsProperty(models.Model):
|
||||
value["province"],
|
||||
self.env["res.country.state"].search(domain).mapped("name"),
|
||||
)
|
||||
if candidates and candidates[1] >= 90:
|
||||
if candidates[1] >= 90:
|
||||
country_state = self.env["res.country.state"].search(
|
||||
domain + [("name", "=", candidates[0])]
|
||||
)
|
||||
|
||||
2
pms_ocr_klippa/security/ir.model.access.csv
Normal file
2
pms_ocr_klippa/security/ir.model.access.csv
Normal file
@@ -0,0 +1,2 @@
|
||||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
|
||||
techinal_klippa_log_access,techinal_klippa_log_access,model_klippa_log,base.group_system,1,1,1,1
|
||||
|
@@ -1,4 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
<head>
|
||||
|
||||
104
pms_ocr_klippa/views/klippa_log_views.xml
Normal file
104
pms_ocr_klippa/views/klippa_log_views.xml
Normal file
@@ -0,0 +1,104 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<odoo>
|
||||
<record id="klippa_log_tree" model="ir.ui.view">
|
||||
<field name="name">klippa.log.tree</field>
|
||||
<field name="model">klippa.log</field>
|
||||
<field name="arch" type="xml">
|
||||
<tree string="Klippa Log">
|
||||
<field name="pms_property_id" />
|
||||
<field name="create_date" />
|
||||
<field name="klippa_status" />
|
||||
<field name="final_status" />
|
||||
<field name="request_id" />
|
||||
</tree>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="klippa_log_form" model="ir.ui.view">
|
||||
<field name="name">klippa.log.form</field>
|
||||
<field name="model">klippa.log</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="Klippa Log">
|
||||
<sheet>
|
||||
<group>
|
||||
<field name="pms_property_id" />
|
||||
<field name="create_date" />
|
||||
<field name="klippa_status" />
|
||||
<field name="final_status" />
|
||||
<field name="request_id" />
|
||||
</group>
|
||||
<notebook>
|
||||
<page string="Klippa Response">
|
||||
<field name="klippa response" />
|
||||
</page>
|
||||
<page string="Service Response">
|
||||
<field name="service_response" />
|
||||
</page>
|
||||
<page string="Front Imgage">
|
||||
<field name="image_base64_front" />
|
||||
</page>
|
||||
<page string="Back Image">
|
||||
<field name="image_base64_back" />
|
||||
</page>
|
||||
<page
|
||||
string="Error"
|
||||
attrs="{'invisible': [('error', '=', False)]}"
|
||||
>
|
||||
<field name="error" />
|
||||
</page>
|
||||
</notebook>
|
||||
</sheet>
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="klippa_log_search" model="ir.ui.view">
|
||||
<field name="name">klippa.log.search</field>
|
||||
<field name="model">klippa.log</field>
|
||||
<field name="arch" type="xml">
|
||||
<search string="Klippa Log">
|
||||
<field name="pms_property_id" />
|
||||
<field name="create_date" />
|
||||
<field name="klippa_status" />
|
||||
<field name="final_status" />
|
||||
</search>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="klippa_log_pivot" model="ir.ui.view">
|
||||
<field name="name">klippa.log.pivot</field>
|
||||
<field name="model">klippa.log</field>
|
||||
<field name="arch" type="xml">
|
||||
<pivot string="Klippa Log">
|
||||
<field name="pms_property_id" type="col" />
|
||||
<field name="create_date" type="col" interval="day" />
|
||||
</pivot>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="klippa_log_graph" model="ir.ui.view">
|
||||
<field name="name">klippa.log.graph</field>
|
||||
<field name="model">klippa.log</field>
|
||||
<field name="arch" type="xml">
|
||||
<graph string="Klippa Log">
|
||||
<field name="pms_property_id" />
|
||||
<field name="create_date" />
|
||||
</graph>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="klippa_log_action" model="ir.actions.act_window">
|
||||
<field name="name">Klippa Log</field>
|
||||
<field name="res_model">klippa.log</field>
|
||||
<field name="view_mode">tree,form,pivot,graph</field>
|
||||
</record>
|
||||
|
||||
<menuitem
|
||||
id="klippa_log_menu"
|
||||
name="Klippa Log"
|
||||
action="klippa_log_action"
|
||||
parent="base.menu_administration"
|
||||
sequence="120"
|
||||
/>
|
||||
|
||||
</odoo>
|
||||
Reference in New Issue
Block a user