Launchpad automatic translations update.
[odoo/odoo.git] / addons / document / document_directory.py
index 8c50164..2a3f54f 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 ##############################################################################
-#    
+#
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 #
 #    GNU Affero General Public License for more details.
 #
 #    You should have received a copy of the GNU Affero General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
 
-import base64
 
 from osv import osv, fields
 from osv.orm import except_orm
-import urlparse
 
-import os
 import nodes
 from tools.translate import _
 
 class document_directory(osv.osv):
     _name = 'document.directory'
     _description = 'Directory'
-    _order = 'name desc'
-    _log_create=True
-
+    _order = 'name'
     _columns = {
         'name': fields.char('Name', size=64, required=True, select=1),
         'write_date': fields.datetime('Date Modified', readonly=True),
         'write_uid':  fields.many2one('res.users', 'Last Modification User', readonly=True),
         'create_date': fields.datetime('Date Created', readonly=True),
         'create_uid':  fields.many2one('res.users', 'Creator', readonly=True),
-        'file_type': fields.char('Content Type', size=32),
         'domain': fields.char('Domain', size=128, help="Use a domain if you want to apply an automatic filter on visible resources."),
         'user_id': fields.many2one('res.users', 'Owner'),
-        'storage_id': fields.many2one('document.storage', 'Storage'),
+        'storage_id': fields.many2one('document.storage', 'Storage', change_default=True),
         'group_ids': fields.many2many('res.groups', 'document_directory_group_rel', 'item_id', 'group_id', 'Groups'),
-        'parent_id': fields.many2one('document.directory', 'Parent Item'),
+        'parent_id': fields.many2one('document.directory', 'Parent Directory', select=1, change_default=True),
         'child_ids': fields.one2many('document.directory', 'parent_id', 'Children'),
         'file_ids': fields.one2many('ir.attachment', 'parent_id', 'Files'),
         'content_ids': fields.one2many('document.directory.content', 'directory_id', 'Virtual Files'),
-        'type': fields.selection([('directory','Static Directory'),('ressource','Other Resources')], 'Type', required=True),
-        'ressource_type_id': fields.many2one('ir.model', 'Directories Mapped to Objects',
-            help="Select an object here and Open ERP will create a mapping for each of these " \
-                 "objects, using the given domain, when browsing through FTP."),
+        'type': fields.selection([
+            ('directory','Static Directory'),
+            ('ressource','Folders per resource'),
+            ],
+            'Type', required=True, select=1, change_default=True,
+            help="Each directory can either have the type Static or be linked to another resource. A static directory, as with Operating Systems, is the classic directory that can contain a set of files. The directories linked to systems resources automatically possess sub-directories for each of resource types defined in the parent directory."),
+        'ressource_type_id': fields.many2one('ir.model', 'Resource model', change_default=True,
+            help="Select an object here and there will be one folder per record of that resource."),
         'resource_field': fields.many2one('ir.model.fields', 'Name field', help='Field to be used as name on resource directories. If empty, the "name" will be used.'),
-        'ressource_parent_type_id': fields.many2one('ir.model', 'Parent Model',
+        'resource_find_all': fields.boolean('Find all resources', required=True,
+                help="If true, all attachments that match this resource will " \
+                    " be located. If false, only ones that have this as parent." ),
+        'ressource_parent_type_id': fields.many2one('ir.model', 'Parent Model', change_default=True,
             help="If you put an object here, this directory template will appear bellow all of these objects. " \
+                 "Such directories are \"attached\" to the specific model or record, just like attachments. " \
                  "Don't put a parent directory if you select a parent model."),
-        'ressource_id': fields.integer('Resource ID'),
+        'ressource_id': fields.integer('Resource ID',
+            help="Along with Parent Model, this ID attaches this folder to a specific record of Parent Model."),
         'ressource_tree': fields.boolean('Tree Structure',
             help="Check this if you want to use the same tree structure as the object selected in the system."),
         'dctx_ids': fields.one2many('document.directory.dctx', 'dir_id', 'Context fields'),
+        'company_id': fields.many2one('res.company', 'Company', change_default=True),
     }
 
 
@@ -80,10 +84,10 @@ class document_directory(osv.osv):
             return False
         return objid.browse(cr, uid, mid, context=context).res_id
 
-    def _get_def_storage(self,cr,uid,context=None):
+    def _get_def_storage(self, cr, uid, context=None):
         if context and context.has_key('default_parent_id'):
                 # Use the same storage as the parent..
-                diro = self.browse(cr,uid,context['default_parent_id'])
+                diro = self.browse(cr, uid, context['default_parent_id'])
                 if diro.storage_id:
                         return diro.storage_id.id
         objid=self.pool.get('ir.model.data')
@@ -92,19 +96,22 @@ class document_directory(osv.osv):
                 return objid.browse(cr, uid, mid, context=context).res_id
         except Exception:
                 return None
-        
+
     _defaults = {
+        'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'document.directory', context=c),
         'user_id': lambda self,cr,uid,ctx: uid,
-        'domain': lambda self,cr,uid,ctx: '[]',
-        'type': lambda *args: 'directory',
-        'ressource_id': lambda *a: 0,        
-        'storage_id': _get_def_storage,
+        'domain': '[]',
+        'type': 'directory',
+        'ressource_id': 0,
+        'storage_id': _get_def_storage, # Still, it is bad practice to set it everywhere.
+        'resource_find_all': True,
     }
     _sql_constraints = [
         ('dirname_uniq', 'unique (name,parent_id,ressource_id,ressource_parent_type_id)', 'The directory name must be unique !'),
-        ('no_selfparent', 'check(parent_id <> id)', 'Directory cannot be parent of itself!')
+        ('no_selfparent', 'check(parent_id <> id)', 'Directory cannot be parent of itself!'),
+        ('dir_parented', 'check(parent_id IS NOT NULL OR storage_id IS NOT NULL)', 'Directory must have a parent or a storage')
     ]
-    def name_get(self, cr, uid, ids, context={}):
+    def name_get(self, cr, uid, ids, context=None):
         res = []
         if not self.search(cr,uid,[('id','in',ids)]):
             ids = []
@@ -120,8 +127,12 @@ class document_directory(osv.osv):
     def get_full_path(self, cr, uid, dir_id, context=None):
         """ Return the full path to this directory, in a list, root first
         """
+        if isinstance(dir_id, (tuple, list)):
+            assert len(dir_id) == 1
+            dir_id = dir_id[0]
+
         def _parent(dir_id, path):
-            parent=self.browse(cr,uid,dir_id)
+            parent=self.browse(cr, uid, dir_id)
             if parent.parent_id and not parent.ressource_parent_type_id:
                 _parent(parent.parent_id.id,path)
                 path.append(parent.name)
@@ -132,31 +143,7 @@ class document_directory(osv.osv):
         _parent(dir_id, path)
         return path
 
-    def ol_get_resource_path(self,cr,uid,dir_id,res_model,res_id):
-        # this method will be used in process module
-        # to be need test and Improvement if resource dir has parent resource (link resource)
-        path=[]
-        def _parent(dir_id,path):
-            parent=self.browse(cr,uid,dir_id)
-            if parent.parent_id and not parent.ressource_parent_type_id:
-                _parent(parent.parent_id.id,path)
-                path.append(parent.name)
-            else:
-                path.append(parent.name)
-                return path
-
-        directory=self.browse(cr,uid,dir_id)
-        model_ids=self.pool.get('ir.model').search(cr,uid,[('model','=',res_model)])
-        if directory:
-            _parent(dir_id,path)
-            path.append(self.pool.get(directory.ressource_type_id.model).browse(cr,uid,res_id).name)
-            #user=self.pool.get('res.users').browse(cr,uid,uid)
-            #return "ftp://%s:%s@localhost:%s/%s/%s"%(user.login,user.password,config.get('ftp_server_port',8021),cr.dbname,'/'.join(path))
-            # No way we will return the password!
-            return "ftp://user:pass@host:port/test/this"
-        return False
-
-    def _check_recursion(self, cr, uid, ids):
+    def _check_recursion(self, cr, uid, ids, context=None):
         level = 100
         while len(ids):
             cr.execute('select distinct parent_id from document_directory where id in ('+','.join(map(str,ids))+')')
@@ -169,9 +156,9 @@ class document_directory(osv.osv):
     _constraints = [
         (_check_recursion, 'Error! You can not create recursive Directories.', ['parent_id'])
     ]
+
     def __init__(self, *args, **kwargs):
-        res = super(document_directory, self).__init__(*args, **kwargs)
-        #self._cache = {}
+        super(document_directory, self).__init__(*args, **kwargs)
 
     def onchange_content_id(self, cr, uid, ids, ressource_type_id):
         return {}
@@ -188,68 +175,80 @@ class document_directory(osv.osv):
         """ Return a node object for the given uri.
            This fn merely passes the call to node_context
         """
-        if not context:
-                context = {}
-        lang = context.get('lang',False)
-        if not lang:
-            user = self.pool.get('res.users').browse(cr, uid, uid)
-            lang = user.context_lang 
-            context['lang'] = lang
-            
-        try: #just instrumentation
-                return nodes.get_node_context(cr, uid, context).get_uri(cr,uri)
-        except Exception,e:
-                print "exception: ",e
-                raise
 
+        return nodes.get_node_context(cr, uid, context).get_uri(cr, uri)
+
+    def get_node_class(self, cr, uid, ids, dbro=None, dynamic=False, context=None):
+        """Retrieve the class of nodes for this directory
+           
+           This function can be overriden by inherited classes ;)
+           @param dbro The browse object, if caller already has it
+        """
+        if dbro is None:
+            dbro = self.browse(cr, uid, ids, context=context)
+
+        if dynamic:
+            return nodes.node_res_obj
+        elif dbro.type == 'directory':
+            return nodes.node_dir
+        elif dbro.type == 'ressource':
+            return nodes.node_res_dir
+        else:
+            raise ValueError("dir node for %s type", dbro.type)
+
+    def _prepare_context(self, cr, uid, nctx, context=None):
+        """ Fill nctx with properties for this database
+        @param nctx instance of nodes.node_context, to be filled
+        @param context ORM context (dict) for us
+        
+        Note that this function is called *without* a list of ids, 
+        it should behave the same for the whole database (based on the
+        ORM instance of document.directory).
+        
+        Some databases may override this and attach properties to the
+        node_context. See WebDAV, CalDAV.
+        """
+        return
+
+    def get_dir_permissions(self, cr, uid, ids, context=None):
+        """Check what permission user 'uid' has on directory 'id'
+        """
+        assert len(ids) == 1
+
+        res = 0
+        for pperms in [('read', 5), ('write', 2), ('unlink', 8)]:
+            try:
+                self.check_access_rule(cr, uid, ids, pperms[0], context=context)
+                res |= pperms[1]
+            except except_orm:
+                pass
+        return res
 
     def _locate_child(self, cr, uid, root_id, uri,nparent, ncontext):
         """ try to locate the node in uri,
             Return a tuple (node_dir, remaining_path)
         """
-        return (nodes.node_database(context=ncontext), uri)         
-        
-    def old_code():
-        if not uri:
-            return node_database(cr, uid, context=context)
-        turi = tuple(uri)
-        node = node_class(cr, uid, '/', False, context=context, type='database')
-        for path in uri[:]:
-            if path:
-                node = node.child(path)
-                if not node:
-                    return False
-        oo = node.object and (node.object._name, node.object.id) or False
-        oo2 = node.object2 and (node.object2._name, node.object2.id) or False
-        return node
-
-    def ol_get_childs(self, cr, uid, uri, context={}):
-        node = self.get_object(cr, uid, uri, context)
-        if uri:
-            children = node.children()
-        else:
-            children= [node]
-        result = map(lambda node: node.path_get(), children)
-        return result
+        return (nodes.node_database(context=ncontext), uri)
 
     def copy(self, cr, uid, id, default=None, context=None):
         if not default:
             default ={}
         name = self.read(cr, uid, [id])[0]['name']
         default.update({'name': name+ " (copy)"})
-        return super(document_directory,self).copy(cr,uid,id,default,context)
+        return super(document_directory,self).copy(cr, uid, id, default, context=context)
 
-    def _check_duplication(self, cr, uid,vals,ids=[],op='create'):
+    def _check_duplication(self, cr, uid, vals, ids=[], op='create'):
         name=vals.get('name',False)
         parent_id=vals.get('parent_id',False)
         ressource_parent_type_id=vals.get('ressource_parent_type_id',False)
         ressource_id=vals.get('ressource_id',0)
         if op=='write':
-            for directory in self.browse(cr,uid,ids):
+            for directory in self.browse(cr, uid, ids):
                 if not name:
                     name=directory.name
                 if not parent_id:
                     parent_id=directory.parent_id and directory.parent_id.id or False
+                # TODO fix algo
                 if not ressource_parent_type_id:
                     ressource_parent_type_id=directory.ressource_parent_type_id and directory.ressource_parent_type_id.id or False
                 if not ressource_id:
@@ -263,17 +262,22 @@ class document_directory(osv.osv):
                 return False
         return True
     def write(self, cr, uid, ids, vals, context=None):
-        if not self._check_duplication(cr,uid,vals,ids,op='write'):
+        if not self._check_duplication(cr, uid, vals, ids, op='write'):
             raise osv.except_osv(_('ValidateError'), _('Directory name must be unique!'))
-        return super(document_directory,self).write(cr,uid,ids,vals,context=context)
+        return super(document_directory,self).write(cr, uid, ids, vals, context=context)
 
     def create(self, cr, uid, vals, context=None):
-        if not self._check_duplication(cr,uid,vals):
+        if not self._check_duplication(cr, uid, vals):
             raise osv.except_osv(_('ValidateError'), _('Directory name must be unique!'))
-        if vals.get('name',False) and (vals.get('name').find('/')+1 or vals.get('name').find('@')+1 or vals.get('name').find('$')+1 or vals.get('name').find('#')+1) :
-            raise osv.except_osv(_('ValidateError'), _('Directory name contains special characters!'))
+        newname = vals.get('name',False)
+        if newname:
+            for illeg in ('/', '@', '$', '#'):
+                if illeg in newname:
+                    raise osv.except_osv(_('ValidateError'), _('Directory name contains special characters!'))
         return super(document_directory,self).create(cr, uid, vals, context)
 
+    # TODO def unlink(...
+
 document_directory()
 
 class document_directory_dctx(osv.osv):
@@ -288,7 +292,7 @@ class document_directory_dctx(osv.osv):
     _name = 'document.directory.dctx'
     _description = 'Directory Dynamic Context'
     _columns = {
-        'dir_id': fields.many2one('document.directory', 'Directory', required=True),
+        'dir_id': fields.many2one('document.directory', 'Directory', required=True, ondelete="cascade"),
         'field': fields.char('Field', size=20, required=True, select=1, help="The name of the field. Note that the prefix \"dctx_\" will be prepended to what is typed here."),
         'expr': fields.char('Expression', size=64, required=True, help="A python expression used to evaluate the field.\n" + \
                 "You can use 'dir_id' for current dir, 'res_id', 'res_model' as a reference to the current record, in dynamic folders"),