[MERGE] OPW 279130 - translate errors raised inside product_id_change (sale/purchase...
[odoo/odoo.git] / addons / document / document_directory.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22
23 from osv import osv, fields
24 from osv.orm import except_orm
25
26 import nodes
27 from tools.translate import _
28
29 class document_directory(osv.osv):
30     _name = 'document.directory'
31     _description = 'Directory'
32     _order = 'name'
33     _columns = {
34         'name': fields.char('Name', size=64, required=True, select=1),
35         'write_date': fields.datetime('Date Modified', readonly=True),
36         'write_uid':  fields.many2one('res.users', 'Last Modification User', readonly=True),
37         'create_date': fields.datetime('Date Created', readonly=True),
38         'create_uid':  fields.many2one('res.users', 'Creator', readonly=True),
39         'domain': fields.char('Domain', size=128, help="Use a domain if you want to apply an automatic filter on visible resources."),
40         'user_id': fields.many2one('res.users', 'Owner'),
41         'storage_id': fields.many2one('document.storage', 'Storage', change_default=True),
42         'group_ids': fields.many2many('res.groups', 'document_directory_group_rel', 'item_id', 'group_id', 'Groups'),
43         'parent_id': fields.many2one('document.directory', 'Parent Directory', select=1, change_default=True),
44         'child_ids': fields.one2many('document.directory', 'parent_id', 'Children'),
45         'file_ids': fields.one2many('ir.attachment', 'parent_id', 'Files'),
46         'content_ids': fields.one2many('document.directory.content', 'directory_id', 'Virtual Files'),
47         'type': fields.selection([
48             ('directory','Static Directory'),
49             ('ressource','Folders per resource'),
50             ],
51             'Type', required=True, select=1, change_default=True,
52             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."),
53         'ressource_type_id': fields.many2one('ir.model', 'Resource model', change_default=True,
54             help="Select an object here and there will be one folder per record of that resource."),
55         '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.'),
56         'resource_find_all': fields.boolean('Find all resources', required=True,
57                 help="If true, all attachments that match this resource will " \
58                     " be located. If false, only ones that have this as parent." ),
59         'ressource_parent_type_id': fields.many2one('ir.model', 'Parent Model', change_default=True,
60             help="If you put an object here, this directory template will appear bellow all of these objects. " \
61                  "Such directories are \"attached\" to the specific model or record, just like attachments. " \
62                  "Don't put a parent directory if you select a parent model."),
63         'ressource_id': fields.integer('Resource ID',
64             help="Along with Parent Model, this ID attaches this folder to a specific record of Parent Model."),
65         'ressource_tree': fields.boolean('Tree Structure',
66             help="Check this if you want to use the same tree structure as the object selected in the system."),
67         'dctx_ids': fields.one2many('document.directory.dctx', 'dir_id', 'Context fields'),
68         'company_id': fields.many2one('res.company', 'Company', change_default=True),
69     }
70
71
72     def _get_root_directory(self, cr,uid, context=None):
73         objid=self.pool.get('ir.model.data')
74         try:
75             mid = objid._get_id(cr, uid, 'document', 'dir_root')
76             if not mid:
77                 return False
78             root_id = objid.read(cr, uid, mid, ['res_id'])['res_id']
79             return root_id
80         except Exception, e:
81             import netsvc
82             logger = netsvc.Logger()
83             logger.notifyChannel("document", netsvc.LOG_WARNING, 'Cannot set directory root:'+ str(e))
84             return False
85         return objid.browse(cr, uid, mid, context=context).res_id
86
87     def _get_def_storage(self, cr, uid, context=None):
88         if context and context.has_key('default_parent_id'):
89                 # Use the same storage as the parent..
90                 diro = self.browse(cr, uid, context['default_parent_id'])
91                 if diro.storage_id:
92                         return diro.storage_id.id
93         objid=self.pool.get('ir.model.data')
94         try:
95                 mid =  objid._get_id(cr, uid, 'document', 'storage_default')
96                 return objid.browse(cr, uid, mid, context=context).res_id
97         except Exception:
98                 return None
99
100     _defaults = {
101         'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'document.directory', context=c),
102         'user_id': lambda self,cr,uid,ctx: uid,
103         'domain': '[]',
104         'type': 'directory',
105         'ressource_id': 0,
106         'storage_id': _get_def_storage, # Still, it is bad practice to set it everywhere.
107         'resource_find_all': True,
108     }
109     _sql_constraints = [
110         ('dirname_uniq', 'unique (name,parent_id,ressource_id,ressource_parent_type_id)', 'The directory name must be unique !'),
111         ('no_selfparent', 'check(parent_id <> id)', 'Directory cannot be parent of itself!'),
112         ('dir_parented', 'check(parent_id IS NOT NULL OR storage_id IS NOT NULL)', 'Directory must have a parent or a storage')
113     ]
114     def name_get(self, cr, uid, ids, context=None):
115         res = []
116         if not self.search(cr,uid,[('id','in',ids)]):
117             ids = []
118         for d in self.browse(cr, uid, ids, context=context):
119             s = ''
120             d2 = d
121             while d2 and d2.parent_id:
122                 s = d2.name + (s and ('/' + s) or '')
123                 d2 = d2.parent_id
124             res.append((d.id, s or d.name))
125         return res
126
127     def get_full_path(self, cr, uid, dir_id, context=None):
128         """ Return the full path to this directory, in a list, root first
129         """
130         if isinstance(dir_id, (tuple, list)):
131             assert len(dir_id) == 1
132             dir_id = dir_id[0]
133
134         def _parent(dir_id, path):
135             parent=self.browse(cr, uid, dir_id)
136             if parent.parent_id and not parent.ressource_parent_type_id:
137                 _parent(parent.parent_id.id,path)
138                 path.append(parent.name)
139             else:
140                 path.append(parent.name)
141                 return path
142         path = []
143         _parent(dir_id, path)
144         return path
145
146     def _check_recursion(self, cr, uid, ids, context=None):
147         level = 100
148         while len(ids):
149             cr.execute('select distinct parent_id from document_directory where id in ('+','.join(map(str,ids))+')')
150             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
151             if not level:
152                 return False
153             level -= 1
154         return True
155
156     _constraints = [
157         (_check_recursion, 'Error! You can not create recursive Directories.', ['parent_id'])
158     ]
159
160     def __init__(self, *args, **kwargs):
161         super(document_directory, self).__init__(*args, **kwargs)
162
163     def onchange_content_id(self, cr, uid, ids, ressource_type_id):
164         return {}
165
166     """
167         PRE:
168             uri: of the form "Sales Order/SO001"
169         PORT:
170             uri
171             object: the object.directory or object.directory.content
172             object2: the other object linked (if object.directory.content)
173     """
174     def get_object(self, cr, uid, uri, context=None):
175         """ Return a node object for the given uri.
176            This fn merely passes the call to node_context
177         """
178
179         return nodes.get_node_context(cr, uid, context).get_uri(cr, uri)
180
181     def get_node_class(self, cr, uid, ids, dbro=None, dynamic=False, context=None):
182         """Retrieve the class of nodes for this directory
183            
184            This function can be overriden by inherited classes ;)
185            @param dbro The browse object, if caller already has it
186         """
187         if dbro is None:
188             dbro = self.browse(cr, uid, ids, context=context)
189
190         if dynamic:
191             return nodes.node_res_obj
192         elif dbro.type == 'directory':
193             return nodes.node_dir
194         elif dbro.type == 'ressource':
195             return nodes.node_res_dir
196         else:
197             raise ValueError("dir node for %s type", dbro.type)
198
199     def _prepare_context(self, cr, uid, nctx, context=None):
200         """ Fill nctx with properties for this database
201         @param nctx instance of nodes.node_context, to be filled
202         @param context ORM context (dict) for us
203         
204         Note that this function is called *without* a list of ids, 
205         it should behave the same for the whole database (based on the
206         ORM instance of document.directory).
207         
208         Some databases may override this and attach properties to the
209         node_context. See WebDAV, CalDAV.
210         """
211         return
212
213     def get_dir_permissions(self, cr, uid, ids, context=None):
214         """Check what permission user 'uid' has on directory 'id'
215         """
216         assert len(ids) == 1
217
218         res = 0
219         for pperms in [('read', 5), ('write', 2), ('unlink', 8)]:
220             try:
221                 self.check_access_rule(cr, uid, ids, pperms[0], context=context)
222                 res |= pperms[1]
223             except except_orm:
224                 pass
225         return res
226
227     def _locate_child(self, cr, uid, root_id, uri,nparent, ncontext):
228         """ try to locate the node in uri,
229             Return a tuple (node_dir, remaining_path)
230         """
231         return (nodes.node_database(context=ncontext), uri)
232
233     def copy(self, cr, uid, id, default=None, context=None):
234         if not default:
235             default ={}
236         name = self.read(cr, uid, [id])[0]['name']
237         default.update({'name': name+ " (copy)"})
238         return super(document_directory,self).copy(cr, uid, id, default, context=context)
239
240     def _check_duplication(self, cr, uid, vals, ids=[], op='create'):
241         name=vals.get('name',False)
242         parent_id=vals.get('parent_id',False)
243         ressource_parent_type_id=vals.get('ressource_parent_type_id',False)
244         ressource_id=vals.get('ressource_id',0)
245         if op=='write':
246             for directory in self.browse(cr, uid, ids):
247                 if not name:
248                     name=directory.name
249                 if not parent_id:
250                     parent_id=directory.parent_id and directory.parent_id.id or False
251                 # TODO fix algo
252                 if not ressource_parent_type_id:
253                     ressource_parent_type_id=directory.ressource_parent_type_id and directory.ressource_parent_type_id.id or False
254                 if not ressource_id:
255                     ressource_id=directory.ressource_id and directory.ressource_id or 0
256                 res=self.search(cr,uid,[('id','<>',directory.id),('name','=',name),('parent_id','=',parent_id),('ressource_parent_type_id','=',ressource_parent_type_id),('ressource_id','=',ressource_id)])
257                 if len(res):
258                     return False
259         if op=='create':
260             res=self.search(cr,uid,[('name','=',name),('parent_id','=',parent_id),('ressource_parent_type_id','=',ressource_parent_type_id),('ressource_id','=',ressource_id)])
261             if len(res):
262                 return False
263         return True
264     def write(self, cr, uid, ids, vals, context=None):
265         if not self._check_duplication(cr, uid, vals, ids, op='write'):
266             raise osv.except_osv(_('ValidateError'), _('Directory name must be unique!'))
267         return super(document_directory,self).write(cr, uid, ids, vals, context=context)
268
269     def create(self, cr, uid, vals, context=None):
270         if not self._check_duplication(cr, uid, vals):
271             raise osv.except_osv(_('ValidateError'), _('Directory name must be unique!'))
272         newname = vals.get('name',False)
273         if newname:
274             for illeg in ('/', '@', '$', '#'):
275                 if illeg in newname:
276                     raise osv.except_osv(_('ValidateError'), _('Directory name contains special characters!'))
277         return super(document_directory,self).create(cr, uid, vals, context)
278
279     # TODO def unlink(...
280
281 document_directory()
282
283 class document_directory_dctx(osv.osv):
284     """ In order to evaluate dynamic folders, child items could have a limiting
285         domain expression. For that, their parents will export a context where useful
286         information will be passed on.
287         If you define sth like "s_id" = "this.id" at a folder iterating over sales, its
288         children could have a domain like [('sale_id', = ,dctx_s_id )]
289         This system should be used recursively, that is, parent dynamic context will be
290         appended to all children down the tree.
291     """
292     _name = 'document.directory.dctx'
293     _description = 'Directory Dynamic Context'
294     _columns = {
295         'dir_id': fields.many2one('document.directory', 'Directory', required=True, ondelete="cascade"),
296         '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."),
297         'expr': fields.char('Expression', size=64, required=True, help="A python expression used to evaluate the field.\n" + \
298                 "You can use 'dir_id' for current dir, 'res_id', 'res_model' as a reference to the current record, in dynamic folders"),
299         }
300
301 document_directory_dctx()
302
303
304 class document_directory_node(osv.osv):
305     _inherit = 'process.node'
306     _columns = {
307         'directory_id':  fields.many2one('document.directory', 'Document directory', ondelete="set null"),
308     }
309 document_directory_node()