oca / oca-decorators Goto Github PK
View Code? Open in Web Editor NEWThis is a library of decorators for Odoo developers.
License: Other
This is a library of decorators for Odoo developers.
License: Other
This is something I've been waiting for a long time, but now that we have this, I don't know which one would be the way to go.
Read the motivation and discussion on implementation from odoo/odoo#11042.
Summarizing, basically I'd love to legally abuse @api.constrains
to have a method that gets executed whenever write()
or create()
are called and include the defined fields.
One way to do it would be just onwrite = odoo.api.constrains
, and leave further work when a incompatibility arises, given the decorator can actually be abused.
Another way to do it would be to do it fine from the beginning. The decorator would be something like:
def onwrite(*fields):
...
When no fields
come in, it is called on every write and create. When there are fields
, it is called only when write
and create
dicts include keys those fields. Recursion should be prevented automatically.
The doubt is how to overload both methods automatically when all I have is a decorator... ๐ค
Ideas?
seems a good idea to me. We already have some modules adding mixins, they might have a better home in a library. But we'd create a new project for that, right?
I also want to create GroupableMixin
some time which adds the function grouped(*fields)
yielding recordsets grouped by the fields given.
I installed this lib through pip install oca-decorators. Actually I need it for the pos_pricelist module for Odoo. Before installation of this lib I cannot install the pricelist Odoo module, after I also can't. Even though the installation of oca-decorators went successful according to the command line. I've tried a reboot, restarting Odoo server but to no avail...
Hi all,
I think that this library finally is not used. Some insights :
@api.foreach
CC : contributors : @lasley, @lmignon
Thanks !
Hi,
The OCA module pos_pricelist (v10.0) is using this lib since a few days. Even the import is protected, the code is broken any installation that has no this lib installed, even if the module is not installed.
What is the good way to import & use this lib, without breaking such deployment.
kind regard.
Implementation
def groups(*groups):
# check if current user are member of one of the given groups.
# If not, raise an error. "To do this action, you should be member of ..."
Call sample
@oca.decorators.groups('group_1', 'group_2', '...')
def function_name(self, args):
# custom action
Main interest
The check will be done if function is called by
(<button name='function_name' />)
So it is a more secured approach that writing <button name='function_name' groups='group_xml_id'/>
that does'nt prevent for the time being case 1 or 2.
Ref of the previous discussion
CC : @NL66278, @ lasley, @pedrobaeza
README points to https://oca.github.io/oca-decorators/ but it's a 404!
cc @lasley
As there's no instruction on how to use it, I have made this sample code:
import oca.decorators
from odoo import models, fields
class Model(models.Model):
_name = 'test'
field = fields.Boolean(compute="_compute_field")
@oca.decorators.foreach
def _compute_field(self):
self.field = False
and I get the error:
Traceback (most recent call last):
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/http.py", line 650, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/http.py", line 687, in dispatch
result = self._call_function(**self.params)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/http.py", line 323, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/service/model.py", line 118, in wrapper
return f(dbname, *args, **kwargs)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/http.py", line 316, in checked_call
result = self.endpoint(*a, **kw)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/http.py", line 966, in call
return self.method(*args, **kw)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/http.py", line 516, in response_wrap
response = f(*args, **kw)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/addons/web/controllers/main.py", line 895, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/addons/web/controllers/main.py", line 887, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/api.py", line 250, in wrapper
return old_api(self, *args, **kwargs)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/models.py", line 5305, in search_read
result = self.read(cr, uid, record_ids, fields, context=read_ctx)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/api.py", line 250, in wrapper
return old_api(self, *args, **kwargs)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/models.py", line 3208, in read
result = BaseModel.read(records, fields, load=load)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/api.py", line 248, in wrapper
return new_api(self, *args, **kwargs)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/models.py", line 3254, in read
values[name] = field.convert_to_read(record[name], use_name_get)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/models.py", line 5769, in getitem
return self._fields[key].get(self, type(self))
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/fields.py", line 831, in get
self.determine_value(record)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/fields.py", line 940, in determine_value
self.compute_value(recs)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/fields.py", line 895, in compute_value
self._compute_value(records)
File "/Datos/Installs/ERP/odoo/9.0/odoo-community/openerp/fields.py", line 885, in _compute_value
getattr(records, self.compute)()
File "/usr/local/lib/python2.7/dist-packages/oca/decorators/foreach.py", line 72, in _foreach
wrapper = decorator(loop, method)
File "/usr/lib/python2.7/dist-packages/decorator.py", line 239, in decorator
return decorate(_func, caller)
File "/usr/lib/python2.7/dist-packages/decorator.py", line 229, in decorate
evaldict, wrapped=func)
File "/usr/lib/python2.7/dist-packages/decorator.py", line 216, in create
self = cls(func, name, signature, defaults, doc, module)
File "/usr/lib/python2.7/dist-packages/decorator.py", line 99, in init
self.name = func.name
AttributeError: 'test' object has no attribute 'name'
Am I doing anything wrong?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.