Skip to content

Commit e4a4dc7

Browse files
committed
[ADD] spec_driven_model module for genDS mixins; WIP
1 parent d359e20 commit e4a4dc7

File tree

10 files changed

+1073
-0
lines changed

10 files changed

+1073
-0
lines changed

spec_driven_model/README.rst

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
2+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
3+
:alt: License: AGPL-3
4+
5+
=================
6+
Spec Driven Model
7+
=================
8+
9+
Tools from specifications driven mixins (from xsd for instance)
10+
11+
Installation
12+
============
13+
14+
To install this module, you need to:
15+
16+
#. Do this ...
17+
18+
Configuration
19+
=============
20+
21+
To configure this module, you need to:
22+
23+
#. Go to ...
24+
25+
.. figure:: path/to/local/image.png
26+
:alt: alternative description
27+
:width: 600 px
28+
29+
Usage
30+
=====
31+
32+
To use this module, you need to:
33+
34+
#. Go to ...
35+
36+
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
37+
:alt: Try me on Runbot
38+
:target: https://runbot.odoo-community.org/runbot/{repo_id}/{branch}
39+
40+
.. repo_id is available in https://github.com/OCA/maintainer-tools/blob/master/tools/repos_with_ids.txt
41+
.. branch is "8.0" for example
42+
43+
Known issues / Roadmap
44+
======================
45+
46+
* ...
47+
48+
Bug Tracker
49+
===========
50+
51+
Bugs are tracked on `GitHub Issues
52+
<https://github.com/OCA/{project_repo}/issues>`_. In case of trouble, please
53+
check there if your issue has already been reported. If you spotted it first,
54+
help us smash it by providing detailed and welcomed feedback.
55+
56+
Credits
57+
=======
58+
59+
Images
60+
------
61+
62+
* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.
63+
64+
Contributors
65+
------------
66+
67+
* Firstname Lastname <[email protected]>
68+
* Second Person <[email protected]>
69+
70+
Funders
71+
-------
72+
73+
The development of this module has been financially supported by:
74+
75+
* Company 1 name
76+
* Company 2 name
77+
78+
Maintainer
79+
----------
80+
81+
.. image:: https://odoo-community.org/logo.png
82+
:alt: Odoo Community Association
83+
:target: https://odoo-community.org
84+
85+
This module is maintained by the OCA.
86+
87+
OCA, or the Odoo Community Association, is a nonprofit organization whose
88+
mission is to support the collaborative development of Odoo features and
89+
promote its widespread use.
90+
91+
To contribute to this module, please visit https://odoo-community.org.

spec_driven_model/__init__.py

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

spec_driven_model/__manifest__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2019 Akretion
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
4+
{
5+
'name': 'Spec Driven Model',
6+
'summary': """
7+
Tools from specifications driven mixins (from xsd for instance)""",
8+
'version': '12.0.1.0.0',
9+
'license': 'AGPL-3',
10+
'author': 'Akretion,Odoo Community Association (OCA)',
11+
'depends': [
12+
],
13+
'data': [
14+
],
15+
'demo': [
16+
],
17+
}

spec_driven_model/hooks.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Copyright (C) 2019 - Raphael Valyi Akretion
2+
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
3+
4+
import sys
5+
import inspect
6+
from odoo import api, models, SUPERUSER_ID
7+
from .models.spec_models import SpecModel, StackedModel
8+
9+
10+
def post_init_hook(cr, registry, module_name, spec_module):
11+
"automatically generate access rules for spec models"
12+
env = api.Environment(cr, SUPERUSER_ID, {})
13+
# TODO no hardcode
14+
remaining_models = get_remaining_spec_models(
15+
cr, registry, module_name, spec_module)
16+
fields = ['id', 'name', 'model_id/id', 'group_id/id',
17+
'perm_read', 'perm_write' , 'perm_create', 'perm_unlink']
18+
access_data = []
19+
for model in remaining_models:
20+
underline_name = model.replace('.', '_')
21+
rec_id = "acl_%s_nfe_40_%s" % ('todo'.split('.')[-1],
22+
underline_name)
23+
# TODO no nfe ref
24+
model_id = "l10n_br_nfe_spec.model_%s" % (underline_name,)
25+
access_data.append([rec_id, underline_name, model_id, 'base.group_user',
26+
'1', '1', '1', '1'])
27+
# TODO make more secure!
28+
env['ir.model.access'].load(fields, access_data)
29+
30+
def get_remaining_spec_models(cr, registry, module_name, spec_module):
31+
cr.execute("""select ir_model.model from ir_model_data
32+
join ir_model on res_id=ir_model.id
33+
where ir_model_data.model='ir.model'
34+
and module=%s;""", (module_name,))
35+
module_models = [i[0] for i in cr.fetchall() if registry.get(i[0])
36+
and not registry[i[0]]._abstract]
37+
38+
injected_models = set()
39+
for model in module_models:
40+
base_class = registry[model]
41+
# 1st classic Odoo classes
42+
if hasattr(base_class, '_inherit'):
43+
injected_models.add(base_class._name)
44+
if isinstance(base_class._inherit, list):
45+
injected_models = injected_models.union(
46+
set(base_class._inherit))
47+
elif base_class._inherit is not None:
48+
injected_models.add(base_class._inherit)
49+
50+
# visit_stack will now need the associated spec classes
51+
injected_classes = set()
52+
remaining_models = set(['nfe.40.tveiculo']) # TODO
53+
# TODO when using a registry loading, use _stack_skip to find
54+
# out which models to leave concrete, see later commented loop
55+
56+
for m in injected_models:
57+
c = SpecModel._odoo_name_to_class(m, spec_module)
58+
if c is not None:
59+
injected_classes.add(c)
60+
61+
for model in module_models:
62+
base_class = registry[model]
63+
# 2nd StackedModel classes, that we will visit
64+
if hasattr(base_class, '_stacked'):
65+
node = SpecModel._odoo_name_to_class(base_class._stacked,
66+
spec_module)
67+
68+
# TODO with registry!!
69+
base_class._visit_stack(node, injected_classes,
70+
base_class._stacked.split('.')[-1],
71+
registry, cr)
72+
# for f in base_class._stack_skip:
73+
# if base_class._fields[]
74+
75+
used_models = [c._name for c in injected_classes]
76+
print(" **** injected spec models (%s): %s" % (
77+
len(used_models), used_models))
78+
# TODO replace by SELECT like for module_models ?
79+
all_spec_models = set([c._name for name, c
80+
in inspect.getmembers(
81+
sys.modules[spec_module], inspect.isclass)])
82+
83+
print("number of all spec models:", len(all_spec_models))
84+
remaining_models = remaining_models.union(
85+
set([i for i in all_spec_models
86+
if i not in [c._name for c in injected_classes]]))
87+
print("\n **** REMAINING spec models to init (%s): %s \n\n" % (
88+
len(remaining_models), remaining_models))
89+
return remaining_models
90+
91+
def register_hook(env, module_name, spec_module):
92+
remaining_models = get_remaining_spec_models(env.cr, env.registry,
93+
module_name, spec_module)
94+
for name in remaining_models:
95+
spec_class = StackedModel._odoo_name_to_class(name, spec_module)
96+
spec_class._module = "fiscal" # TODO use python_module ?
97+
c = type(name, (SpecModel, spec_class),
98+
{'_name': spec_class._name,
99+
'_inherit': ['spec.mixin.nfe'],
100+
'_original_module': "fiscal",
101+
'_rec_name': spec_class._concrete_rec_name})
102+
models.MetaModel.module_to_models[module_name] += [c]
103+
104+
# now we init these models properly
105+
# a bit like odoo.modules.loading#load_module_graph would do.
106+
c._build_model(env.registry, env.cr)
107+
108+
env[name]._prepare_setup()
109+
env[name]._setup_base()
110+
env[name]._setup_fields()
111+
env[name]._setup_complete()
112+
113+
# TODO only in update mode!
114+
env.registry.init_models(env.cr, remaining_models, {'module': module_name})

spec_driven_model/models/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from . import spec_mixin
2+
from . import spec_view
3+
from . import spec_models
4+
from . import spec_import

0 commit comments

Comments
 (0)