Book Image

Python for Google App Engine

By : Massimiliano Pippi
Book Image

Python for Google App Engine

By: Massimiliano Pippi

Overview of this book

Table of Contents (15 chapters)
Python for Google App Engine
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Loading and saving data


Now that we know how to connect to a Cloud SQL instance from our App Engine application, it's time to learn how to write and read data from the database. We already created a table called ops, and we will use it to store information about user operations. We will log the following events:

  • A user has created a note

  • A user has added a file

  • A user has performed a shrink operation

We have to assign a text code to each of the operation types we want to log. To do so, we can use a simple Python class that works as an enumeration. In the utils.py module, we add the following code:

class OpTypes(object):
    NOTE_CREATED = 'NCREATED'
    FILE_ADDED = 'FADDED'
    SHRINK_PERFORMED = 'SHRINKED'

We will see how to use it in a moment. We now provide a log_operation() method in the utils.py module that we will use to log operations in the Cloud SQL database. We will call this function within the Notes code passing along the user who actually performed the operation, the appropriate...