Skip to content

Commit 6fab67f

Browse files
committed
[MIG] shopinvader_base_url
1 parent c9d783b commit 6fab67f

6 files changed

Lines changed: 41 additions & 25 deletions

File tree

shopinvader_base_url/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"version": "18.0.1.0.0",
99
"category": "tools",
1010
"license": "AGPL-3",
11-
"summary": "Keep history of url for products & categories ",
11+
"summary": "Keep history of url for products & categories",
1212
"author": "Akretion, ACSONE SA/NV",
1313
"website": "https://github.com/shopinvader/odoo-shopinvader-catalog",
1414
"depends": ["base", "base_sparse_field_list_support"],

shopinvader_base_url/models/abstract_url.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def _compute_url_key(self):
8383
def _get_keyword_fields(self):
8484
"""This method return a list of field that will be concatenated
8585
with '-' to generate the url
86-
Ex: if you return ['name', 'code'] the url will be f"{record.name}-{record.code}"
86+
Ex: if you return ['name', 'code'] the url will be
87+
f"{record.name}-{record.code}"
8788
8889
Note: the self already include in the context the lang of the record
8990
Note: you can return key like in depends ex: ["categ_id.name", "code"]
@@ -133,7 +134,7 @@ def _prepare_url(self, referential, lang, url_key):
133134
"res_model": self._name,
134135
"res_id": self.id,
135136
"referential": referential,
136-
"lang_id": self.env["res.lang"]._lang_get_id(lang),
137+
"lang_id": self.env["res.lang"]._lang_get(lang).id,
137138
"manual": False,
138139
}
139140

shopinvader_base_url/models/url_url.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,27 @@ class UrlUrl(models.Model):
1919
res_id = fields.Many2oneReference(
2020
string="Record ID",
2121
help="ID of the target record in the database",
22-
model_field="res_model",
22+
model_field="res_model_field",
2323
readonly=True,
2424
index=True,
2525
)
2626
res_model = fields.Selection(
2727
selection=lambda s: s._get_model_with_url_selection(), readonly=True, index=True
2828
)
29+
res_model_field = fields.Char(
30+
string="Model Name",
31+
compute="_compute_res_model_field",
32+
readonly=True,
33+
store=True,
34+
)
2935
redirect = fields.Boolean(help="If tick this url is a redirection to the new url")
3036
referential = fields.Selection(
3137
selection=lambda s: s._get_all_referential(),
3238
index=True,
3339
default="global",
3440
required=True,
3541
)
36-
lang_id = fields.Many2one("res.lang", "Lang", index=True, required=True)
42+
lang_id = fields.Many2one("res.lang", string="Lang", index=True, required=True)
3743
need_refresh = fields.Boolean()
3844

3945
_sql_constraints = [
@@ -65,6 +71,11 @@ def _get_model_with_url_selection(self):
6571
)
6672
]
6773

74+
@api.depends("res_model")
75+
def _compute_res_model_field(self):
76+
for record in self:
77+
record.res_model_field = record.res_model
78+
6879
def _get_all_referential(self):
6980
"""Return the list of referential for your url, by default it's global
7081
but you can do your own implementation to have url per search engine

shopinvader_base_url/tests/test_abstract_url.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,30 @@
88

99

1010
class TestAbstractUrl(TransactionCase, FakeModelLoader):
11-
@classmethod
12-
def setUpClass(cls):
13-
super().setUpClass()
14-
cls.loader = FakeModelLoader(cls.env, cls.__module__)
15-
cls.loader.backup_registry()
11+
def setUp(self):
12+
super().setUp()
13+
self.loader = FakeModelLoader(self.env, self.__module__)
14+
self.loader.backup_registry()
1615
from .models import FakeCateg, FakeProduct
1716

18-
cls.loader.update_registry([FakeProduct, FakeCateg])
17+
self.loader.update_registry([FakeProduct, FakeCateg])
1918

20-
cls.lang_en = cls.env.ref("base.lang_en")
21-
cls.lang_fr = cls.env.ref("base.lang_fr")
22-
cls.lang_fr.active = True
23-
cls.product = (
24-
cls.env["fake.product"]
19+
self.lang_en = self.env.ref("base.lang_en")
20+
self.lang_fr = self.env.ref("base.lang_fr")
21+
self.lang_fr.active = True
22+
self.product = (
23+
self.env["fake.product"]
2524
.with_context(lang="en_US")
2625
.create({"name": "My Product"})
2726
)
28-
cls.product.with_context(lang="fr_FR").name = "Mon Produit"
27+
self.product.with_context(lang="fr_FR").name = "Mon Produit"
2928

3029
def _expect_url_for_lang(self, lang, url_key):
3130
self.assertEqual(self.product._get_main_url("global", lang).key, url_key)
3231

33-
@classmethod
34-
def tearDownClass(cls):
35-
cls.loader.restore_registry()
36-
super().tearDownClass()
32+
def tearDown(self):
33+
self.loader.restore_registry()
34+
super().tearDown()
3735

3836
def test_update_url_key(self):
3937
self.product._update_url_key("global", "en_US")
@@ -123,7 +121,8 @@ def test_write_inactive(self):
123121
mocked_redirect.assert_called_once()
124122

125123
def test_update_twice_write_once(self):
126-
""""
124+
(
125+
""""
127126
When we update twice the same record, the write method should be called
128127
only once. This is important because for example, by default, in
129128
shopinvader_search_engine_update, when the method write is called,
@@ -133,7 +132,9 @@ def test_update_twice_write_once(self):
133132
times the _update_url_key method. The first time, if every time the
134133
method make a write on the record, the record will end up with the
135134
state to_recompute.
136-
""" ""
135+
"""
136+
""
137+
)
137138

138139
# we mock the write method to check the number of call but we want the
139140
# method to be executed

shopinvader_base_url/views/url_view.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
name="res_model"
1313
invisible="context.get('hide_res_model')"
1414
/>
15+
<field name="res_model_field" invisible="True" />
1516
<field name="key" />
1617
<field name="res_id" invisible="context.get('hide_res_id')" />
1718
<field name="manual" />
@@ -30,6 +31,7 @@
3031
<field name="lang_id" />
3132
<field name="res_model" invisible="context.get('hide_res_model')" />
3233
<field name="key" />
34+
<field name="res_model_field" invisible="True" />
3335
<field name="res_id" invisible="context.get('hide_res_id')" />
3436
<field name="manual" />
3537
<field name="redirect" />
@@ -38,14 +40,14 @@
3840
</record>
3941

4042
<record id="base_url_action_view" model="ir.actions.act_window">
41-
<field name="name">url list Viewer</field>
43+
<field name="name">Url List</field>
4244
<field name="res_model">url.url</field>
4345
<field name="view_mode">list,form</field>
4446
</record>
4547
<!-- This Menu Item Must have a parent -->
4648
<menuitem
4749
id="url_menu_view"
48-
name="Url_rewrite"
50+
name="Url Rewrite"
4951
parent="base.next_id_9"
5052
action="base_url_action_view"
5153
/>

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
odoo-test-helper

0 commit comments

Comments
 (0)