bessgoogleapp-step6.py

We now add the fonctionality of the datastore: A new type of entity (Dataset), with three key value pairs. We use this entity to store dataset in the datastore (dataset.put()).

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

def generate_menu(out):
    out.write("<p>You can go to:</p>")
    for page in ["upload","visualise"]:
        out.write("<p><a href='"+page+"'>"+page+"</a></p>")

class Dataset(db.Model):
  descrip = db.StringProperty(multiline=True)
  content = db.StringListProperty()
  date    = db.DateTimeProperty(auto_now_add=True)


class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write('<h1>Welcome to the BESS cloud tutorial!</h1>')
        out = self.response.out
        generate_menu(out)

class UploadPage(webapp.RequestHandler):
    def get(self):
        out = self.response.out
        out.write('<h1>Welcome to the BESS upload page!</h1>')
        out.write("""<form action='/completeupload' method='post'>
                     <div>Name of dataset:<br/>
                          <input type='text' name='dataset_name' maxlength='120' size='15'></div>
                     <div>Description:<br/>
                          <textarea name='dataset_descrip' rows='3' cols='60'></textarea></div>
                     <div>Content of dataset:<br/>
                          <textarea name='dataset_content' rows='10' cols='60'></textarea></div>
                     <div><input type='submit' value='Upload Dataset'></div>
                   </form>""")

        generate_menu(out)

class CompleteUploadPage(webapp.RequestHandler):
    def post(self):

        dataset_name = self.request.get('dataset_name')
        dataset_cont = self.request.get('dataset_content').split()
        
        dataset = Dataset(key_name=dataset_name)
        dataset.content = dataset_cont
        dataset.descrip = self.request.get('dataset_descrip')

        dataset.put()

        out = self.response.out
        out.write('<h1>Upload completed</h1>')
        out.write("<p>The dataset <b>"+dataset_name + "</b> was uplodaded successfully.</p>")

        generate_menu(out)

class VisualisePage(webapp.RequestHandler):
    def get(self):
        self.response.out.write('<h1>Welcome to the BESS visualise page!</h1>')

application = webapp.WSGIApplication(
                                     [('/'      , MainPage  ),
                                      ('/upload', UploadPage),
                                      ('/visualise' , VisualisePage ),
                                      ('/completeupload' , CompleteUploadPage )],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
back

 
Last generated on 17 Sep 2015       francois.taiani@irisa.fr     Valid HTML 4.0!