document: improve the attach_parent_id() function.

No need to call an SQL command in iteration! No need to transfer the
db_datas back and forth from Postgres!

Also, prepended an underscore, so that the function is never exported
through RPC.

bzr revid: p_christ@hol.gr-20101223142529-hgudbfj54p4d2e34
This commit is contained in:
P. Christeas 2010-12-23 16:25:29 +02:00
parent f551479a9b
commit 43387c7f43
2 changed files with 30 additions and 10 deletions

View File

@ -36,14 +36,33 @@ class document_file(osv.osv):
_inherit = 'ir.attachment'
_rec_name = 'datas_fname'
def attach_parent_id(self, cr, uid, ids=[], context=None):
"""Attach Parent id For document"""
def _attach_parent_id(self, cr, uid, ids=None, context=None):
"""Migrate ir.attachments to the document module.
When the 'document' module is loaded on a db that has had plain attachments,
they will need to be attached to some parent folder, and be converted from
base64-in-bytea to raw-in-bytea format.
This function performs the internal migration, once and forever, for these
attachments. It cannot be done through the nominal ORM maintenance code,
because the root folder is only created after the document_data.xml file
is loaded.
It also establishes the parent_id NOT NULL constraint that ir.attachment
should have had (but would have failed if plain attachments contained null
values).
"""
parent_id = self.pool.get('document.directory')._get_root_directory(cr,uid)
ids = self.search(cr, uid, [('parent_id', '=', False)])
attach_doc = self.browse(cr, uid, ids, context=context)
for attach in attach_doc:
cr.execute("UPDATE ir_attachment SET parent_id = %s,db_datas = decode(encode(%s,'escape'), 'base64') WHERE id = %s", (parent_id, attach.db_datas, attach.id))
if not parent_id:
logging.getLogger('document').warning("at _attach_parent_id(), still not able to set the parent!")
return False
if ids is not None:
raise NotImplementedError("Ids is just there by convention! Don't use it yet, please.")
cr.execute("UPDATE ir_attachment " \
"SET parent_id = %s, db_datas = decode(encode(db_datas,'escape'), 'base64') " \
"WHERE parent_id IS NULL", (parent_id,))
cr.execute("ALTER TABLE ir_attachment ALTER parent_id SET NOT NULL")
return True
def _get_filestore(self, cr):

View File

@ -98,9 +98,10 @@
<field name="parent_id" ref="dir_root"/>
<field name="ressource_id">0</field>
</record>
<function model="ir.attachment" name="attach_parent_id"/>
<!-- After we have setup the root directory, migrate the attachments
to point to that. -->
<function model="ir.attachment" name="_attach_parent_id"/>
</data>
</openerp>