Skip to content

Commit

Permalink
Merge d7eab5f into 6a10a5f
Browse files Browse the repository at this point in the history
  • Loading branch information
katyukha committed Dec 8, 2016
2 parents 6a10a5f + d7eab5f commit b9c9590
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 24 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Last changes
============

Release 0.7.1
-------------

- Bugfix in ``Client.ref`` method. now it returns ``Record`` instance,
as mentioned in documentation
- Added ``Record.get(field_name, default)`` method.

Release 0.7.0
-------------

Expand Down
2 changes: 1 addition & 1 deletion examples/Basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.3"
"version": "2.7.12+"
}
},
"nbformat": 4,
Expand Down
20 changes: 10 additions & 10 deletions examples/Examples & HTML tests.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@
" \n",
" <tr>\n",
" \n",
" <th>DB Index</th>\n",
" <th>Index</th>\n",
" \n",
" <th>DB URL</th>\n",
" <th>URL</th>\n",
" \n",
" <th>DB Aliases</th>\n",
" <th>Aliases</th>\n",
" \n",
" </tr>\n",
" \n",
Expand All @@ -373,7 +373,7 @@
" \n",
" <td>xml-rpc://admin@localhost:8069/openerp_proxy_test_db</td>\n",
" \n",
" <td></td>\n",
" <td>ldb</td>\n",
" \n",
" </tr>\n",
" \n",
Expand All @@ -388,7 +388,7 @@
"</div>"
],
"text/plain": [
"<extend_me.Session at 0x2d0cf10>"
"<extend_me.Session at 0x7fb6f9659550>"
]
},
"execution_count": 7,
Expand Down Expand Up @@ -429,11 +429,11 @@
" \n",
" <tr>\n",
" \n",
" <th>DB Index</th>\n",
" <th>Index</th>\n",
" \n",
" <th>DB URL</th>\n",
" <th>URL</th>\n",
" \n",
" <th>DB Aliases</th>\n",
" <th>Aliases</th>\n",
" \n",
" </tr>\n",
" \n",
Expand All @@ -458,7 +458,7 @@
"</div>"
],
"text/plain": [
"<extend_me.Session at 0x2d0cf10>"
"<extend_me.Session at 0x7fb6f9659550>"
]
},
"execution_count": 8,
Expand Down Expand Up @@ -11473,7 +11473,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.3"
"version": "2.7.12+"
}
},
"nbformat": 4,
Expand Down
3 changes: 1 addition & 2 deletions openerp_proxy/connection/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ def __call__(self, *args):


class JSONRPCProxy(object):
""" Wrapper class around XML-RPC's ServerProxy to wrap method's errors
into XMLRPCError class
""" Simple Odoo service proxy wrapper
"""
def __init__(self, host, port, service, ssl=False, ssl_verify=True):
self.host = host
Expand Down
8 changes: 5 additions & 3 deletions openerp_proxy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ def connect(self, **kwargs):
return Client(**init_kwargs)

# Get the uid
if self._pwd is None or self.username is None or self.dbname is None:
raise LoginException("User login and password required "
if not self._pwd or not self.username or not self.dbname:
raise LoginException("User login and password and dbname required "
"for this operation")

uid = self.services['common'].login(self.dbname,
Expand Down Expand Up @@ -395,7 +395,9 @@ def ref(self, xmlid):
[('module', '=', module), ('name', '=', name)],
limit=1)
if res:
return res[0]
res = res[0]
return self[res.model].read_records(res.res_id)

return False

def __getitem__(self, name):
Expand Down
28 changes: 21 additions & 7 deletions openerp_proxy/ext/repr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,27 @@ def _repr_html_(self):
u"</ul>"
)

def _get_aliases_for_url(url):
""" Get list of aliases for url
"""
return u", ".join((
_(aliase)
for aliase, aliase_url in self.aliases.items()
if aliase_url == url
))

def _gen_description_data():
""" Generator for tuples (index, url, aliases)
"""
for url in self.db_list:
yield (
self.index_rev[url],
url,
_get_aliases_for_url(url),
)

return describe_object_html(
((self._index_url(url),
url,
u", ".join((_(al)
for al, aurl in self.aliases.items()
if aurl == url)))
for url in self._databases.keys()),
_gen_description_data(),
caption='Previous connections',
help=help_text,
headers=[u'DB Index', u'DB URL', u'DB Aliases'])
headers=[u'Index', u'URL', u'Aliases'])
12 changes: 12 additions & 0 deletions openerp_proxy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
and other unneccesary information
getpass - getpass.getpass functon from standard python library.
useful, if you do not want live passwords in ipython history.
anyfield - only if ``anyfield`` package is installed
F - ``anyfield.F``. (only if ``anyfiled`` package is installed)
Databases You previously worked with: %(databases)s
Expand Down Expand Up @@ -79,6 +81,16 @@ def prepare_shell_env(session):
'getpass': getpass
}

try:
import anyfield
except ImportError:
pass
else:
_locals.update({
'anyfield': anyfield,
'F': anyfield.F,
})

return _locals, header


Expand Down
24 changes: 24 additions & 0 deletions openerp_proxy/orm/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,30 @@ def copy(self, default=None, context=None):
cache=self._cache,
context=self.context)

def get(self, field_name, default=None):
""" Try to get field *field_name*, if if field name is not available
return *default* value for it
if *default* is None and it is not possible to get field value,
then raises *KeyErro*
:param str field_name: name of field to get value for
:param default: default value for case when no such field
:return: field value
:raises KeyError: if cannot get field value
Note: This may be useful for code that expected to be working for
different Odoo versions which have different database schemes.
"""
try:
res = self[field_name]
except KeyError:
if default is None:
raise
else:
res = default
return res


RecordListMeta = ExtensibleType._('RecordList', with_meta=abc.ABCMeta)

Expand Down
1 change: 1 addition & 0 deletions openerp_proxy/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def test_212_from_url_protocol_host_port_user_db(self):
def test_220_ref_existing(self):
partner = self.client.ref('base.main_partner')
self.assertIsInstance(partner, Record)
self.assertEqual(partner._object.name, 'res.partner')

def test_221_ref_un_existing(self):
partner = self.client.ref('base.unexisting_partner_id')
Expand Down
12 changes: 12 additions & 0 deletions openerp_proxy/tests/test_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,18 @@ def test_copy(self):
self.assertNotEqual(res.ref, self.record.ref)
self.assertEqual(res.ref, 'My Test Copy Partner Ref')

def test_get_existing_field(self):
rec = self.record
self.assertEqual(rec.get('name'), self.record.name)
self.assertEqual(rec.get('name', 'default'), self.record.name)

def test_get_unexisting_field(self):
rec = self.record
with self.assertRaises(KeyError):
rec.get('unexisting_name')

self.assertEqual(rec.get('unexisting_name', 'default'), 'default')


class Test_22_RecordList(BaseTestCase):

Expand Down
1 change: 1 addition & 0 deletions openerp_proxy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def __call__(self, value):

raise UnicodeError('unable to convert to unicode %r' % (value,))


# default converter instance
ustr = UConverter()

Expand Down
2 changes: 1 addition & 1 deletion openerp_proxy/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "0.7.0" # pragma: no cover
version = "0.7.1" # pragma: no cover

0 comments on commit b9c9590

Please sign in to comment.