-1

I found code for a guestbook storing text in a datastore. I've been looking all morning to find how would i modify my code to upload a file instead of reading from the textfield. and displaying the file details after displaying it. I would appreciate any help? or maybe there's an answer already out there i just haven't found it. Here's my code so far:

import cgi
import datetime
import urllib
import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.api import users
import webapp2


class Greeting(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)


def upload_key(upload_name=None):
  return db.Key.from_path('Upload', upload_name or 'default_upload')


class MainPage(webapp2.RequestHandler):
  def get(self):
    self.response.out.write('<html><body>')
    upload_name=self.request.get('upload_name')

    greetings = db.GqlQuery("SELECT * "
                            "FROM Greeting "
                            "WHERE ANCESTOR IS :1 "
                            "ORDER BY date DESC LIMIT 10",
                            upload_key(upload_name))

    for greeting in greetings:
      if greeting.author:
        self.response.out.write(
            '<b>%s</b> wrote:' % greeting.author.nickname())
      else:
        self.response.out.write('An anonymous person wrote:')
      self.response.out.write('<blockquote>%s</blockquote>' %
                              cgi.escape(greeting.content))

    self.response.out.write("""
          <form action="/sign?%s" method="post">
            <div><textarea name="content" rows="3" cols="60"></textarea></div>
            <div><input type="submit" value="Upload a File"></div>
          </form>
          <hr>
          <form>Name: <input value="%s" name="upload_name">
          <input type="submit" value="switch user"></form>
        </body>
      </html>""" % (urllib.urlencode({'upload_name': upload_name}),
                          cgi.escape(upload_name)))


class Upload(webapp2.RequestHandler):
  def post(self):

    upload_name = self.request.get('upload_name')
    greeting = Greeting(parent=upload_key(upload_name))

    if users.get_current_user():
      greeting.author = users.get_current_user()

    greeting.content = self.request.get('content')
    greeting.put()
    self.redirect('/?' + urllib.urlencode({'upload_name': upload_name}))


APP = webapp2.WSGIApplication([
  ('/', MainPage),
  ('/sign', Upload)
], debug=True)


def main():
  APP.RUN()


if __name__ == '__main__':
  main()
K_m_a
  • 11
  • 5

1 Answers1

0

There's two basic approaches. The traditional approach, and the approach you'll find the most samples for, is the Blobstore API. The new approach is Google Cloud Storage. The advantages of the Blobstore is that there are more existing samples, but the advantage of GCS is that the same code can work outside the context of App Engine.

APPROACH 1 - BLOBSTORE API - EASIER *

Here are the official Blobstore docs with samples.

Here's a similar Stack Overflow question.

APPROACH - GOOGLE CLOUD STORAGE API - BETTER

For Google Cloud Storage, the official client library is gcloud-python. Since this is not part of the App Engine SDK, you will generally "vendor" it (include it directly in your project) before you deploy the App Engine app using pip -t flag, and modififying an appengine_config.py file. See the instructions for that in "Installing a library". The short version of the story is that you do

mkdir lib
pip install gcloud-python -t lib

then add an appengine_config.py with the follow lines:

from google.appengine.ext import vendor

# Third-party libraries are stored in "lib", vendoring will make
# sure that they are importable by the application.
vendor.add('lib')

Finally, we walk through using this API in a Python app in this tutorial

Community
  • 1
  • 1
Bill Prin
  • 2,498
  • 1
  • 20
  • 27