import types
import openerp
-import openerp.modules.registry
from openerp import SUPERUSER_ID
-from openerp import tools
+from openerp import models, tools
+from openerp.modules.registry import RegistryManager
from openerp.osv import fields, osv
from openerp.osv.orm import BaseModel, Model, MAGIC_COLUMNS, except_orm
from openerp.tools import config
res[model.id] = self.pool["ir.ui.view"].search(cr, uid, [('model', '=', model.model)])
return res
+ def _inherited_models(self, cr, uid, ids, field_name, arg, context=None):
+ res = {}
+ for model in self.browse(cr, uid, ids, context=context):
+ res[model.id] = []
+ inherited_models = [model_name for model_name in self.pool[model.model]._inherits]
+ if inherited_models:
+ res[model.id] = self.search(cr, uid, [('model', 'in', inherited_models)], context=context)
+ return res
+
_columns = {
'name': fields.char('Model Description', translate=True, required=True),
'model': fields.char('Model', required=True, select=1),
'info': fields.text('Information'),
'field_id': fields.one2many('ir.model.fields', 'model_id', 'Fields', required=True, copy=True),
+ 'inherited_model_ids': fields.function(_inherited_models, type="many2many", obj="ir.model", string="Inherited models",
+ help="The list of models that extends the current model."),
'state': fields.selection([('manual','Custom Object'),('base','Base Object')],'Type', readonly=True),
'access_ids': fields.one2many('ir.model.access', 'model_id', 'Access'),
'osv_memory': fields.function(_is_osv_memory, string='Transient Model', type='boolean',
# only reload pool for normal unlink. For module uninstall the
# reload is done independently in openerp.modules.loading
cr.commit() # must be committed before reloading registry in new cursor
- openerp.modules.registry.RegistryManager.new(cr.dbname)
- openerp.modules.registry.RegistryManager.signal_registry_change(cr.dbname)
+ RegistryManager.new(cr.dbname)
+ RegistryManager.signal_registry_change(cr.dbname)
return res
if vals.get('state','base')=='manual':
self.instanciate(cr, user, vals['model'], context)
model = self.pool[vals['model']]
- model._prepare_setup_fields(cr, SUPERUSER_ID)
- model._setup_fields(cr, SUPERUSER_ID)
ctx = dict(context,
field_name=vals['name'],
field_state='manual',
update_custom_fields=True)
model._auto_init(cr, ctx)
model._auto_end(cr, ctx) # actually create FKs!
- openerp.modules.registry.RegistryManager.signal_registry_change(cr.dbname)
+ self.pool.setup_models(cr, partial=(not self.pool.ready))
+ RegistryManager.signal_registry_change(cr.dbname)
return res
def instanciate(self, cr, user, model, context=None):
- class x_custom_model(osv.osv):
- _custom = True
if isinstance(model, unicode):
model = model.encode('utf-8')
- x_custom_model._name = model
- x_custom_model._module = False
- a = x_custom_model._build_model(self.pool, cr)
- if not a._columns:
- x_name = 'id'
- elif 'x_name' in a._columns.keys():
- x_name = 'x_name'
- else:
- x_name = a._columns.keys()[0]
- x_custom_model._rec_name = x_name
- a._rec_name = x_name
+
+ class CustomModel(models.Model):
+ _name = model
+ _module = False
+ _custom = True
+
+ obj = CustomModel._build_model(self.pool, cr)
+ obj._rec_name = CustomModel._rec_name = (
+ 'x_name' if 'x_name' in obj._columns else
+ list(obj._columns)[0] if obj._columns else
+ 'id'
+ )
class ir_model_fields(osv.osv):
_name = 'ir.model.fields'
'translate': fields.boolean('Translatable', help="Whether values for this field can be translated (enables the translation mechanism for that field)"),
'size': fields.integer('Size'),
'state': fields.selection([('manual','Custom Field'),('base','Base Field')],'Type', required=True, readonly=True, select=1),
- 'on_delete': fields.selection([('cascade','Cascade'),('set null','Set NULL')], 'On Delete', help='On delete property for many2one fields'),
+ 'on_delete': fields.selection([('cascade', 'Cascade'), ('set null', 'Set NULL'), ('restrict', 'Restrict')],
+ 'On Delete', help='On delete property for many2one fields'),
'domain': fields.char('Domain', help="The optional domain to restrict possible values for relationship fields, "
"specified as a Python expression defining a list of triplets. "
"For example: [('color','=','red')]"),
column_name = cr.fetchone()
if column_name and (result and result[0] == 'r'):
cr.execute('ALTER table "%s" DROP column "%s" cascade' % (model._table, field.name))
- model._columns.pop(field.name, None)
-
# remove m2m relation table for custom fields
# we consider the m2m relation is only one way as it's not possible
# to specify the relation table in the interface for custom fields
# TODO master: maybe use ir.model.relations for custom fields
if field.state == 'manual' and field.ttype == 'many2many':
- rel_name = self.pool[field.model]._all_columns[field.name].column._rel
+ rel_name = model._fields[field.name].relation
cr.execute('DROP table "%s"' % (rel_name))
+ model._pop_field(field.name)
+
return True
def unlink(self, cr, user, ids, context=None):
res = super(ir_model_fields, self).unlink(cr, user, ids, context)
if not context.get(MODULE_UNINSTALL_FLAG):
cr.commit()
- openerp.modules.registry.RegistryManager.signal_registry_change(cr.dbname)
+ self.pool.setup_models(cr, partial=(not self.pool.ready))
+ RegistryManager.signal_registry_change(cr.dbname)
return res
def create(self, cr, user, vals, context=None):
if self.pool.fields_by_model is not None:
cr.execute('SELECT * FROM ir_model_fields WHERE id=%s', (res,))
self.pool.fields_by_model.setdefault(vals['model'], []).append(cr.dictfetchone())
- model.__init__(self.pool, cr)
- model._prepare_setup_fields(cr, SUPERUSER_ID)
- model._setup_fields(cr, SUPERUSER_ID)
+ model.__init__(self.pool, cr)
#Added context to _auto_init for special treatment to custom field for select_level
ctx = dict(context,
field_name=vals['name'],
update_custom_fields=True)
model._auto_init(cr, ctx)
model._auto_end(cr, ctx) # actually create FKs!
- openerp.modules.registry.RegistryManager.signal_registry_change(cr.dbname)
+ self.pool.setup_models(cr, partial=(not self.pool.ready))
+ RegistryManager.signal_registry_change(cr.dbname)
return res
res = super(ir_model_fields,self).write(cr, user, ids, vals, context=context)
if column_rename:
- cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO "%s"' % column_rename[1])
+ obj, rename = column_rename
+ cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO "%s"' % rename)
# This is VERY risky, but let us have this feature:
- # we want to change the key of column in obj._columns dict
- col = column_rename[0]._columns.pop(column_rename[1][1]) # take object out, w/o copy
- column_rename[0]._columns[column_rename[1][2]] = col
+ # we want to change the key of field in obj._fields and obj._columns
+ field = obj._pop_field(rename[1])
+ obj._add_field(rename[2], field)
if models_patch:
# We have to update _columns of the model(s) and then call their
for __, patch_struct in models_patch.items():
obj = patch_struct[0]
+ # TODO: update new-style fields accordingly
for col_name, col_prop, val in patch_struct[1]:
setattr(obj._columns[col_name], col_prop, val)
obj._auto_init(cr, ctx)
obj._auto_end(cr, ctx) # actually create FKs!
- openerp.modules.registry.RegistryManager.signal_registry_change(cr.dbname)
+
+ if column_rename or models_patch:
+ self.pool.setup_models(cr, partial=(not self.pool.ready))
+ RegistryManager.signal_registry_change(cr.dbname)
+
return res
class ir_model_constraint(Model):