runbot/runbot_test/models.py
William Braeckman b51909fab6 [IMP] runbot: implement public api mixin
Until now api routes on runbot have been created through custom website
pages in production.
We want to unify the API by making a 'public' api, inspired by the way
`web_search_read` works.

This commit adds:
 - A route to list all publicly available models
 - A route to do a read on a public model
 - A route to fetch the publicly available specification for a model
 - A public model mixin that provides all the tools required to support
   the above mentionned routes.

The mixin adds the ability to add the `public` attribute on fields.
Any field marked as public can then be publicly queried through the
controller.
Relational fields work in a nested manner (`fields` key in the field's
sub-specification) (up to a depth of 10).
The public api does not allow going through a relationship back and
front (parent->child->parent is NOT allowed).

Because we are based on `web_search_read`, we heavily focus on
validating the specification, for security reasons, and offset the load
of reading to the `web_read` function (we currently don't provide limit
metadata).
2025-03-17 13:44:35 +01:00

71 lines
2.4 KiB
Python

from odoo import models, api, fields
from odoo.addons.runbot.fields import JsonDictField
class TestModelParent(models.Model):
_name = 'runbot.test.model.parent'
_inherit = ['runbot.public.model.mixin']
_description = 'parent'
private_field = fields.Boolean(public=False)
field_bool = fields.Boolean(public=True)
field_integer = fields.Integer(public=True)
field_float = fields.Float(public=True)
field_char = fields.Char(public=True)
field_text = fields.Text(public=True)
field_html = fields.Html(public=True)
field_date = fields.Date(public=True)
field_datetime = fields.Datetime(public=True)
field_selection = fields.Selection(selection=[('a', 'foo'), ('b', 'bar')], public=True)
field_json = JsonDictField(public=True)
field_many2one = fields.Many2one('runbot.test.model.parent', public=True)
field_one2many = fields.One2many('runbot.test.model.child', 'parent_id', public=True)
field_one2many_private = fields.One2many('runbot.test.model.child.private', 'parent_id', public=True)
field_many2many = fields.Many2many(
'runbot.test.model.parent', relation='test_model_relation', public=True,
column1='col_1', column2='col_2',
)
field_one2many_computed = fields.One2many('runbot.test.model.child', compute='_compute_one2many', public=True)
field_many2many_computed = fields.Many2many('runbot.test.model.parent', compute='_compute_many2many', public=True)
@api.model
def _api_request_allow_direct_access(self):
return False
@api.depends()
def _compute_one2many(self):
self.field_one2many_computed = self.env['runbot.test.model.child'].search([])
@api.depends()
def _compute_many2many(self):
for rec in self:
rec.field_many2many_computed = self.env['runbot.test.model.parent'].search([
('id', '!=', rec.id),
])
class TestModelChild(models.Model):
_name = 'runbot.test.model.child'
_inherit = ['runbot.public.model.mixin']
_description = 'child'
parent_id = fields.Many2one('runbot.test.model.parent', required=True, public=True)
data = fields.Integer(public=True)
@api.model
def _api_request_allow_direct_access(self):
return False
class TestPrivateModelChild(models.Model):
_name = 'runbot.test.model.child.private'
_description = 'Private Child'
parent_id = fields.Many2one('runbot.test.model.parent', required=True)
data = fields.Integer()