From 62a2dce9bc306a483f628e76973b04640835626e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 16 Feb 2008 17:35:02 +0100 Subject: [PATCH] Fix some webdav commands and add constraint on directory and attachment names --- trytond/ir/attachment.py | 6 +++-- trytond/webdav/webdav.py | 52 ++++++++++++++++++++++++++++++++++++---- trytond/webdavsvc.py | 4 ++-- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/trytond/ir/attachment.py b/trytond/ir/attachment.py index 0e4d5fb7d6..268d6e8142 100644 --- a/trytond/ir/attachment.py +++ b/trytond/ir/attachment.py @@ -15,8 +15,10 @@ class Attachment(OSV): type='binary', string='Datas'), 'datas_fname': fields.Char('Data Filename',size=64), 'description': fields.Text('Description'), - 'res_model': fields.Char('Resource Model',size=64, readonly=True), - 'res_id': fields.Integer('Resource ID', readonly=True), + 'res_model': fields.Char('Resource Model',size=64, + readonly=True, required=True), + 'res_id': fields.Integer('Resource ID', readonly=True, + required=True), 'link': fields.Char('Link', size=256), 'digest': fields.Char('Digest', size=32), 'collision': fields.Integer('Collision'), diff --git a/trytond/webdav/webdav.py b/trytond/webdav/webdav.py index 35de65245c..bebca262c4 100644 --- a/trytond/webdav/webdav.py +++ b/trytond/webdav/webdav.py @@ -29,11 +29,29 @@ class Directory(OSV): _constraints = [ ('check_recursion', 'Error! You can not create recursive directories.', ['parent']), + ('check_attachment', + 'Error! You can not create a directory \n' \ + 'with the same name of an existing file \n' \ + 'inside the same directory.', ['name']), ] ext2mime = { '.png': 'image/png', } + def check_attachment(self, cursor, user, ids): + attachment_obj = self.pool.get('ir.attachment') + for directory in self.browse(cursor, user, ids): + if directory.parent: + attachment_ids = attachment_obj.search(cursor, user, [ + ('res_model', '=', self._name), + ('res_id', '=', directory.parent.id), + ]) + for attachment in attachment_obj.browse(cursor, user, + attachment_ids): + if attachment.datas_fname == directory.name: + return False + return True + def _uri2object(self, cursor, user, uri, object_name=_name, object_id=False, context=None): attachment_obj = self.pool.get('ir.attachment') @@ -194,13 +212,14 @@ class Directory(OSV): from DAV.utils import get_uriparentpath, get_urifilename object_name, object_id = self._uri2object(cursor, user, get_uriparentpath(uri), context=context) - if object_name in ('ir.attachment') \ + if not object_name \ + or object_name in ('ir.attachment') \ or not object_id: raise DAV_Forbidden attachment_obj = self.pool.get('ir.attachment') - object_name, object_id = self._uri2object(cursor, user, uri, + object_name2, object_id2 = self._uri2object(cursor, user, uri, context=context) - if not object_id: + if not object_id2: name = get_urifilename(uri) try: attachment_obj.create(cursor, user, { @@ -214,7 +233,7 @@ class Directory(OSV): raise DAV_Forbidden else: try: - attachment_obj.write(cursor, user, object_id, { + attachment_obj.write(cursor, user, object_id2, { 'datas': base64.encodestring(data), }, context=context) except: @@ -275,3 +294,28 @@ class Directory(OSV): return None Directory() + + +class Attachment(OSV): + _name = 'ir.attachment' + + def __init__(self, pool): + super(Attachment, self).__init__(pool) + self._constraints.append( + ('check_directory', + 'Error! You can not create a attachment \n' \ + 'on a directory that have the same name \n' \ + 'than a child directory.', ['datas_fname'])) + + def check_directory(self, cursor, user, ids): + directory_obj = self.pool.get('webdav.directory') + for attachment in self.browse(cursor, user, ids): + if attachment.res_model == 'webdav.directory': + directory = directory_obj.browse(cursor, user, + attachment.res_id) + for child in directory.childs: + if child.name == attachment.name: + return False + return True + +Attachment() diff --git a/trytond/webdavsvc.py b/trytond/webdavsvc.py index 6dc2682076..f2b91bff0a 100644 --- a/trytond/webdavsvc.py +++ b/trytond/webdavsvc.py @@ -256,8 +256,8 @@ class TrytonDAVInterface(iface.dav_interface): def copytree(self, src, dst, overwrite): return copytree(self, src, dst, overwrite) - def moveone(self, uri): - return moveone(self, uri) + def moveone(self, src, dst, overwrite): + return moveone(self, src, dst, overwrite) def movetree(self, src, dst, overwrite): return movetree(self, src, dst, overwrite)