Files
odoo_source/odoo/addons/test_http/models.py
T
Julien Castiaux c0647b5c52 [REF] core: HTTPocalypse (14) changes all addons
This commit is the 14th commit of a comprehensive refactor of our HTTP
framework. See odoo/odoo#78857 for complete historic, discussions and
rationnals.

* `request.uid = x` => `request.update_env(user=x)`.
* `request.context = x` => `request.update_env(context=x)`.
* `request.context = dict(request.context, x=y)`
   => `request.update_context(x=y)`.
* `request.cr = None` => `request.cr.close()`.
* `http.mono_db()` => `request.db`.
* `http.dispatch_rpc()` => `service.dispatch_rpc()`.
* `@service.model.check` => `service.model.retrying()`.
* `request.endpoint`
   => `env['ir.http']._match(request.httprequest.path)[0].endpoint`.
* `request.routing_iteration `=> `removed`.
* `request.jsonrequest` => `request.dispatcher.jsonrequest`.

Note that `request.params` is now set much later in the process. If you
are in a situation where you values from the query string or the
http body you can use `request.get_http_params()`.

Note that using the new `request.future_response`, it is possible to
add headers and cookies on the response object before the response
object is initialized. Please note that headers/cookies saved on
the future response will NOT be injected in case of error.

PR: odoo#78857
Task: 2571224
2022-02-24 13:30:51 +00:00

58 lines
2.3 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models, fields, api
MILKY_WAY_REGIONS = ['P3X', 'P4X', 'P2X', 'P5C']
PEGASUS_REGIONS = ['M4R', 'P3Y', 'M6R']
class Stargate(models.Model):
_name = 'test_http.stargate'
_description = 'Stargate'
name = fields.Char(required=True, store=True, compute='_compute_name', readonly=False, help="The stargate/planet common name.")
address = fields.Char(required=True, help="The stargate's 6-glyphs address used by other stargates to dial this one.")
sgc_designation = fields.Char(store=True, compute='_compute_sgc_designation', help="The SGC designation name of this stargate.")
galaxy_id = fields.Many2one('test_http.galaxy', required=True, help="The galaxy where this stargate is.")
has_galaxy_crystal = fields.Boolean(store=True, compute='_compute_has_galaxy_crystal', readonly=False, help="Whether this stargate can dial other galaxies.")
_sql_constraints = [
('address_length', 'CHECK(LENGTH(address) = 6)', "Local addresses have 6 glyphs"),
]
@api.depends('galaxy_id')
def _compute_has_galaxy_crystal(self):
milky_way = self.env.ref('test_http.milky_way')
for gate in self:
gate.has_galaxy_crystal = gate.galaxy_id == milky_way
@api.depends('sgc_designation')
def _compute_name(self):
for gate in self:
if not gate.name:
gate.name = gate.sgc_designation
@api.depends('address')
def _compute_sgc_designation(self):
""" Forge a sgc designation that looks like a real one. """
for gate in self:
if gate.galaxy_id.name not in ('Milky Way', 'Pegasus'):
gate.sgc_designation = False
continue
region_part = (
PEGASUS_REGIONS[gate.id % len(PEGASUS_REGIONS)]
if gate.galaxy_id.name == 'Pegasus'
else MILKY_WAY_REGIONS[gate.id % len(MILKY_WAY_REGIONS)]
)
local_part = str(int.from_bytes(gate.address.encode(), 'big'))[:3]
gate.sgc_designation = f'{region_part}-{local_part}'
class Galaxy(models.Model):
_name = 'test_http.galaxy'
_description = 'Galaxy'
name = fields.Char(required=True, help='The galaxy common name.')