[I18N] all: updated translation templates
[odoo/odoo.git] / addons / document_webdav / webdav.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #    Copyright (c) 1999 Christian Scholz (ruebe@aachen.heimat.de)
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import xml.dom.minidom
24 domimpl = xml.dom.minidom.getDOMImplementation()
25 from xml.dom.minicompat import StringTypes
26
27 import urlparse
28 import urllib
29 from osv import osv
30 from tools.translate import _
31
32 try:
33     from DAV import utils
34     from DAV.propfind import PROPFIND
35     from DAV.report import REPORT
36 except ImportError:
37     raise osv.except_osv(_('PyWebDAV Import Error!'), _('Please install PyWebDAV from http://code.google.com/p/pywebdav/downloads/detail?name=PyWebDAV-0.9.4.tar.gz&can=2&q=/'))
38
39 import tools
40
41 class Text2(xml.dom.minidom.Text):
42     def writexml(self, writer, indent="", addindent="", newl=""):
43         data = "%s%s%s" % (indent, self.data, newl)
44         data = data.replace("&", "&amp;").replace("<", "&lt;")
45         data = data.replace(">", "&gt;")
46         writer.write(data)
47
48 class Prop2xml(object):
49     """ A helper class to convert property structs to DAV:XML
50     
51         Written to generalize the use of _prop_child(), a class is 
52         needed to hold some persistent data accross the recursions 
53         of _prop_elem_child().
54     """
55     
56     def __init__(self, doc, namespaces, nsnum):
57         """ Init the structure
58         @param doc the xml doc element
59         @param namespaces a dict of namespaces
60         @param nsnum the next namespace number to define
61         """
62         self.doc = doc
63         self.namespaces = namespaces
64         self.nsnum = nsnum
65
66     def createText2Node(self, data):
67         if not isinstance(data, StringTypes):
68             raise TypeError, "node contents must be a string"
69         t = Text2()
70         t.data = data
71         t.ownerDocument = self.doc
72         return t
73
74     def _prop_child(self, xnode, ns, prop, value):
75         """Append a property xml node to xnode, with <prop>value</prop>
76
77            And a little smarter than that, it will consider namespace and
78            also allow nested properties etc.
79
80            :param ns the namespace of the <prop/> node
81            :param prop the name of the property
82            :param value the value. Can be:
83                     string: text node
84                     tuple ('elem', 'ns') for empty sub-node <ns:elem />
85                     tuple ('elem', 'ns', sub-elems) for sub-node with elements
86                     tuple ('elem', 'ns', sub-elems, {attrs}) for sub-node with 
87                             optional elements and attributes
88                     list, of above tuples
89         """
90         if ns == 'DAV:':
91             ns_prefix = 'D:'
92         else:
93             ns_prefix="ns"+str(self.namespaces.index(ns))+":"
94
95         pe = self.doc.createElement(ns_prefix+str(prop))
96         if hasattr(value, '__class__') and value.__class__.__name__ == 'Element':
97             pe.appendChild(value)
98         else:
99             if ns == 'DAV:' and prop=="resourcetype" and isinstance(value, int):
100                 # hack, to go..
101                 if value == 1:
102                     ve = self.doc.createElement("D:collection")
103                     pe.appendChild(ve)
104             else:
105                 self._prop_elem_child(pe, ns, value, ns_prefix)
106
107             xnode.appendChild(pe)
108
109     def _prop_elem_child(self, pnode, pns, v, pns_prefix):
110
111         if isinstance(v, list):
112             for vit in v:
113                 self._prop_elem_child(pnode, pns, vit, pns_prefix)
114         elif isinstance(v,tuple):
115             need_ns = False
116             if v[1] == pns:
117                 ns_prefix = pns_prefix
118             elif v[1] == 'DAV:':
119                 ns_prefix = 'D:'
120             elif v[1] in self.namespaces:
121                 ns_prefix="ns"+str(self.namespaces.index(v[1]))+":"
122             else:
123                 ns_prefix="ns"+str(self.nsnum)+":"
124                 need_ns = True
125
126             ve = self.doc.createElement(ns_prefix+v[0])
127             if need_ns:
128                 ve.setAttribute("xmlns:ns"+str(self.nsnum), v[1])
129             if len(v) > 2 and v[2] is not None:
130                 if isinstance(v[2], (list, tuple)):
131                     # support nested elements like:
132                     # ( 'elem', 'ns:', [('sub-elem1', 'ns1'), ...]
133                     self._prop_elem_child(ve, v[1], v[2], ns_prefix)
134                 else:
135                     vt = self.createText2Node(tools.ustr(v[2]))
136                     ve.appendChild(vt)
137             if len(v) > 3 and v[3]:
138                 assert isinstance(v[3], dict)
139                 for ak, av in v[3].items():
140                     ve.setAttribute(ak, av)
141             pnode.appendChild(ve)
142         else:
143             ve = self.createText2Node(tools.ustr(v))
144             pnode.appendChild(ve)
145
146
147 super_mk_prop_response = PROPFIND.mk_prop_response
148 def mk_prop_response(self, uri, good_props, bad_props, doc):
149     """ make a new <prop> result element
150
151     We differ between the good props and the bad ones for
152     each generating an extra <propstat>-Node (for each error
153     one, that means).
154
155     """
156     re=doc.createElement("D:response")
157     # append namespaces to response
158     nsnum=0
159     namespaces = self.namespaces[:]
160     if 'DAV:' in namespaces:
161         namespaces.remove('DAV:')
162     for nsname in namespaces:
163         re.setAttribute("xmlns:ns"+str(nsnum),nsname)
164         nsnum=nsnum+1
165
166     propgen = Prop2xml(doc, namespaces, nsnum)
167     # write href information
168     uparts=urlparse.urlparse(uri)
169     fileloc=uparts[2]
170     if isinstance(fileloc, unicode):
171         fileloc = fileloc.encode('utf-8')
172     href=doc.createElement("D:href")
173     davpath = self._dataclass.parent.get_davpath()
174     if uparts[0] and uparts[1]:
175         hurl = '%s://%s%s%s' % (uparts[0], uparts[1], davpath, urllib.quote(fileloc))
176     else:
177         # When the request has been relative, we don't have enough data to
178         # reply with absolute url here.
179         hurl = '%s%s' % (davpath, urllib.quote(fileloc))
180     huri=doc.createTextNode(hurl)
181     href.appendChild(huri)
182     re.appendChild(href)
183
184     # write good properties
185     ps=doc.createElement("D:propstat")
186     if good_props:
187         re.appendChild(ps)
188     s=doc.createElement("D:status")
189     t=doc.createTextNode("HTTP/1.1 200 OK")
190     s.appendChild(t)
191     ps.appendChild(s)
192
193     gp=doc.createElement("D:prop")
194     for ns in good_props.keys():
195         if ns == 'DAV:':
196             ns_prefix = 'D:'
197         else:
198             ns_prefix="ns"+str(namespaces.index(ns))+":"
199         for p,v in good_props[ns].items():
200             if v is None:
201                 continue
202             propgen._prop_child(gp, ns, p, v)
203
204     ps.appendChild(gp)
205     re.appendChild(ps)
206
207     # now write the errors!
208     if len(bad_props.items()):
209
210         # write a propstat for each error code
211         for ecode in bad_props.keys():
212             ps=doc.createElement("D:propstat")
213             re.appendChild(ps)
214             s=doc.createElement("D:status")
215             t=doc.createTextNode(utils.gen_estring(ecode))
216             s.appendChild(t)
217             ps.appendChild(s)
218             bp=doc.createElement("D:prop")
219             ps.appendChild(bp)
220
221             for ns in bad_props[ecode].keys():
222                 if ns == 'DAV:':
223                     ns_prefix='D:'
224                 else:
225                     ns_prefix="ns"+str(self.namespaces.index(ns))+":"
226
227             for p in bad_props[ecode][ns]:
228                 pe=doc.createElement(ns_prefix+str(p))
229                 bp.appendChild(pe)
230
231             re.appendChild(ps)
232
233     # return the new response element
234     return re
235
236
237 def mk_propname_response(self,uri,propnames,doc):
238     """ make a new <prop> result element for a PROPNAME request
239
240     This will simply format the propnames list.
241     propnames should have the format {NS1 : [prop1, prop2, ...], NS2: ...}
242
243     """
244     re=doc.createElement("D:response")
245
246     # write href information
247     uparts=urlparse.urlparse(uri)
248     fileloc=uparts[2]
249     if isinstance(fileloc, unicode):
250         fileloc = fileloc.encode('utf-8')
251     href=doc.createElement("D:href")
252     davpath = self._dataclass.parent.get_davpath()
253     if uparts[0] and uparts[1]:
254         hurl = '%s://%s%s%s' % (uparts[0], uparts[1], davpath, urllib.quote(fileloc))
255     else:
256         # When the request has been relative, we don't have enough data to
257         # reply with absolute url here.
258         hurl = '%s%s' % (davpath, urllib.quote(fileloc))
259     huri=doc.createTextNode(hurl)
260     href.appendChild(huri)
261     re.appendChild(href)
262
263     ps=doc.createElement("D:propstat")
264     nsnum=0
265
266     for ns,plist in propnames.items():
267         # write prop element
268         pr=doc.createElement("D:prop")
269         if ns == 'DAV':
270             nsp = 'D'
271         else:
272             nsp="ns"+str(nsnum)
273             ps.setAttribute("xmlns:"+nsp,ns)
274             nsnum=nsnum+1
275
276         # write propertynames
277         for p in plist:
278             pe=doc.createElement(nsp+":"+p)
279             pr.appendChild(pe)
280
281         ps.appendChild(pr)
282
283     re.appendChild(ps)
284
285     return re
286
287 PROPFIND.mk_prop_response = mk_prop_response
288 PROPFIND.mk_propname_response = mk_propname_response
289
290 def mk_lock_response(self, uri, props):
291     """ Prepare the data response to a DAV LOCK command
292     
293     This function is here, merely to be in the same file as the
294     ones above, that have similar code.
295     """
296     doc = domimpl.createDocument('DAV:', "D:prop", None)
297     ms = doc.documentElement
298     ms.setAttribute("xmlns:D", "DAV:")
299     # ms.tagName = 'D:multistatus'
300     namespaces = []
301     nsnum = 0
302     propgen = Prop2xml(doc, namespaces, nsnum)
303     # write href information
304     uparts=urlparse.urlparse(uri)
305     fileloc=uparts[2]
306     if isinstance(fileloc, unicode):
307         fileloc = fileloc.encode('utf-8')
308     davpath = self.parent.get_davpath()
309     if uparts[0] and uparts[1]:
310         hurl = '%s://%s%s%s' % (uparts[0], uparts[1], davpath, urllib.quote(fileloc))
311     else:
312         # When the request has been relative, we don't have enough data to
313         # reply with absolute url here.
314         hurl = '%s%s' % (davpath, urllib.quote(fileloc))
315         
316     props.append( ('lockroot', 'DAV:', ('href', 'DAV:', (hurl))))
317     pld = doc.createElement('D:lockdiscovery')
318     ms.appendChild(pld)
319     propgen._prop_child(pld, 'DAV:', 'activelock', props)
320
321     return doc.toxml(encoding="utf-8")
322
323 super_create_prop = REPORT.create_prop
324
325 def create_prop(self):
326     try:
327         if (self.filter is not None) and self._depth == "0":
328             hrefs = self.filter.getElementsByTagNameNS('DAV:', 'href')
329             if hrefs:
330                 self._depth = "1"
331     except Exception:
332         pass
333     return super_create_prop(self)
334
335 REPORT.create_prop = create_prop
336
337 #eof