How to use a custom __init__ of an app engine Python model class properly? -


I am trying to implement a delayed blog post removal plan. So an annoying are you sure? , you get a 2 minute time limit for canceling the deletion.

I have to track whether the DB will be removed when With the model class ( DeleteQueueItem ), as I did not find a way to remove the work from the queue and I suspect what is there.

should create an DeleteQueueItem unit to automatically set an delete_when property and add a task to the queue I use the relative path of the blog post in their < Em> key_name and also want to use it as key_name as a custom init :

 The  class inspires me for DeleteQueueItem (db.Model): "" The model to keep track of the items that can be removed through the work queue. Ega. KEY_NAME delete_when = db.DateTimeProperty is controlled as "URL to post" "#" URL path () DEF __init __ (self, ** kwargs): delay = 120 # seconds T = datetime.timedelta (seconds = Delay) Deadline = datetime.datetime.now () - TKEY_NAME = kwargs.get ('KEY_NAME') db.Model .__ init __ (self, ** kwargs) self.delete_when = deadline taskqueue.add (url) = '/ Admin / work / delete_page', countdown = delay, parameters = {'path': key_name})  

This works, until I delete the unit Does not attempt:

  fetched_item = Mod .DeleteQueueItem.get_by_key_name (path)  

with it failed:

  Writing error: __init __ () takes exactly 1 non-keyword logic (2 )  

What am I doing? Normally, you should not try the init method of model classes and should not override it / p>

A better method is to use the factory's method, which you call instead of the constructor.

In addition, you probably want to add jobs when you write units instead of creation time. If you do not do this, then you end up with a race situation: The task can be executed before the new unit is stored in the datastore!

Here's the recommended restructuring:

  class DeleteQueueItem (db.Model): "# URL path to blog post" model item through that work queue Keep track of will be removed. " KEY_NAME delete_when = db.DateTimeProperty is controlled () @classmethod DEF new (CLS, key_name): delay = 120 # secondsT = datetime timemindleta (second = delay) deadline = datetime.datetime.now () - T return class (key_name = key_name, delete_when = deadline) def (self, ** kwargs): def _tx (): taskqueue.add (url = '/ admin / task / delete_page', countdown = delay, parameters = { 'Path': key_name}, transactional = true) super return super (delete queitime, self) .put (** kwargs) if not self.is_saved (): return db.run_in_transaction (_tx) Other: retu come N Super (Htanakyuiitm Self) Kput (** Cwarj)  

Comments