Commit 5acf8565e9498f8f2413701416fde176b245dcca
1 parent
a979a15f
Exists in
master
and in
4 other branches
Adding timestamp feature to plugins
Signed-off-by: Macartur Sousa <macartur.sc@gmail.com> Signed-off-by: Simião Carvalho <simiaosimis@gmail.com>
Showing
2 changed files
with
36 additions
and
1 deletions
Show diff stats
colab/plugins/models.py
@@ -3,8 +3,19 @@ from datetime import datetime | @@ -3,8 +3,19 @@ from datetime import datetime | ||
3 | 3 | ||
4 | class TimeStampPlugin(models.Model): | 4 | class TimeStampPlugin(models.Model): |
5 | ''' | 5 | ''' |
6 | - Class used to store timestamp from plugins | 6 | + Class used to store timestamps from plugins |
7 | ''' | 7 | ''' |
8 | id = models.IntegerField(primary_key= True) | 8 | id = models.IntegerField(primary_key= True) |
9 | name = models.CharField(max_length=255,unique=True,null=False) | 9 | name = models.CharField(max_length=255,unique=True,null=False) |
10 | timestamp = models.DateTimeField(default=datetime.min,blank=True) | 10 | timestamp = models.DateTimeField(default=datetime.min,blank=True) |
11 | + | ||
12 | + @classmethod | ||
13 | + def update_timestamp(cls, class_name): | ||
14 | + instance = TimeStampPlugin.objects.filter(name=class_name)[0] | ||
15 | + instance.timestamp = datetime.now() | ||
16 | + instance.save() | ||
17 | + | ||
18 | + @classmethod | ||
19 | + def get_last_updated(cls, class_name): | ||
20 | + instance = TimeStampPlugin.objects.get_or_create(name=class_name)[0] | ||
21 | + return instance.timestamp |
docs/source/plugindev.rst
@@ -162,3 +162,27 @@ created in this tutorial. Inside this directory, create a txt file for each | @@ -162,3 +162,27 @@ created in this tutorial. Inside this directory, create a txt file for each | ||
162 | model that can be queried. Each of this files must contain the model fields that | 162 | model that can be queried. Each of this files must contain the model fields that |
163 | will be search if no filter is applied. If there is any doubts to create these | 163 | will be search if no filter is applied. If there is any doubts to create these |
164 | files, please check the `Guide to create a SearchIndexesFiles`_. | 164 | files, please check the `Guide to create a SearchIndexesFiles`_. |
165 | + | ||
166 | +Storing TimeStamp | ||
167 | +--------------- | ||
168 | +TimeStamp is a parameter to control the last time a model was updated, you should use it | ||
169 | +when you want the data updated after a given time. To do that the colab's model (colab.plugins.models) have a | ||
170 | +TimeStampPlugin class, used to store all last updates from timestamp from all plugins. | ||
171 | + | ||
172 | +Class Methods: | ||
173 | + update_timestamp(cls,class_name): allow store a current datetime. | ||
174 | + | ||
175 | + get_last_updated_timestamp(cls,class_name): allow get a datetime with last timestamp stored from class_name. | ||
176 | + | ||
177 | +Example Usage: | ||
178 | + | ||
179 | +.. code-block:: python | ||
180 | + from colab.plugins.models import TimeStampPlugin | ||
181 | + | ||
182 | + class TestPlugin(): | ||
183 | + | ||
184 | + def update_timestamp(self): | ||
185 | + TimeStampPlugin.update_timestamp('TestPlugin') | ||
186 | + | ||
187 | + def get_last_updated_timestamp(self): | ||
188 | + return TimeStampPlugin.get_last_updated_timestamp('TestPlugin') |