Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
243 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
""" | ||
""" | ||
|
||
from ..plugin import Plugin | ||
from ..orm.object import Object | ||
from ..orm.record import (Record, | ||
RecordList) | ||
|
||
import six | ||
|
||
|
||
class ExternalIDS(Plugin): | ||
""" This plugin adds aditional methods to work with | ||
external_ids (xml_id) for Odoo records. | ||
""" | ||
class Meta: | ||
name = "external_ids" | ||
|
||
def get_for(self, val, module=None): | ||
""" Return RecordList of 'ir.model.data' for val or False | ||
:param val: value to get 'ir.model.data' records for | ||
:param str module: module name to search 'ir.model.data' for | ||
:rtype: RecordList | ||
:return: RecordList with 'ir.model.data' records found | ||
:raises ValueError: if *val* argument could not be parsed | ||
``val`` could be one of folowing types: | ||
- ``Record`` instance | ||
- ``RecordList`` instance | ||
- ``tuple(model, res_id)``, for example ``('res.partner', 5)`` | ||
- ``str``, string in format 'module.name'. | ||
Note, that if *module* specified as parametr, then *val* | ||
supposed to be *name* only. | ||
""" | ||
data_obj = self.client['ir.model.data'] | ||
domain = [] if module is None else [('module', '=', module)] | ||
if isinstance(val, Record): | ||
domain +=[('model', '=', val._object.name), | ||
('res_id', '=', val.id)] | ||
elif isinstance(val, RecordList): | ||
domain += [('model', '=', val._object.name), | ||
('res_id', 'in', val.ids)] | ||
elif isinstance(val, (tuple, list)) and len(val) == 2: | ||
model, res_id = val | ||
domain += [('model', '=', model), | ||
('res_id', '=', res_id)] | ||
elif isinstance(val, six.string_types): | ||
if module is None: | ||
try: | ||
module, name = val.split('.') | ||
except ValueError: | ||
raise ValueError( | ||
"Bad xml_id passed. cannot fetch module name.") | ||
else: | ||
name = val | ||
|
||
domain = [('module', '=', module), | ||
('name', '=', name)] | ||
else: | ||
raise ValueError("Cannot parse value [%r]" % val) | ||
|
||
return data_obj.search_records(domain) | ||
|
||
def get_xmlid(self, val, module=None): | ||
""" Return *xml_id* for *val*. | ||
Note, that only first xml_id will be returned! | ||
:param val: look in documentation for | ||
`get_for | ||
<#openerp_proxy.plugins.external_ids.ExternalIDS.get_for>`__ | ||
method | ||
:param str module: module name to search xml_id for | ||
:rtype: str | ||
:return: xml_id for *val* or False if not found | ||
:raises ValueError: if *val* argument could not be parsed | ||
Note, that if *module* specified as parametr, then *val* | ||
supposed to be *name* only | ||
""" | ||
e_record = self.get_for(val, module=module) | ||
if e_record: | ||
return e_record[0].complete_name | ||
return False | ||
|
||
def get_record(self, xml_id, module=None): | ||
""" Return *Record* instance for specified xml_id | ||
:param str xml_id: string with xml_id to search record for | ||
:param str module: module name to search Record in | ||
:rtype: Record | ||
:return: Record for *val* or False if not found | ||
:raises ValueError: if *xml_id* argument could not be parsed | ||
""" | ||
assert isinstance(xml_id, six.string_types), "xml_id must be string" | ||
e_record = self.get_for(xml_id, module=module) | ||
if e_record: | ||
e_record = e_record[0] | ||
return self.client[e_record.model][e_record.res_id] | ||
return False | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version = "0.6.5-dev" # pragma: no cover | ||
version = "0.6.5.dev0" # pragma: no cover |