1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
23 from document import nodes
24 from tools.safe_eval import safe_eval as eval
26 from tools.dict_tools import dict_filter
28 from document.dict_tools import dict_filter
33 class node_acl_mixin(object):
34 def _get_dav_owner(self, cr):
37 def _get_dav_group(self, cr):
40 def _get_dav_supported_privilege_set(self, cr):
43 def _get_dav_current_user_privilege_set(self, cr):
46 def _get_dav_props_hlpr(self, cr, par_class, prop_model,
47 prop_ref_field, res_id):
48 """ Helper for dav properties, usable in subclasses
50 @param par_class The parent class
51 @param prop_model The name of the orm model holding the properties
52 @param prop_ref_field The name of the field at prop_model pointing to us
53 @param res_id the id of self in the corresponing orm table, that should
54 match prop_model.prop_ref_field
56 ret = par_class.get_dav_props(self, cr)
58 propobj = self.context._dirobj.pool.get(prop_model)
59 uid = self.context.uid
60 ctx = self.context.context.copy()
62 # Not really needed because we don't do eval here:
63 # ctx.update({'uid': uid, 'dbname': self.context.dbname })
64 # dict_filter(self.context.extra_ctx, ['username', 'groupname', 'webdav_path'], ctx)
65 sdomain = [(prop_ref_field, '=', False),]
67 sdomain = ['|', (prop_ref_field, '=', res_id)] + sdomain
68 prop_ids = propobj.search(cr, uid, sdomain, context=ctx)
71 for pbro in propobj.browse(cr, uid, prop_ids, context=ctx):
72 ret[pbro.namespace] = ret.get(pbro.namespace, ()) + \
74 # Note that we cannot have properties to conditionally appear
75 # on the context, yet.
79 def _get_dav_eprop_hlpr(self, cr, ns, prop,
80 par_class, prop_model,
81 prop_ref_field, res_id):
82 """ Helper for get dav eprop, usable in subclasses
84 @param namespace the one to search for
85 @param name Name to search for
86 @param par_class The parent class
87 @param prop_model The name of the orm model holding the properties
88 @param prop_ref_field The name of the field at prop_model pointing to us
89 @param res_id the id of self in the corresponing orm table, that should
90 match prop_model.prop_ref_field
92 ret = par_class.get_dav_eprop(self, cr, ns, prop)
96 propobj = self.context._dirobj.pool.get(prop_model)
97 uid = self.context.uid
98 ctx = self.context.context.copy()
100 ctx.update({'uid': uid, 'dbname': self.context.dbname })
101 ctx['node_classname'] = "%s.%s" % (self.__class__.__module__, self.__class__.__name__)
102 dict_filter(self.context.extra_ctx, ['username', 'groupname', 'webdav_path'], ctx)
103 sdomain = [(prop_ref_field, '=', False),('namespace', '=', ns), ('name','=', prop)]
105 sdomain = ['|', (prop_ref_field, '=', res_id)] + sdomain
106 prop_ids = propobj.search(cr, uid, sdomain, context=ctx)
108 pbro = propobj.browse(cr, uid, prop_ids[0], context=ctx)
111 if val.startswith("('") and val.endswith(")"):
112 glbls = { 'urlquote': urllib.quote, }
113 val = eval(val, glbls, ctx)
119 class node_dir(node_acl_mixin, nodes.node_dir):
120 """ override node_dir and add DAV functionality
122 DAV_PROPS = { "DAV:": ('owner', 'group',
123 'supported-privilege-set',
124 'current-user-privilege-set'),
126 DAV_M_NS = { "DAV:" : '_get_dav',}
127 http_options = { 'DAV': ['access-control',] }
129 def get_dav_resourcetype(self, cr):
130 return ('collection', 'DAV:')
132 def get_dav_props(self, cr):
133 return self._get_dav_props_hlpr(cr, nodes.node_dir,
134 'document.webdav.dir.property', 'dir_id', self.dir_id)
136 def get_dav_eprop(self, cr, ns, prop):
137 return self._get_dav_eprop_hlpr(cr, ns, prop, nodes.node_dir,
138 'document.webdav.dir.property', 'dir_id', self.dir_id)
141 class node_file(node_acl_mixin, nodes.node_file):
142 DAV_PROPS = { "DAV:": ('owner', 'group',
143 'supported-privilege-set',
144 'current-user-privilege-set'),
146 DAV_M_NS = { "DAV:" : '_get_dav',}
147 http_options = { 'DAV': ['access-control', ] }
150 def get_dav_resourcetype(self, cr):
153 def get_dav_props(self, cr):
154 return self._get_dav_props_hlpr(cr, nodes.node_dir,
155 None, 'file_id', self.file_id)
156 #'document.webdav.dir.property', 'dir_id', self.dir_id)
158 #def get_dav_eprop(self, cr, ns, prop):
160 class node_database(nodes.node_database):
161 def get_dav_resourcetype(self, cr):
162 return ('collection', 'DAV:')
164 def get_dav_props(self, cr):
165 return self._get_dav_props_hlpr(cr, nodes.node_dir,
166 'document.webdav.dir.property', 'dir_id', False)
168 def get_dav_eprop(self, cr, ns, prop):
169 return self._get_dav_eprop_hlpr(cr, nodes.node_dir, ns, prop,
170 'document.webdav.dir.property', 'dir_id', False)
172 class node_res_obj(node_acl_mixin, nodes.node_res_obj):
173 DAV_PROPS = { "DAV:": ('owner', 'group',
174 'supported-privilege-set',
175 'current-user-privilege-set'),
177 DAV_M_NS = { "DAV:" : '_get_dav',}
178 http_options = { 'DAV': ['access-control',] }
180 def get_dav_resourcetype(self, cr):
181 return ('collection', 'DAV:')
183 def get_dav_props(self, cr):
184 return self._get_dav_props_hlpr(cr, nodes.node_res_obj,
185 'document.webdav.dir.property', 'dir_id', self.dir_id)
187 def get_dav_eprop(self, cr, ns, prop):
188 return self._get_dav_eprop_hlpr(cr, ns, prop, nodes.node_res_obj,
189 'document.webdav.dir.property', 'dir_id', self.dir_id)
192 class node_res_dir(node_acl_mixin, nodes.node_res_dir):
193 DAV_PROPS = { "DAV:": ('owner', 'group',
194 'supported-privilege-set',
195 'current-user-privilege-set'),
197 DAV_M_NS = { "DAV:" : '_get_dav',}
198 http_options = { 'DAV': ['access-control',] }
199 res_obj_class = node_res_obj
201 def get_dav_resourcetype(self, cr):
202 return ('collection', 'DAV:')
204 def get_dav_props(self, cr):
205 return self._get_dav_props_hlpr(cr, nodes.node_res_dir,
206 'document.webdav.dir.property', 'dir_id', self.dir_id)
208 def get_dav_eprop(self, cr, ns, prop):
209 return self._get_dav_eprop_hlpr(cr, ns, prop, nodes.node_res_dir,
210 'document.webdav.dir.property', 'dir_id', self.dir_id)
212 # Some copies, so that this module can replace 'from document import nodes'
213 get_node_context = nodes.get_node_context
214 node_context = nodes.node_context
215 node_class = nodes.node_class
216 node_descriptor = nodes.node_descriptor