Skip to content

Commit

Permalink
[IMP] core: move SUPERUSER_ID to odoo.api
Browse files Browse the repository at this point in the history
In order to become a native namespace, `odoo` package must not contain
any variables. This is a step in that direction. `odoo.SUPERUSER_ID`
still continues to work.
odoo.api.SUPERUSER_ID is often used with Environment, so we declare both
in the same package.

task-4069446
  • Loading branch information
kmagusiak committed Jan 24, 2025
1 parent 67cb74f commit be09a3a
Show file tree
Hide file tree
Showing 34 changed files with 97 additions and 81 deletions.
10 changes: 5 additions & 5 deletions addons/onboarding/tests/test_onboarding_concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import psycopg2.errors

import odoo
from odoo import api
from odoo.modules.registry import Registry
from odoo.tests.common import get_db_name, tagged, BaseCase
from odoo.tools import mute_logger
Expand All @@ -22,7 +22,7 @@ def setUpClass(cls):
cls.addClassCleanup(cls.cleanUpClass)

with cls.registry.cursor() as cr:
env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
env = api.Environment(cr, api.SUPERUSER_ID, {})
cls.onboarding_id = env['onboarding.onboarding'].create([
{
'name': 'Test Onboarding Concurrent',
Expand All @@ -34,7 +34,7 @@ def setUpClass(cls):
@classmethod
def cleanUpClass(cls):
with cls.registry.cursor() as cr:
env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
env = api.Environment(cr, api.SUPERUSER_ID, {})
env['onboarding.onboarding'].browse(cls.onboarding_id).unlink()
env['onboarding.progress'].search([
('onboarding_id', '=', cls.onboarding_id)
Expand All @@ -46,7 +46,7 @@ def test_concurrent_create_progress(self):

def run():
with self.registry.cursor() as cr:
env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
env = api.Environment(cr, api.SUPERUSER_ID, {})
onboarding = env['onboarding.onboarding'].search([
('id', '=', self.onboarding_id)
])
Expand All @@ -69,7 +69,7 @@ def run():
raised_2 = future_2.result(timeout=3)

with self.registry.cursor() as cr:
env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
env = api.Environment(cr, api.SUPERUSER_ID, {})
self.assertEqual(
len(env['onboarding.progress'].search([('onboarding_id', '=', self.onboarding_id)])),
1,
Expand Down
3 changes: 2 additions & 1 deletion addons/product_expiry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from . import models
from . import wizard

from odoo import api, SUPERUSER_ID, Command
from odoo import Command


def _enable_tracking_numbers(env):
""" This hook ensures the tracking numbers are enabled when the module is installed since the
Expand Down
1 change: 0 additions & 1 deletion addons/repair/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from . import wizard
from . import report

from odoo import api, SUPERUSER_ID

def _create_warehouse_data(env):
""" This hook is used to add default repair picking types on every warehouse.
Expand Down
1 change: 1 addition & 0 deletions addons/web/controllers/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import psycopg2

import odoo.api
import odoo.exceptions
import odoo.modules.registry
from odoo import http
Expand Down
6 changes: 3 additions & 3 deletions odoo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
# ----------------------------------------------------------
# Shortcuts
# ----------------------------------------------------------
# The hard-coded super-user id (a.k.a. administrator, or root user).
SUPERUSER_ID = 1


def registry(database_name=None):
"""
Expand All @@ -47,6 +44,9 @@ def registry(database_name=None):
from . import _monkeypatches
_monkeypatches.patch_all()

# ----------------------------------------------------------
# Export admin user constant
from .orm.utils import SUPERUSER_ID

# ----------------------------------------------------------
# Imports
Expand Down
3 changes: 2 additions & 1 deletion odoo/addons/base/models/assetsbundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

from rjsmin import jsmin as rjsmin

from odoo import release, SUPERUSER_ID, _
from odoo import release, _
from odoo.api import SUPERUSER_ID
from odoo.http import request
from odoo.tools import (func, misc, transpile_javascript,
is_odoo_module, SourceMapGenerator, profiler, OrderedSet)
Expand Down
4 changes: 2 additions & 2 deletions odoo/addons/base/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from collections import defaultdict

from odoo import api, fields, models, SUPERUSER_ID, _
from odoo import api, fields, models, _
from odoo.exceptions import AccessError, ValidationError, UserError
from odoo.fields import Domain
from odoo.http import Stream, root, request
Expand Down Expand Up @@ -706,7 +706,7 @@ def regenerate_assets_bundles(self):
("url", "=like", "/web/assets/%"),
('res_model', '=', 'ir.ui.view'),
('res_id', '=', 0),
('create_uid', '=', SUPERUSER_ID),
('create_uid', '=', api.SUPERUSER_ID),
]).unlink()
self.env.registry.clear_cache('assets')

Expand Down
3 changes: 2 additions & 1 deletion odoo/addons/base/models/ir_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import json
from datetime import date

from odoo import api, fields, models, tools, _, SUPERUSER_ID
from odoo import api, fields, models, tools, _
from odoo.api import SUPERUSER_ID
from odoo.exceptions import ValidationError
from odoo.tools import SQL

Expand Down
3 changes: 2 additions & 1 deletion odoo/addons/base/models/ir_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
slugify_lib = None

import odoo
from odoo import api, http, models, tools, SUPERUSER_ID
from odoo import api, http, models, tools
from odoo.api import SUPERUSER_ID
from odoo.exceptions import AccessDenied
from odoo.http import request, Response, ROUTING_KEYS, SAFE_HTTP_METHODS
from odoo.modules.registry import Registry
Expand Down
3 changes: 2 additions & 1 deletion odoo/addons/base/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import threading
import warnings

from odoo import api, fields, models, tools, _, Command, SUPERUSER_ID
from odoo import api, fields, models, tools, _, Command
from odoo.api import SUPERUSER_ID
from odoo.exceptions import ValidationError, UserError
from odoo.osv import expression
from odoo.tools import html2plaintext, file_open, ormcache
Expand Down
4 changes: 2 additions & 2 deletions odoo/addons/base/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from random import randint
from werkzeug import urls

from odoo import api, fields, models, tools, SUPERUSER_ID, _, Command
from odoo import api, fields, models, tools, _, Command
from odoo.exceptions import RedirectWarning, UserError, ValidationError

import typing
Expand Down Expand Up @@ -392,7 +392,7 @@ def _compute_user_id(self):

@api.depends('user_ids.share', 'user_ids.active')
def _compute_partner_share(self):
super_partner = self.env['res.users'].browse(SUPERUSER_ID).partner_id
super_partner = self.env['res.users'].browse(api.SUPERUSER_ID).partner_id
if super_partner in self:
super_partner.partner_share = False
for partner in self - super_partner:
Expand Down
3 changes: 2 additions & 1 deletion odoo/addons/base/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
from lxml.builder import E
from passlib.context import CryptContext as _CryptContext

from odoo import api, fields, models, tools, SUPERUSER_ID, _, Command
from odoo import api, fields, models, tools, _, Command
from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
from odoo.api import SUPERUSER_ID
from odoo.exceptions import AccessDenied, AccessError, UserError, ValidationError
from odoo.fields import Domain
from odoo.http import request, DEFAULT_LANG
Expand Down
4 changes: 2 additions & 2 deletions odoo/addons/base/tests/test_db_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_REPEATABLE_READ

import odoo
from odoo import api
from odoo.modules.registry import Registry
from odoo.sql_db import db_connect, TestCursor
from odoo.tests import common
Expand Down Expand Up @@ -132,7 +132,7 @@ def setUp(self):
# now we make a test cursor for self.cr
self.cr = self.registry.cursor()
self.addCleanup(self.cr.close)
self.env = odoo.api.Environment(self.cr, odoo.SUPERUSER_ID, {})
self.env = api.Environment(self.cr, api.SUPERUSER_ID, {})
self.record = self.env['res.partner'].create({'name': 'Foo'})

def write(self, record, value):
Expand Down
6 changes: 3 additions & 3 deletions odoo/addons/base/tests/test_ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from PIL import Image

import odoo
from odoo.api import SUPERUSER_ID
from odoo.exceptions import AccessError
from odoo.addons.base.models.ir_attachment import IrAttachment
from odoo.addons.base.tests.common import TransactionCaseWithUserDemo
Expand Down Expand Up @@ -317,14 +317,14 @@ def test_read_permission(self):
# Check the user can access his own attachment
attachment_user.datas
# Create an attachment as superuser without res_model/res_id
attachment_admin = self.Attachments.with_user(odoo.SUPERUSER_ID).create({'name': 'foo'})
attachment_admin = self.Attachments.with_user(SUPERUSER_ID).create({'name': 'foo'})
# Check the record cannot be accessed by a regular user
with self.assertRaises(AccessError):
attachment_admin.with_user(self.env.user).datas
# Check the record can be accessed by an admin (other than superuser)
admin_user = self.env.ref('base.user_admin')
# Safety assert that base.user_admin is not the superuser, otherwise the test is useless
self.assertNotEqual(odoo.SUPERUSER_ID, admin_user.id)
self.assertNotEqual(SUPERUSER_ID, admin_user.id)
attachment_admin.with_user(admin_user).datas

def test_with_write_permissions(self):
Expand Down
4 changes: 2 additions & 2 deletions odoo/addons/base/tests/test_res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from types import SimpleNamespace
from unittest.mock import patch

from odoo import Command, SUPERUSER_ID
from odoo.addons.base.models.res_users import is_selection_groups, get_selection_groups, name_selection_groups
from odoo.exceptions import AccessError, UserError, ValidationError
from odoo.api import SUPERUSER_ID
from odoo.exceptions import UserError
from odoo.http import _request_stack
from odoo.tests import Form, TransactionCase, new_test_user, tagged, HttpCase, users, warmup
from odoo.tools import mute_logger
Expand Down
4 changes: 2 additions & 2 deletions odoo/addons/base/tests/test_uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from contextlib import contextmanager
import unittest

from odoo import api, SUPERUSER_ID
from odoo import api
from odoo.tests import common
from odoo.tests.common import BaseCase

Expand All @@ -20,7 +20,7 @@ def environment():
"""
reg = Registry(common.get_db_name())
with reg.cursor() as cr:
yield api.Environment(cr, SUPERUSER_ID, {})
yield api.Environment(cr, api.SUPERUSER_ID, {})


MODULE = 'test_uninstall'
Expand Down
4 changes: 2 additions & 2 deletions odoo/addons/test_access_rights/tests/test_feedback.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from odoo import SUPERUSER_ID, Command
from odoo.api import SUPERUSER_ID
from odoo.exceptions import AccessError
from odoo.fields import Command
from odoo.tests import TransactionCase
from odoo.tools.misc import mute_logger

Expand Down
2 changes: 1 addition & 1 deletion odoo/addons/test_assetsbundle/controllers/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import SUPERUSER_ID
from odoo.api import SUPERUSER_ID
from odoo.http import Controller, request, route

class TestAssetsBundleController(Controller):
Expand Down
8 changes: 4 additions & 4 deletions odoo/addons/test_http/tests/test_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from freezegun import freeze_time
from urllib3.util import parse_url

import odoo
from odoo import api, http
from odoo.tests import new_test_user, tagged, RecordCapturer
from odoo.tools import config, file_open, image_process
from odoo.tools.misc import submap
Expand Down Expand Up @@ -456,7 +456,7 @@ def setUpClass(cls):
'Content-Type': 'image/png',
'Content-Disposition': 'inline; filename=nologo.png'
}
super_user = cls.env['res.users'].browse([odoo.SUPERUSER_ID])
super_user = cls.env['res.users'].browse([api.SUPERUSER_ID])
companies = ResCompany.browse([super_user.company_id.id]) | ResCompany.create(
{
'name': 'Company 2',
Expand Down Expand Up @@ -646,7 +646,7 @@ def _test_upload_small_file(self):
f'{self.base_url()}/web/binary/upload_attachment',
files={'ufile': file},
data={
'csrf_token': odoo.http.Request.csrf_token(self),
'csrf_token': http.Request.csrf_token(self),
'model': 'test_http.stargate',
'id': self.env.ref('test_http.earth').id,
},
Expand Down Expand Up @@ -692,7 +692,7 @@ def test_upload_large_file(self):
f'{self.base_url()}/web/binary/upload_attachment',
files={'ufile': file},
data={
'csrf_token': odoo.http.Request.csrf_token(self),
'csrf_token': http.Request.csrf_token(self),
'model': 'test_http.stargate',
'id': self.env.ref('test_http.earth').id,
'callback': 'callmemaybe',
Expand Down
3 changes: 0 additions & 3 deletions odoo/addons/test_lint/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, SUPERUSER_ID
1 change: 1 addition & 0 deletions odoo/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
readonly,
)
from odoo.orm.environments import Environment
from odoo.orm.utils import SUPERUSER_ID

from odoo.orm.types import ContextType, DomainType, IdType, Self, ValuesType
9 changes: 5 additions & 4 deletions odoo/cli/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import sys
import time

from . import Command
import odoo
from odoo import api
from odoo.modules.registry import Registry
from odoo.tools.populate import populate_models
from odoo.api import Environment

from . import Command

DEFAULT_FACTOR = '10000'
DEFAULT_SEPARATOR = '_'
Expand Down Expand Up @@ -56,11 +57,11 @@ def run(self, cmdargs):
sys.exit("-d/--database/db_name has multiple database, please provide a single one")
registry = Registry(dbnames[0])
with registry.cursor() as cr:
env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {'active_test': False})
env = api.Environment(cr, api.SUPERUSER_ID, {'active_test': False})
self.populate(env, model_factors, separator_code)

@classmethod
def populate(cls, env: Environment, modelname_factors: dict[str, int], separator_code: int):
def populate(cls, env: api.Environment, modelname_factors: dict[str, int], separator_code: int):
model_factors = {
model: factor
for model_name, factor in modelname_factors.items()
Expand Down
9 changes: 5 additions & 4 deletions odoo/cli/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import sys
import threading

import odoo
import odoo # to expose in the shell
from odoo import api
from odoo.modules.registry import Registry
from odoo.service import server
from odoo.tools import config
Expand Down Expand Up @@ -135,9 +136,9 @@ def shell(self, dbname):
threading.current_thread().dbname = dbname
registry = Registry(dbname)
with registry.cursor() as cr:
uid = odoo.SUPERUSER_ID
ctx = odoo.api.Environment(cr, uid, {})['res.users'].context_get()
env = odoo.api.Environment(cr, uid, ctx)
uid = api.SUPERUSER_ID
ctx = api.Environment(cr, uid, {})['res.users'].context_get()
env = api.Environment(cr, uid, ctx)
local_vars['env'] = env
local_vars['self'] = env.user
self.console(local_vars)
Expand Down
4 changes: 2 additions & 2 deletions odoo/modules/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import odoo.sql_db
import odoo.tools.sql
import odoo.tools.translate
from odoo import SUPERUSER_ID, api, tools
from odoo import api, tools
from odoo.tools.misc import SENTINEL

from . import db as modules_db
Expand Down Expand Up @@ -466,7 +466,7 @@ def load_modules(registry: Registry, force_demo: bool = False, status: None = No
# processed_modules: for cleanup step after install
# loaded_modules: to avoid double loading
report = registry._assertion_report
env = api.Environment(cr, SUPERUSER_ID, {})
env = api.Environment(cr, api.SUPERUSER_ID, {})
loaded_modules, processed_modules = load_module_graph(
env,
graph,
Expand Down
Loading

0 comments on commit be09a3a

Please sign in to comment.