Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Now has add-file on front page and delete buttons for files

  • Loading branch information...
commit f8455db2b9c030a08b3e00f798ce6b9894786c97 1 parent 8272fc0
@armandofox armandofox authored
Showing with 50 additions and 26 deletions.
  1. +14 −2 app.yaml
  2. +19 −24 bookware.py
  3. +17 −0 index.html
View
16 app.yaml
@@ -1,8 +1,20 @@
-application: bookware
+application: saasbook
version: 1
runtime: python
api_version: 1
handlers:
-- url: /.*
+
+- url: /files/?.*
+ script: bookware.py
+
+- url: /?
script: bookware.py
+
+- url: /admin/.*
+ script: bookware.py
+ login: admin
+
+- url: /main.css
+ static_files: main.css
+ upload: main.css
View
43 bookware.py
@@ -1,53 +1,48 @@
#!/usr/bin/env python
#
-import os
import urllib
+import logging
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
-
-class AddFileHandler(webapp.RequestHandler):
- def get(self):
- upload_url = blobstore.create_upload_url('/admin/upload')
- body = """
- <form action="%s" method="POST" enctype="multipart/form-data">
- Upload File:
- <input type="file" name="file"><br/>
- <input type="submit" name="submit" value="Upload">
- </form>
- """ % upload_url
- self.response.out.write(template.render('template.html', {'body': body}))
+from google.appengine.api.users import is_current_user_admin
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
blob_info = upload_files[0]
- body = """
- <h3>Uploaded file '%s' (%d bytes) as /files/%s</h3>
- <p>
- <a href="/admin/add">Add another</a> | <a href="/">List</a>
- </p>
- """ % (blob_info.filename, blob_info.size, blob_info.key())
- self.response.out.write(template.render('template.html', {'body': body}))
+ self.redirect('/')
+
+class DeleteHandler(webapp.RequestHandler):
+ def post(self, urlpath):
+ blobname = urllib.unquote(urlpath)
+ logging.debug("Deleting " + blobname)
+ blobstore.delete(blobname)
+ self.redirect('/')
class ListHandler(webapp.RequestHandler):
def get(self):
- self.response.out.write(template.render('index.html', {'blobs': blobstore.BlobInfo.all() }))
+ upload_url = blobstore.create_upload_url('/admin/upload')
+ params = {'admin': is_current_user_admin(),
+ 'upload_url': upload_url,
+ 'blobs': blobstore.BlobInfo.all() }
+ self.response.out.write(template.render('index.html', params))
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
resource = str(urllib.unquote(resource))
blob_info = blobstore.BlobInfo.get(resource)
- self.send_blob(blob_info)
+ self.send_blob(blob_info, save_as=True)
def main():
+ logging.getLogger().setLevel(logging.DEBUG)
application = webapp.WSGIApplication(
- [('/admin/add', AddFileHandler),
- ('/admin/upload', UploadHandler),
+ [('/admin/upload', UploadHandler),
+ ('/admin/delete/(.*)$', DeleteHandler),
('/files/?$', ListHandler),
('/?$', ListHandler),
('/files/([^/]+)?', ServeHandler),
View
17 index.html
@@ -6,6 +6,16 @@
<h1>Downloadable Files</h1>
+ {% if admin %}
+ <form action="{{ upload_url }}" method="POST" enctype="multipart/form-data">
+ <p>
+ Upload New File:
+ <input type="file" name="file">
+ <input type="submit" name="submit" value="Upload">
+ </p>
+ </form>
+ {% endif %}
+
<table>
<thead>
<th>File</th>
@@ -18,6 +28,13 @@
<td><a href="/files/{{ blob.key }}">{{ blob.filename }}</a></td>
<td>{{ blob.size }}</td>
<td>{{ blob.creation.ctime }}</td>
+ {% if admin %}
+ <td>
+ <form method="post" action="/admin/delete/{{ blob.key }}">
+ <input type="submit" name="delete" value="Delete"/>
+ </form>
+ </td>
+ {% endif %}
</tr>
{% endfor %}
</tbody>
Please sign in to comment.
Something went wrong with that request. Please try again.