[MERGE]: Merge with lp:openobject-trunk-dev-addons2
[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 import urlparse
26 import urllib
27 from DAV import utils
28 from DAV.propfind import PROPFIND
29 import tools
30
31
32 super_mk_prop_response = PROPFIND.mk_prop_response
33 def mk_prop_response(self, uri, good_props, bad_props, doc):
34     """ make a new <prop> result element
35
36     We differ between the good props and the bad ones for
37     each generating an extra <propstat>-Node (for each error
38     one, that means).
39
40     """
41     re=doc.createElement("D:response")
42     # append namespaces to response
43     nsnum=0
44     for nsname in self.namespaces:
45         re.setAttribute("xmlns:ns"+str(nsnum),nsname)
46         nsnum=nsnum+1
47
48     # write href information
49     uparts=urlparse.urlparse(uri)
50     fileloc=uparts[2]
51     if isinstance(fileloc, unicode):
52         fileloc = fileloc.encode('utf-8')
53     href=doc.createElement("D:href")
54     davpath = self._dataclass.parent.get_davpath()
55     hurl = '%s://%s%s%s' % (uparts[0], uparts[1], davpath, urllib.quote(fileloc))
56     huri=doc.createTextNode(hurl)
57     href.appendChild(huri)
58     re.appendChild(href)
59
60     # write good properties
61     ps=doc.createElement("D:propstat")
62     if good_props:
63         re.appendChild(ps)
64
65     gp=doc.createElement("D:prop")
66     for ns in good_props.keys():
67         ns_prefix="ns"+str(self.namespaces.index(ns))+":"
68         for p,v in good_props[ns].items():
69             if not v:
70                 pass
71             pe=doc.createElement(ns_prefix+str(p))
72             if hasattr(v, '__class__') and v.__class__.__name__ == 'Element':
73                 pe.appendChild(v)
74             else:
75                 if p=="resourcetype":
76                     if v==1:
77                         ve=doc.createElement("D:collection")
78                         pe.appendChild(ve)
79                 else:
80                     ve=doc.createTextNode(tools.ustr(v))
81                     pe.appendChild(ve)
82
83             gp.appendChild(pe)
84
85     ps.appendChild(gp)
86     s=doc.createElement("D:status")
87     t=doc.createTextNode("HTTP/1.1 200 OK")
88     s.appendChild(t)
89     ps.appendChild(s)
90     re.appendChild(ps)
91
92     # now write the errors!
93     if len(bad_props.items()):
94
95         # write a propstat for each error code
96         for ecode in bad_props.keys():
97             ps=doc.createElement("D:propstat")
98             re.appendChild(ps)
99             bp=doc.createElement("D:prop")
100             ps.appendChild(bp)
101
102             for ns in bad_props[ecode].keys():
103                 ns_prefix="ns"+str(self.namespaces.index(ns))+":"
104
105             for p in bad_props[ecode][ns]:
106                 pe=doc.createElement(ns_prefix+str(p))
107                 bp.appendChild(pe)
108
109             s=doc.createElement("D:status")
110             t=doc.createTextNode(utils.gen_estring(ecode))
111             s.appendChild(t)
112             ps.appendChild(s)
113             re.appendChild(ps)
114
115     # return the new response element
116     return re
117
118
119 def mk_propname_response(self,uri,propnames,doc):
120     """ make a new <prop> result element for a PROPNAME request
121
122     This will simply format the propnames list.
123     propnames should have the format {NS1 : [prop1, prop2, ...], NS2: ...}
124
125     """
126     re=doc.createElement("D:response")
127
128     # write href information
129     uparts=urlparse.urlparse(uri)
130     fileloc=uparts[2]
131     if isinstance(fileloc, unicode):
132         fileloc = fileloc.encode('utf-8')
133     href=doc.createElement("D:href")
134     davpath = self._dataclass.parent.get_davpath()
135     hurl = '%s://%s%s%s' % (uparts[0], uparts[1], davpath, urllib.quote(fileloc))
136     huri=doc.createTextNode(hurl)
137     href.appendChild(huri)
138     re.appendChild(href)
139
140     ps=doc.createElement("D:propstat")
141     nsnum=0
142
143     for ns,plist in propnames.items():
144         # write prop element
145         pr=doc.createElement("D:prop")
146         nsp="ns"+str(nsnum)
147         pr.setAttribute("xmlns:"+nsp,ns)
148         nsnum=nsnum+1
149
150     # write propertynames
151     for p in plist:
152         pe=doc.createElement(nsp+":"+p)
153         pr.appendChild(pe)
154
155     ps.appendChild(pr)
156     re.appendChild(ps)
157
158     return re
159
160 PROPFIND.mk_prop_response = mk_prop_response
161 PROPFIND.mk_propname_response = mk_propname_response
162