Permalink
Browse files

fields_relational: avoid in-function import

fields_* are loaded *before* orm.py and hence cannot import it. However,
at some places, they need to use its classes (such as browse_record etc).
That's what orm_utils is for. It provides a module member to be filled
with the orm classes.
  • Loading branch information...
1 parent a6db9e4 commit 9280f2dc0e882b633ff54eff75bd957b2c27cbe2 @xrg committed Apr 8, 2012
Showing with 15 additions and 13 deletions.
  1. +10 −13 bin/osv/fields_relational.py
  2. +3 −0 bin/osv/orm.py
  3. +2 −0 bin/tools/orm_utils.py
View
23 bin/osv/fields_relational.py
@@ -26,7 +26,7 @@
import warnings
from tools import sql_model
from tools import expr_utils as eu
-from tools.orm_utils import only_ids
+from tools import orm_utils
from operator import itemgetter
#.apidoc title: Relationals fields
@@ -60,7 +60,6 @@ def _auto_init_prefetch(self, name, obj, prefetch_schema, context=None):
class _rel2one(_relational):
def _val2browse(self, val, name, parent_bro):
- from orm import browse_null, browse_record, except_orm
if val:
obj = parent_bro._table.pool.get(self._obj)
if isinstance(val, (list, tuple)):
@@ -71,7 +70,7 @@ def _val2browse(self, val, name, parent_bro):
value = False
if value:
- assert not isinstance(value, browse_record)
+ assert not isinstance(value, orm_utils.browse_record)
if obj is None:
# In some cases the target model is not available yet,
# but this resolution is late enough to let the model
@@ -84,15 +83,15 @@ def _val2browse(self, val, name, parent_bro):
context = parent_bro._context
global __hush_pyflakes
__hush_pyflakes= (cr, context)
- raise except_orm(_('Error'), _('%s: ORM model %s cannot be found for %s field!') % \
+ raise orm_utils.except_orm(_('Error'), _('%s: ORM model %s cannot be found for %s field!') % \
(parent_bro._table_name, self._obj, name))
- ret = browse_record(parent_bro._cr,
+ ret = orm_utils.browse_record(parent_bro._cr,
parent_bro._uid, value, obj, parent_bro._cache,
context=parent_bro._context,
list_class=parent_bro._list_class,
fields_process=parent_bro._fields_process)
else:
- ret = browse_null()
+ ret = orm_utils.browse_null()
return ret
def _browse2val(self, bro, name):
@@ -119,18 +118,17 @@ class _rel2many(_relational):
""" common baseclass for -2many relation fields
"""
def _val2browse(self, val, name, parent_bro):
- from orm import browse_record
obj = parent_bro._table.pool.get(self._obj)
return parent_bro._list_class([
- browse_record(parent_bro._cr, parent_bro._uid, id, obj,
+ orm_utils.browse_record(parent_bro._cr, parent_bro._uid, id, obj,
parent_bro._cache, context=parent_bro._context, list_class=parent_bro._list_class,
fields_process=parent_bro._fields_process) \
for id in val],
parent_bro._context)
def _browse2val(self, bro, name):
- return only_ids(bro)
+ return orm_utils.only_ids(bro)
def expr_eval(self, cr, uid, obj, lefts, operator, right, pexpr, context):
if len(lefts) > 1: # TODO pg84 reduce
@@ -1140,18 +1138,17 @@ def __init__(self, string, selection, size=64, **args):
_column.__init__(self, string=string, size=size, selection=selection, **args)
def _val2browse(self, val, name, parent_bro):
- from orm import browse_null, browse_record
if not val:
- return browse_null()
+ return orm_utils.browse_null()
ref_obj, ref_id = val.split(',')
ref_id = long(ref_id)
if ref_id:
obj = parent_bro._table.pool.get(ref_obj)
- return browse_record(parent_bro._cr, parent_bro._uid, ref_id, obj, parent_bro._cache,
+ return orm_utils.browse_record(parent_bro._cr, parent_bro._uid, ref_id, obj, parent_bro._cache,
context=parent_bro._context, list_class=parent_bro._list_class,
fields_process=parent_bro._fields_process)
- return browse_null()
+ return orm_utils.browse_null()
def get(self, cr, obj, ids, name, uid=None, context=None, values=None):
"""
View
3 bin/osv/orm.py
@@ -222,6 +222,7 @@ def __add__(self, y):
only_ids = orm_utils.only_ids # take it from there..
orm_utils.browse_record_list = browse_record_list # ..put that one back
orm_utils.browse_null = browse_null
+orm_utils.except_orm = except_orm
class browse_record(object):
""" An object that behaves like a row of an object's table.
@@ -560,6 +561,8 @@ def _invalidate_others(self, ids, model=None):
return
+orm_utils.browse_record = browse_record
+
class orm_template(object):
""" THE base of all ORM models
"""
View
2 bin/tools/orm_utils.py
@@ -29,6 +29,8 @@
browse_record_list = None # MUST be set by orm.py!
browse_record_null = None
+browse_record = None
+except_orm = None
def only_ids(ids):
""" Return the list of ids from either a browse_record_list or plain list

0 comments on commit 9280f2d

Please sign in to comment.