Doc Webdav: Cleanup code, fix db listing, children paths.
[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     href=doc.createElement("D:href")
52     davpath = self._dataclass.parent.get_davpath()
53     hurl = '%s://%s%s%s' % (uparts[0], uparts[1], davpath, urllib.quote(fileloc))
54     huri=doc.createTextNode(hurl)
55     href.appendChild(huri)
56     re.appendChild(href)
57
58     # write good properties
59     ps=doc.createElement("D:propstat")
60     if good_props:
61         re.appendChild(ps)
62
63     gp=doc.createElement("D:prop")
64     for ns in good_props.keys():
65         ns_prefix="ns"+str(self.namespaces.index(ns))+":"
66         for p,v in good_props[ns].items():            
67             if not v:
68                 pass
69             pe=doc.createElement(ns_prefix+str(p))
70             if hasattr(v, '__class__') and v.__class__.__name__ == 'Element':
71                 pe.appendChild(v)
72             else:
73                 if p=="resourcetype":
74                     if v==1:
75                         ve=doc.createElement("D:collection")
76                         pe.appendChild(ve)
77                 else:
78                     ve=doc.createTextNode(tools.ustr(v))
79                     pe.appendChild(ve)
80
81             gp.appendChild(pe)
82     
83     ps.appendChild(gp)
84     s=doc.createElement("D:status")
85     t=doc.createTextNode("HTTP/1.1 200 OK")
86     s.appendChild(t)
87     ps.appendChild(s)
88     re.appendChild(ps)
89
90     # now write the errors!
91     if len(bad_props.items()):
92
93         # write a propstat for each error code
94         for ecode in bad_props.keys():
95             ps=doc.createElement("D:propstat")
96             re.appendChild(ps)
97             bp=doc.createElement("D:prop")
98             ps.appendChild(bp)
99
100             for ns in bad_props[ecode].keys():
101                 ns_prefix="ns"+str(self.namespaces.index(ns))+":"
102             
103             for p in bad_props[ecode][ns]:
104                 pe=doc.createElement(ns_prefix+str(p))
105                 bp.appendChild(pe)
106             
107             s=doc.createElement("D:status")
108             t=doc.createTextNode(utils.gen_estring(ecode))
109             s.appendChild(t)
110             ps.appendChild(s)
111             re.appendChild(ps)
112
113     # return the new response element
114     return re
115     
116
117 def mk_propname_response(self,uri,propnames,doc):
118     """ make a new <prop> result element for a PROPNAME request 
119
120     This will simply format the propnames list.
121     propnames should have the format {NS1 : [prop1, prop2, ...], NS2: ...}
122
123     """
124     re=doc.createElement("D:response")
125
126     # write href information
127     uparts=urlparse.urlparse(uri)
128     fileloc=uparts[2]
129     href=doc.createElement("D:href")
130     davpath = self._dataclass.parent.get_davpath()
131     hurl = '%s://%s%s%s' % (uparts[0], uparts[1], davpath, urllib.quote(fileloc))
132     huri=doc.createTextNode(hurl)
133     href.appendChild(huri)
134     re.appendChild(href)
135
136     ps=doc.createElement("D:propstat")
137     nsnum=0
138
139     for ns,plist in propnames.items():
140         # write prop element
141         pr=doc.createElement("D:prop")
142         nsp="ns"+str(nsnum)
143         pr.setAttribute("xmlns:"+nsp,ns)
144         nsnum=nsnum+1
145
146     # write propertynames
147     for p in plist:
148         pe=doc.createElement(nsp+":"+p)
149         pr.appendChild(pe)
150
151     ps.appendChild(pr)
152     re.appendChild(ps)
153
154     return re
155
156 PROPFIND.mk_prop_response = mk_prop_response
157 PROPFIND.mk_propname_response = mk_propname_response
158