Use a Reference field on ir.attachment to store resource informations

Improve the ir.attachment views to be usable on the client side
This commit is contained in:
Cédric Krier
2010-03-23 10:58:24 +01:00
parent a171da0ef3
commit 57cf3835eb
4 changed files with 68 additions and 33 deletions
+2
View File
@@ -1,3 +1,5 @@
* Use a Reference field on ir.attachment to store resource informations
Improve the ir.attachment views to be usable on the client side
* Use basic access authentication for XML-RPC
* Replace child{1,2} attributes in xml views by a unique child
* Models that uses _inherits will search in inherited parents for missing functions
+37 -7
View File
@@ -3,6 +3,8 @@
"Attachment"
from trytond.model import ModelView, ModelSQL, fields
from trytond.config import CONFIG
from trytond.backend import TableHandler
from trytond.pyson import Eval, Bool, Not
import os
try:
import hashlib
@@ -19,8 +21,8 @@ class Attachment(ModelSQL, ModelView):
datas = fields.Function(fields.Binary('Datas'), 'get_datas',
setter='set_datas')
description = fields.Text('Description')
res_model = fields.Char('Resource Model', readonly=True)
res_id = fields.Integer('Resource ID', readonly=True)
resource = fields.Reference('Resource', selection='models_get',
select=1)
link = fields.Char('Link')
digest = fields.Char('Digest', size=32)
collision = fields.Integer('Collision')
@@ -29,14 +31,42 @@ class Attachment(ModelSQL, ModelView):
def __init__(self):
super(Attachment, self).__init__()
self._sql_constraints += [
('res_model_res_id_name',
'UNIQUE (res_model, res_id, name)',
'The names of attachments must be unique by record!'),
('resource_name', 'UNIQUE(resource, name)',
'The names of attachments must be unique by resource!'),
]
def init(self, cursor, module_name):
super(Attachment, self).init(cursor, module_name)
table = TableHandler(cursor, self, module_name)
# Migration from 1.4 res_model and res_id merged into resource
# Reference
if table.column_exist('res_model') and \
table.column_exist('res_id'):
table.drop_constraint('res_model_res_id_name')
cursor.execute('UPDATE "%s" '
'SET "resource" = "res_model"||\',\'||"res_id"' % self._table)
table.drop_column('res_model')
table.drop_column('res_id')
def default_resource(self, cursor, user, context=None):
if context is None:
context = {}
return context.get('resource')
def default_collision(self, cursor, user, context=None):
return 0
def models_get(self, cursor, user, context=None):
model_obj = self.pool.get('ir.model')
model_ids = model_obj.search(cursor, user, [], context=context)
res = []
for model in model_obj.browse(cursor, user, model_ids,
context=context):
res.append([model.model, model.name])
return res
def get_datas(self, cursor, user, ids, name, context=None):
res = {}
db_name = cursor.dbname
@@ -130,8 +160,8 @@ class Attachment(ModelSQL, ModelView):
ids = [ids]
model_names = set()
for attachment in self.browse(cursor, 0, ids, context=context):
if attachment.res_model:
model_names.add(attachment.res_model)
if attachment.resource:
model_names.add(attachment.resource.split(',')[0])
for model_name in model_names:
model_access_obj.check(cursor, user, model_name, mode=mode,
context=context)
+16 -10
View File
@@ -8,17 +8,23 @@ this repository contains the full copyright notices and license terms. -->
<field name="type">form</field>
<field name="arch" type="xml">
<![CDATA[
<form string="Attachments">
<form string="Attachments" col="2">
<label name="resource"/>
<field name="resource"/>
<label name="name"/>
<field name="name"/>
<label name="res_model"/>
<field name="res_model"/>
<label name="res_id"/>
<field name="res_id"/>
<label name="datas"/>
<field name="datas"/>
<separator name="description" colspan="4"/>
<field name="description" colspan="4"/>
<label name="link"/>
<field name="link" widget="url"/>
<notebook colspan="2">
<page string="Preview" name="preview" col="1">
<field name="datas" widget="image"/>
</page>
<page name="description" col="1">
<field name="description"/>
</page>
</notebook>
</form>
]]>
</field>
@@ -28,10 +34,10 @@ this repository contains the full copyright notices and license terms. -->
<field name="type">tree</field>
<field name="arch" type="xml">
<![CDATA[
<tree string="Attachments">
<tree string="Attachments" fill="1">
<field name="resource" select="1"/>
<field name="name" select="1"/>
<field name="res_model" select="1"/>
<field name="res_id"/>
<field name="datas_size"/>
</tree>
]]>
</field>
+13 -16
View File
@@ -52,8 +52,8 @@ class Collection(ModelSQL, ModelView):
for collection in self.browse(cursor, user, ids):
if collection.parent:
attachment_ids = attachment_obj.search(cursor, user, [
('res_model', '=', self._name),
('res_id', '=', collection.parent.id),
('resource', '=', '%s,%s' %
(self._name, collection.parent.id)),
])
for attachment in attachment_obj.browse(cursor, user,
attachment_ids):
@@ -139,8 +139,7 @@ class Collection(ModelSQL, ModelView):
attachment_id = False
if attachment_ids is None:
attachment_ids = attachment_obj.search(cursor, user, [
('res_model', '=', object_name),
('res_id', '=', object_id),
('resource', '=', '%s,%s' % (object_name, object_id)),
], context=context)
attachments = attachment_obj.browse(cursor, user,
attachment_ids, context=context)
@@ -207,8 +206,7 @@ class Collection(ModelSQL, ModelView):
[(object_name, object_id)].get(name, [])
if attachment_ids is None:
attachment_ids = attachment_obj.search(cursor, user, [
('res_model', '=', object_name),
('res_id', '=', object_id),
('resource', '=', '%s,%s' % (object_name, object_id)),
], context=context)
attachments = attachment_obj.browse(cursor, user,
attachment_ids, context=context)
@@ -307,8 +305,7 @@ class Collection(ModelSQL, ModelView):
attachment_obj = self.pool.get('ir.attachment')
attachment_ids = attachment_obj.search(cursor, user, [
('res_model', '=', object_name),
('res_id', '=', object_id),
('resource', '=', '%s,%s' % (object_name, object_id)),
], context=context)
for attachment in attachment_obj.browse(cursor, user, attachment_ids,
context=context):
@@ -528,8 +525,7 @@ class Collection(ModelSQL, ModelView):
'name': name,
'datas': base64.encodestring(data or ''),
'name': name,
'res_model': object_name,
'res_id': object_id,
'resource': '%s,%s' % (object_name, object_id),
}, context=context)
except:
raise DAV_Forbidden
@@ -621,12 +617,13 @@ class Attachment(ModelSQL, ModelView):
def check_collection(self, cursor, user, ids):
collection_obj = self.pool.get('webdav.collection')
for attachment in self.browse(cursor, user, ids):
if attachment.res_model == 'webdav.collection':
collection = collection_obj.browse(cursor, user,
attachment.res_id)
for child in collection.childs:
if child.name == attachment.name:
return False
if attachment.resource:
model_name, record_id = attachment.resource.split(',')
if model_name == 'webdav.collection':
collection = collection_obj.browse(cursor, user, record_id)
for child in collection.childs:
if child.name == attachment.name:
return False
return True
Attachment()