#! / usr / bin / env python # encoding: utf-8 "" " model.py $ Id: model.py 4 2010-01-25 12: 14: 48Z sigizmund $ Created by Roman Kirillov on 2010-01-25. "" " import datetime from google.appengine.ext import db from google.appengine.api import users class Visitor (db.Model): '' ' Simple object which uses Google DataStore persistence model '' ' '' 'UserProperty encapsulates information about a Google's user account available to application. It is set to use current user as default user automatically whever new object is created '' ' user = db.UserProperty (required = True, auto_current_user_add = True) '' 'DateTimeProperty uses standard date datetime.datetime and it is set to auto-update to now () whenever record is saved '' ' lastVisit = db.DateTimeProperty (auto_now = True) '' 'Very simple integer property with default value of 0' '' hits = db.IntegerProperty (default = 0)
Visitor
class inherits from the standard GAE class google.appengine.ext.db.Model
, which is an interface to Datastore - all classes that want to be persistent using this technology should extend this class. Our Visitor
class has three data fields: user
, lastVisit
and hits
. Consider them in detail, starting with the last.db.IntegerProperty hits
encapsulates the usual integer data type. By default, all new objects of this class are assigned the value 0. Everything is simple:u = Visitor () u.hits = 1 # ... hitsCount = u.hitsWorking with such fields is absolutely transparent - as a rule, you use them in the same way as objects of the basic, integer type.
lastVisit
is like the previous one, db.DateTimeProperty
encapsulates the built-in data type, only this time it is datetime.datetime
. By and large, there is no difference - it works in the same way with a field as with the specified data type. The field is configured so that you do not need to manually update it - every time the record is saved, the field value is set to now()
(in this case, this is reasonable).db.UserProperty user
is the most interesting one used here. It encapsulates the GAE class of users.User - no need to store the user's email, no need to store a unique ID in case the email changes - it's all there. And naturally, this is exactly the class whose object you get by making users.get_current_user()
. The settings of the field indicate that every time an object is created, the value of this field will be set according to the current user.hits
) and save it in the Datastore:
u = Visitor () u.hits = 17 u.put ()
Visitor
. About how to search for objects by the value of a specific field, I will tell a little bit later - in the next part of the article. The sources of our test application are gradually gaining their haven here and will be laid out as they are written.
Source: https://habr.com/ru/post/81920/