[MERGE] from trunk
[odoo/odoo.git] / addons / document_webdav / document_webdav.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 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 from openerp.osv import fields, osv
23 from openerp.tools import config
24
25 import nodes
26
27 class document_davdir(osv.osv):
28     _inherit = 'document.directory'
29
30     _columns = {
31         # Placed here just for a reference
32         'dav_prop_ids': fields.one2many('document.webdav.dir.property', 'dir_id', 'DAV properties'),
33         }
34
35     def get_node_class(self, cr, uid, ids, dbro=None, dynamic=False, context=None):
36         # Note: in this function, nodes come from document_webdav/nodes.py !
37         if dbro is None:
38             dbro = self.browse(cr, uid, ids, context=context)
39
40         if dynamic:
41             return nodes.node_res_obj
42         elif dbro.type == 'directory':
43             return nodes.node_dir
44         elif dbro.type == 'ressource':
45             return nodes.node_res_dir
46         else:
47             raise ValueError("Directory node for %s type.", dbro.type)
48
49     def _prepare_context(self, cr, uid, nctx, context=None):
50         nctx.node_file_class = nodes.node_file
51         # We can fill some more fields, but avoid any expensive function
52         # that might be not worth preparing.
53         nctx.extra_ctx['webdav_path'] = '/'+config.get_misc('webdav','vdir','webdav')
54         usr_obj = self.pool.get('res.users')
55         res = usr_obj.read(cr, uid, uid, ['login','lang'])
56         if res:
57             nctx.extra_ctx['username'] = res['login']
58             nctx.extra_ctx['lang'] = res['lang']
59         # TODO group
60         return
61
62     def _locate_child(self, cr, uid, root_id, uri, nparent, ncontext):
63         """ try to locate the node in uri,
64             Return a tuple (node_dir, remaining_path)
65         """
66         return (nodes.node_database(context=ncontext), uri)
67
68 class dav_dir_property(osv.osv):
69     """ Arbitrary WebDAV properties, attached to document.directories.
70
71     Some DAV properties have to be settable at directories, depending
72     on the database directory structure.
73
74     Example would be the principal-URL.
75
76     There _can_ be properties without a directory, which means that they
77     globally apply to all the directories (aka. collections) of the
78     present database.
79     """
80     _name = 'document.webdav.dir.property'
81
82     _columns = {
83         'create_date': fields.datetime('Date Created', readonly=True),
84         'create_uid':  fields.many2one('res.users', 'Creator', readonly=True),
85         'write_date': fields.datetime('Date Modified', readonly=True),
86         'write_uid':  fields.many2one('res.users', 'Last Modification User', readonly=True),
87         'dir_id': fields.many2one('document.directory', 'Directory', required=False, select=1),
88         'namespace': fields.char('Namespace', size=127, required=True),
89         'name': fields.char('Name', size=64, required=True),
90         'value': fields.text('Value'),
91         'do_subst': fields.boolean('Substitute', required=True),
92         }
93
94     _defaults = {
95         'do_subst': False,
96         }
97
98 class dav_file_property(osv.osv):
99     """ Arbitrary WebDAV properties, attached to ir.attachments.
100
101     A special case is the locks that can be applied on file nodes.
102
103     There _can_ be properties without a file (RFC?), which means that they
104     globally apply to all the attachments of the present database.
105
106     TODO access permissions, per property.
107     """
108     _name = 'document.webdav.file.property'
109
110     _columns = {
111         'create_date': fields.datetime('Date Created', readonly=True),
112         'create_uid':  fields.many2one('res.users', 'Creator', readonly=True),
113         'write_date': fields.datetime('Date Modified', readonly=True),
114         'write_uid':  fields.many2one('res.users', 'Last Modification User', readonly=True),
115         'file_id': fields.many2one('ir.attachment', 'Document', required=False, select=1),
116         'namespace': fields.char('Namespace', size=127, required=True),
117         'name': fields.char('Name', size=64, required=True),
118         'value': fields.text('Value'),
119         'do_subst': fields.boolean('Substitute', required=True),
120         }
121
122     _defaults = {
123         'do_subst': False,
124         }
125
126 #eof
127 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: