[REF] caldav
[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 #
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 import xml.dom.minidom
23 domimpl = xml.dom.minidom.getDOMImplementation()
24 import urlparse
25 import urllib
26 from DAV import utils
27 from DAV.propfind import PROPFIND
28 import tools
29
30
31 super_mk_prop_response = PROPFIND.mk_prop_response
32 def mk_prop_response(self,uri,good_props,bad_props,doc):        
33     """ make a new <prop> result element 
34
35     We differ between the good props and the bad ones for
36     each generating an extra <propstat>-Node (for each error
37     one, that means).
38     
39     """      
40     re=doc.createElement("D:response")
41     # append namespaces to response
42     nsnum=0
43     for nsname in self.namespaces:
44         re.setAttribute("xmlns:ns"+str(nsnum),nsname)
45         nsnum=nsnum+1
46     
47     # write href information
48     uparts=urlparse.urlparse(uri)
49     fileloc=uparts[2]
50     href=doc.createElement("D:href")
51     huri=doc.createTextNode(uparts[0]+'://'+'/'.join(uparts[1:2]) + urllib.quote(fileloc))
52     href.appendChild(huri)
53     re.appendChild(href)
54
55     # write good properties
56     ps=doc.createElement("D:propstat")
57     if good_props:
58         re.appendChild(ps)
59
60     gp=doc.createElement("D:prop")
61     for ns in good_props.keys():
62         ns_prefix="ns"+str(self.namespaces.index(ns))+":"
63         for p,v in good_props[ns].items():            
64             if not v:
65                 pass
66             pe=doc.createElement(ns_prefix+str(p))
67             if hasattr(v, '__class__') and v.__class__.__name__ == 'Element':
68                 pe.appendChild(v)
69             else:
70                 if p=="resourcetype":
71                     if v==1:
72                         ve=doc.createElement("D:collection")
73                         pe.appendChild(ve)
74                 else:
75                     ve=doc.createTextNode(tools.ustr(v))
76                     pe.appendChild(ve)
77
78             gp.appendChild(pe)
79     
80     ps.appendChild(gp)
81     s=doc.createElement("D:status")
82     t=doc.createTextNode("HTTP/1.1 200 OK")
83     s.appendChild(t)
84     ps.appendChild(s)
85     re.appendChild(ps)
86
87     # now write the errors!
88     if len(bad_props.items()):
89
90         # write a propstat for each error code
91         for ecode in bad_props.keys():
92             ps=doc.createElement("D:propstat")
93             re.appendChild(ps)
94             bp=doc.createElement("D:prop")
95             ps.appendChild(bp)
96
97             for ns in bad_props[ecode].keys():
98                 ns_prefix="ns"+str(self.namespaces.index(ns))+":"
99             
100             for p in bad_props[ecode][ns]:
101                 pe=doc.createElement(ns_prefix+str(p))
102                 bp.appendChild(pe)
103             
104             s=doc.createElement("D:status")
105             t=doc.createTextNode(utils.gen_estring(ecode))
106             s.appendChild(t)
107             ps.appendChild(s)
108             re.appendChild(ps)
109
110     # return the new response element
111     return re
112     
113
114 PROPFIND.mk_prop_response = mk_prop_response
115