comparison simplewiki/handlers.py @ 5:b2fbb4f982da default tip

[mq]: edit
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 07 Sep 2010 22:58:11 -0700
parents dd1c4916cbcd
children
comparison
equal deleted inserted replaced
4:dd1c4916cbcd 5:b2fbb4f982da
123 def __init__(self, app, request): 123 def __init__(self, app, request):
124 Handler.__init__(self, app, request) 124 Handler.__init__(self, app, request)
125 if 'file' not in request.POST: 125 if 'file' not in request.POST:
126 raise HandlerMatchException 126 raise HandlerMatchException
127 self.file = self.request.POST['file'] 127 self.file = self.request.POST['file']
128 if not getattr(self.file, 'filename', None): 128 filename = getattr(self.file, 'filename', '')
129 raise HandlerMatchException 129 if filename:
130 content = self.file.file.read()
131 else:
132 content = self.file
133
130 self.location = request.path_info.rstrip('/') 134 self.location = request.path_info.rstrip('/')
131 path = os.path.join(self.app.directory, *self.request.environ['path']) 135 path = os.path.join(self.app.directory, *self.request.environ['path'])
132 if os.path.isdir(path): 136 if os.path.isdir(path):
133 self.directory = path 137 self.directory = path
134 self.filename = os.path.join(self.directory, self.file.filename) 138 self.filename = os.path.join(self.directory, filename)
135 self.location += '/' + self.file.filename 139 self.location += '/' + filename
136 else: 140 else:
137 self.directory = os.path.dirname(path) 141 self.directory = os.path.dirname(path)
138 self.filename = path 142 self.filename = path
139 143
140 f = file(self.filename, 'wb') 144 f = file(self.filename, 'wb')
141 f.write(self.file.file.read()) 145 f.write(content)
142 f.close() 146 f.close()
143 147
144 def __call__(self): 148 def __call__(self):
145 self.redirect(self.location) 149 self.redirect(self.location)
146 150
154 raise HandlerMatchException 158 raise HandlerMatchException
155 159
156 def __call__(self): 160 def __call__(self):
157 return FileApp(self.file) 161 return FileApp(self.file)
158 162
163 class EditView(Handler):
164 methods = set(['GET'])
165 template = 'edit.html'
166
167 def __init__(self, app, request):
168 if 'edit' not in request.GET:
169 raise HandlerMatchException
170
171 Handler.__init__(self, app, request)
172
173 self.file = os.path.join(self.app.directory, *request.environ['path'])
174 if not os.path.exists(self.file):
175 raise HandlerMatchException
176 self.data = { 'request': request,
177 'link': self.link,
178 'file': '/' + '/'.join(request.environ['path']),
179 'content': file(self.file).read()
180 }
181
182 def __call__(self):
183 return getattr(self, self.request.method.title())()
184
185 def Get(self):
186 template = self.app.loader.load(self.template)
187 return Response(content_type='text/html',
188 body=template.generate(**self.data).render('html'))