1 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
2 # This module is free software, and you may redistribute it and/or modify
3 # under the same terms as Python, so long as this copyright message and
4 # disclaimer are retained in their original form.
5 #
6 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
7 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
8 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
9 # POSSIBILITY OF SUCH DAMAGE.
10 #
11 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
12 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
14 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
15 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
16 #
17 # $Id: ZRoundup.py,v 1.1 2001-12-12 23:27:13 richard Exp $
18 #
19 ''' ZRoundup module - exposes the roundup web interface to Zope
20 '''
21 from Globals import InitializeClass, HTMLFile
22 from OFS.SimpleItem import Item
23 from OFS.PropertyManager import PropertyManager
24 from Acquisition import Implicit
25 from Persistence import Persistent
26 from AccessControl import ClassSecurityInfo
27 from AccessControl import ModuleSecurityInfo
28 modulesecurity = ModuleSecurityInfo()
30 import roundup.instance
31 from roundup import cgi_client
33 modulesecurity.declareProtected('View management screens', 'manage_addZRoundupForm')
34 manage_addZRoundupForm = HTMLFile('dtml/manage_addZRoundupForm', globals())
36 modulesecurity.declareProtected('Add Z Roundups', 'manage_addZRoundup')
37 def manage_addZRoundup(self, id, instance_home, REQUEST):
38 """Add a ZRoundup product """
39 # validate the instance_home
40 roundup.instance.open(instance_home)
41 self._setObject(id, ZRoundup(id, instance_home))
42 return self.manage_main(self, REQUEST)
44 class RequestWrapper:
45 '''Make the Zope RESPONSE look like a BaseHTTPServer
46 '''
47 def __init__(self, RESPONSE):
48 self.RESPONSE = RESPONSE
49 self.wfile = self.RESPONSE
50 def send_response(self, status):
51 self.RESPONSE.setStatus(status)
52 def send_header(self, header, value):
53 self.RESPONSE.addHeader(header, value)
54 def end_headers(self):
55 # not needed - the RESPONSE object handles this internally on write()
56 pass
58 class FormItem:
59 '''Make a Zope form item look like a cgi.py one
60 '''
61 def __init__(self, value):
62 self.value = value
63 if hasattr(self.value, 'filename'):
64 self.filename = self.value.filename
65 self.file = self.value
67 class FormWrapper:
68 '''Make a Zope form dict look like a cgi.py one
69 '''
70 def __init__(self, form):
71 self.form = form
72 def __getitem__(self, item):
73 return FormItem(self.form[item])
74 def has_key(self, item):
75 return self.form.has_key(item)
76 def keys(self):
77 return self.form.keys()
79 class ZRoundup(Item, PropertyManager, Implicit, Persistent):
80 '''An instance of this class provides an interface between Zope and roundup for one
81 roundup instance
82 '''
83 meta_type = 'Z Roundup'
84 security = ClassSecurityInfo()
86 def __init__(self, id, instance_home):
87 self.id = id
88 self.instance_home = instance_home
90 # define the properties that define this object
91 _properties = (
92 {'id':'id', 'type': 'string', 'mode': 'w'},
93 {'id':'instance_home', 'type': 'string', 'mode': 'w'},
94 )
95 property_extensible_schema__ = 0
97 # define the tabs for the management interface
98 manage_options= PropertyManager.manage_options + (
99 {'label': 'View', 'action':'index_html'},
100 ) + Item.manage_options
102 icon = "misc_/ZRoundup/icon"
104 security.declarePrivate('_opendb')
105 def _opendb(self):
106 '''Open the roundup instnace database for a transaction
107 '''
108 instance = roundup.instance.open(self.instance_home)
109 request = RequestWrapper(self.REQUEST['RESPONSE'])
110 env = self.REQUEST.environ
111 env['INSTANCE_NAME'] = self.id
112 if env['REQUEST_METHOD'] == 'GET':
113 # force roundup to re-parse the request because Zope fiddles
114 # with it and we lose all the :filter, :columns, etc goodness
115 form = None
116 else:
117 form = FormWrapper(self.REQUEST.form)
118 return instance.Client(instance, request, env, form)
120 security.declareProtected('View', 'index_html')
121 def index_html(self):
122 '''Alias index_html to roundup's index
123 '''
124 client = self._opendb()
125 # fake the path that roundup should use
126 client.split_path = ['index']
127 return client.main()
129 def __getitem__(self, item):
130 '''All other URL accesses are passed throuh to roundup
131 '''
132 try:
133 client = self._opendb()
134 # fake the path that roundup should use
135 client.split_path = [item]
136 # and call roundup to do something
137 client.main()
138 return ''
139 except cgi_client.NotFound:
140 raise 'NotFound', self.REQUEST.URL
141 pass
142 except:
143 import traceback
144 traceback.print_exc()
145 # all other exceptions in roundup are valid
146 raise
147 raise KeyError, item
150 InitializeClass(ZRoundup)
151 modulesecurity.apply(globals())
154 #
155 # $Log: not supported by cvs2svn $
156 #
157 #
158 # vim: set filetype=python ts=4 sw=4 et si