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