Skip to content

Commit 0332789

Browse files
committed
[IMP] api_log: Add collection of logs
1 parent 2548ca9 commit 0332789

8 files changed

Lines changed: 105 additions & 3 deletions

File tree

api_log/README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ Contributors
5757
------------
5858

5959
- Florian Mounier florian.mounier@akretion.com
60+
- `PyTech <https://www.pytech.it>`__:
61+
62+
- Simone Rubino <simone.rubino@pytech.it>
6063

6164
Maintainers
6265
-----------

api_log/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
from . import api_log_collection
12
from . import api_log

api_log/models/api_log.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright 2025 Akretion (http://www.akretion.com).
22
# @author Florian Mounier <florian.mounier@akretion.com>
3+
# Copyright 2025 Simone Rubino - PyTech
34
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
5+
46
import base64
57
import json
68
import time
@@ -15,6 +17,21 @@ class APILog(models.Model):
1517
_name = "api.log"
1618
_description = "Log for API"
1719

20+
collection_ref = fields.Reference(
21+
selection="_selection_collection_ref",
22+
index=True,
23+
)
24+
collection_model = fields.Char(
25+
compute="_compute_collection",
26+
store=True,
27+
index=True,
28+
)
29+
collection_id = fields.Integer(
30+
compute="_compute_collection",
31+
store=True,
32+
index=True,
33+
)
34+
1835
# Request
1936
request_url = fields.Char()
2037
request_method = fields.Char()
@@ -59,6 +76,25 @@ class APILog(models.Model):
5976
compute="_compute_response_headers_derived", store=True
6077
)
6178

79+
@api.model
80+
def _selection_collection_ref(self):
81+
return []
82+
83+
@api.depends(
84+
"collection_ref",
85+
)
86+
def _compute_collection(self):
87+
for log in self:
88+
collection = log.collection_ref
89+
if collection:
90+
collection_model = collection._name
91+
collection_id = collection.id
92+
else:
93+
collection_model = False
94+
collection_id = False
95+
log.collection_model = collection_model
96+
log.collection_id = collection_id
97+
6298
@api.model
6399
def _headers_hidden_keys(self):
64100
"""Header keys that should not be logged.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2025 Simone Rubino - PyTech
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
4+
from odoo import fields, models
5+
6+
7+
class APILogCollection(models.AbstractModel):
8+
_name = "api.log_collection.mixin"
9+
_description = "Collection of API logs"
10+
11+
log_requests = fields.Boolean(
12+
help="Log requests in database.",
13+
)
14+
15+
log_ids = fields.One2many(
16+
comodel_name="api.log",
17+
compute="_compute_log_ids",
18+
string="Logs",
19+
)
20+
21+
def _compute_log_ids(self):
22+
for collection in self:
23+
collection.log_ids = self.env["api.log"].search(
24+
[("collection_ref", "=", "%s,%s" % (collection._name, collection.id))]
25+
)
26+
27+
def action_logs(self):
28+
collections_refs = [
29+
"%s,%s" % (collection._name, collection.id) for collection in self
30+
]
31+
return {
32+
"type": "ir.actions.act_window",
33+
"res_model": "api.log",
34+
"name": "Logs",
35+
"view_type": "form",
36+
"view_mode": "tree,form",
37+
"target": "current",
38+
"domain": [
39+
(
40+
"collection_ref",
41+
"in",
42+
collections_refs,
43+
),
44+
],
45+
"context": dict(self.env.context),
46+
}

api_log/static/description/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ <h2><a class="toc-backref" href="#toc-entry-3">Authors</a></h2>
403403
<h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
404404
<ul class="simple">
405405
<li>Florian Mounier <a class="reference external" href="mailto:florian.mounier&#64;akretion.com">florian.mounier&#64;akretion.com</a></li>
406+
<li><a class="reference external" href="https://www.pytech.it">PyTech</a>:<ul>
407+
<li>Simone Rubino &lt;<a class="reference external" href="mailto:simone.rubino&#64;pytech.it">simone.rubino&#64;pytech.it</a>&gt;</li>
408+
</ul>
409+
</li>
406410
</ul>
407411
</div>
408412
<div class="section" id="maintainers">

api_log/tests/common.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
# Copyright 2025 Simone Rubino - PyTech
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
14
from odoo.tests.common import HttpCase
25

36

4-
class CommonAPILog(HttpCase):
7+
class Common(HttpCase):
58
@classmethod
69
def setUpClass(cls):
710
super().setUpClass()

api_log/tests/test_api_log.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# Copyright 2025 Akretion (http://www.akretion.com).
22
# @author Florian Mounier <florian.mounier@akretion.com>
3+
# Copyright 2025 Simone Rubino - PyTech
34
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
45

56
import requests
67

78
from odoo.http import Request, Response
89

9-
from odoo.addons.api_log.tests.common import CommonAPILog
10+
from odoo.addons.api_log.tests.common import Common
1011

1112

12-
class TestAPILog(CommonAPILog):
13+
class TestAPILog(Common):
1314
def test_log_request(self):
1415
base_url = self.base_url()
1516
secret_api_key = "my-secret-api-key"

api_log/views/api_log_views.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<field name="response_date" />
4646
</group>
4747
<group name="misc">
48+
<field name="collection_ref" />
4849
<field
4950
name="stack_trace"
5051
attrs="{'invisible': [('stack_trace', '=', False)]}"
@@ -61,6 +62,7 @@
6162
<field name="model">api.log</field>
6263
<field name="arch" type="xml">
6364
<tree>
65+
<field name="collection_ref" optional="hide" />
6466
<field name="request_date" />
6567
<field name="request_method" />
6668
<field name="request_url" />
@@ -78,6 +80,7 @@
7880
<field name="arch" type="xml">
7981
<search>
8082
<field name="name" />
83+
<field name="collection_ref" />
8184
<separator />
8285
<filter
8386
string="Success"
@@ -91,6 +94,11 @@
9194
/>
9295

9396
<group name="groupby">
97+
<filter
98+
name="collection_groupby"
99+
string="Collection"
100+
context="{'group_by': 'collection_ref'}"
101+
/>
94102
<filter
95103
name="method_groupby"
96104
string="Method"

0 commit comments

Comments
 (0)