From 0d9d590e425bed027b19b3f9a33d6d22069b3d4c Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Tue, 28 Jul 2020 16:03:11 -0700 Subject: [PATCH 1/2] Add last accessed time to Xml model, and update it when getting from the db --- appengine/storage.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/appengine/storage.py b/appengine/storage.py index a3d351c32..478d1f25d 100644 --- a/appengine/storage.py +++ b/appengine/storage.py @@ -23,6 +23,7 @@ __author__ = "q.neutron@gmail.com (Quynh Neutron)" import cgi import hashlib from random import randint +from datetime import timezone from google.cloud import ndb @@ -30,6 +31,7 @@ class Xml(ndb.Model): # A row in the database. xml_hash = ndb.IntegerProperty() xml_content = ndb.TextProperty() + last_accessed = ndb.DateTimeProperty(auto_now = true) def keyGen(): @@ -72,10 +74,19 @@ def keyToXml(key_provided): client = ndb.Client() with client.context(): result = Xml.get_by_id(key_provided) + if not result: xml = "" else: xml = result.xml_content + # Update row to include last accessed time--because last_accessed is set to + # auto_now, any write updates the time. + with client.context(): + row = Xml(id = key_provided, + xml_hash = result.xml_hash, + xml_content = result.xml_content) + row.put(); + return xml From afdd558f0e252ec905db7b4a54f8c3b716fb62fe Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Fri, 31 Jul 2020 14:57:52 -0700 Subject: [PATCH 2/2] Simplify --- appengine/storage.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/appengine/storage.py b/appengine/storage.py index 478d1f25d..e90375bf1 100644 --- a/appengine/storage.py +++ b/appengine/storage.py @@ -23,7 +23,6 @@ __author__ = "q.neutron@gmail.com (Quynh Neutron)" import cgi import hashlib from random import randint -from datetime import timezone from google.cloud import ndb @@ -31,8 +30,7 @@ class Xml(ndb.Model): # A row in the database. xml_hash = ndb.IntegerProperty() xml_content = ndb.TextProperty() - last_accessed = ndb.DateTimeProperty(auto_now = true) - + last_accessed = ndb.DateTimeProperty(auto_now=True) def keyGen(): # Generate a random string of length KEY_LEN. @@ -74,19 +72,14 @@ def keyToXml(key_provided): client = ndb.Client() with client.context(): result = Xml.get_by_id(key_provided) - if not result: xml = "" else: - xml = result.xml_content - # Update row to include last accessed time--because last_accessed is set to - # auto_now, any write updates the time. + # Put it back into the datastore immediately, which updates the last + # accessed time. with client.context(): - row = Xml(id = key_provided, - xml_hash = result.xml_hash, - xml_content = result.xml_content) - row.put(); - + result.put() + xml = result.xml_content return xml