Practice 7 - Google App Engine continued
In this practice session we will continue with Google App engine and see how we can store arbitrary data in Data store.
References
Referred documents and web sites contain supportive information for the practice.
Exercise 7.1. Creating a second Google App page
- Lets create a new sub-page under your Google App engine application
- NB! Make sure you DO NOT overwrite your current main page as this would remove your previous lab submission when you deploy your application later!
- Create a new python file under your Google app engine project folder named
second.py
- Inside the new python file, create a new class for displaying the page content
- For example:
class SecondMainPage(webapp2.RequestHandler):
- Also add a
get(self)
method and display some text on the web page
- For example:
- Import the webapp2 module:
import webapp2
- Inside the new python file, create a new class for displaying the page content
- In the original python file, Create a new entry under
application = webapp2.WSGIApplication
for the newly created page- It should link to: /secondexercise
- In the original python file, import the classes from the newly created file:
from second import *
- Deploy the application and check that everything is working correctly.
- From now on, continue adding new content to the
second.py
file while only changing the original file to modify theapplication = webapp2.WSGIApplication
entries.
Exercise 7.2. Using the Google NDB Datastore service
- Create a new NDB object class Message
- Check https://cloud.google.com/appengine/docs/standard/python/ndb/creating-entity-models to see how it is done.
- It must have the following fields
- message_from - name of the sender, String type
- message_text - message content, String type
- message_time - time of the message, DateTime format
- DateTime should have an option:
auto_now=True
- You can check the supported data types from here: https://cloud.google.com/appengine/docs/standard/python/ndb/entity-property-reference
- DateTime should have an option:
- Create a new form for entering messages on the /secondexercise page
<form action="/message" name ="myform" method="post">
From: <input type="text" name="from" /> <br />
Message: <input type="text" name="text" />
<input type="submit" value="Submit" name="submit">
</form>
- Create a class HandleMessage for processing the message form and link it to sub-page /message
- Add a
def post(self):
method to HandleMessage class- You can access the entered form data from inside the post method like this:
m_from = self.request.get('from')
- Create a new Message object using the entered data and enter it to database:
message = Message(message_from=m_from, message_text=m_text)
message.put()
- output a confirmation about the entered message and create a HTML link back to the http://<your_google_app_id>.appspot.com/secondexercise page
- You can access the entered form data from inside the post method like this:
Exercise 7.3. Displaying data from NDB Datastore
- Modify the /secondexercise page to display the latest 10 Messages from NDB
- Check https://developers.google.com/appengine/docs/python/ndb/queries on how to query NDB.
- Create a query for all data from Message's
- Sort by Message.message_time in Descending order
- Fetch 10 entries
- create a cycle over all the results and print them out one at a time
- Use cgi to escape any unneeded html characters
import cgi
- For example:
self.response.out.write(cgi.escape(message.message_text))
- Use cgi to escape any unneeded html characters
Exercise 7.4. Add file uploading to the Messages
- Check the previous exercise and your solution on how file uploading worked, but you will have to:
- Modify the Message class to also include an entry for a BlobStore key
- Modify the Message form to also have a optional file upload form entry
- Don't forget to modify
upload_url = blobstore.create_upload_url('...')
to point to a correct subpage of your site. It should link to the page where your Message class is handled.
- Don't forget to modify
- Modify the HandleMessage class to also handle uploaded file
- You will also have to change the type of the class from
webapp2.RequestHandler
toblobstore_handlers.BlobstoreUploadHandler
- On the /secondexercise, when displaying the messages also print out a link to the uploaded file (just like you did last time)
- You will also have to change the type of the class from
- For additional information how to query data from blob store, take a look at:
Exercise 7.5. Add user identification and information to the Messages
- Ask for user authentication.
- Only allow users to send messages if they are authenticated with Google accounts
- Remove the user submited "From" field and replace it username or full name from google accounts
- Display the name of the user in the list of previous messages.
Exercise 7.6. Blocking unsafe files from being uploaded
- Modify the File uploading to only allow uploading certain safe types of files (.txt, .jpeg, .gif, .doc, .odt, .pdf, ...)
- HINT when using blobstore upload directly you can not actually restrict uploading files, but you can check the name of the file that was uploaded and if it does not match your list, then delete the uploaded blob!
- What file types to allow is up to you, but try to have a extensive list of commonly accepted safe files.
Deliverables
- Deploy the latest version of the application in App Engine
- Verify that your previous lab assignment is also still working in appspot.com
- Provide the full web link to your GoogleAppEngine second exercise page
- Upload the source code of your full Google App.
Sellele ülesandele ei saa enam lahendusi esitada.