From 5acf8565e9498f8f2413701416fde176b245dcca Mon Sep 17 00:00:00 2001 From: Macartur Sousa Date: Sun, 1 Nov 2015 12:37:35 -0200 Subject: [PATCH] Adding timestamp feature to plugins --- colab/plugins/models.py | 13 ++++++++++++- docs/source/plugindev.rst | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/colab/plugins/models.py b/colab/plugins/models.py index ffe61fa..5ec030e 100644 --- a/colab/plugins/models.py +++ b/colab/plugins/models.py @@ -3,8 +3,19 @@ from datetime import datetime class TimeStampPlugin(models.Model): ''' - Class used to store timestamp from plugins + Class used to store timestamps from plugins ''' id = models.IntegerField(primary_key= True) name = models.CharField(max_length=255,unique=True,null=False) timestamp = models.DateTimeField(default=datetime.min,blank=True) + + @classmethod + def update_timestamp(cls, class_name): + instance = TimeStampPlugin.objects.filter(name=class_name)[0] + instance.timestamp = datetime.now() + instance.save() + + @classmethod + def get_last_updated(cls, class_name): + instance = TimeStampPlugin.objects.get_or_create(name=class_name)[0] + return instance.timestamp diff --git a/docs/source/plugindev.rst b/docs/source/plugindev.rst index 079464d..a4eb2e8 100644 --- a/docs/source/plugindev.rst +++ b/docs/source/plugindev.rst @@ -162,3 +162,27 @@ created in this tutorial. Inside this directory, create a txt file for each model that can be queried. Each of this files must contain the model fields that will be search if no filter is applied. If there is any doubts to create these files, please check the `Guide to create a SearchIndexesFiles`_. + +Storing TimeStamp +--------------- +TimeStamp is a parameter to control the last time a model was updated, you should use it +when you want the data updated after a given time. To do that the colab's model (colab.plugins.models) have a +TimeStampPlugin class, used to store all last updates from timestamp from all plugins. + +Class Methods: + update_timestamp(cls,class_name): allow store a current datetime. + + get_last_updated_timestamp(cls,class_name): allow get a datetime with last timestamp stored from class_name. + +Example Usage: + +.. code-block:: python + from colab.plugins.models import TimeStampPlugin + + class TestPlugin(): + + def update_timestamp(self): + TimeStampPlugin.update_timestamp('TestPlugin') + + def get_last_updated_timestamp(self): + return TimeStampPlugin.get_last_updated_timestamp('TestPlugin') -- libgit2 0.21.2