Commit 8b909d60131a22427554b269f959334b1897391d

Authored by Alexandre A. Barbosa
2 parents 9c2a2265 0696cbe8

Merge pull request #93 from colab/timestamp_plugins

Timestamp plugins
colab/plugins/migrations/0001_initial.py 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +# -*- coding: utf-8 -*-
  2 +from __future__ import unicode_literals
  3 +
  4 +from django.db import models, migrations
  5 +import datetime
  6 +
  7 +
  8 +class Migration(migrations.Migration):
  9 +
  10 + dependencies = [
  11 + ]
  12 +
  13 + operations = [
  14 + migrations.CreateModel(
  15 + name='TimeStampPlugin',
  16 + fields=[
  17 + ('id', models.IntegerField(serialize=False, primary_key=True)),
  18 + ('name', models.CharField(unique=True, max_length=255)),
  19 + ('timestamp', models.DateTimeField(default=datetime.datetime(1, 1, 1, 0, 0), blank=True)),
  20 + ],
  21 + options={
  22 + },
  23 + bases=(models.Model,),
  24 + ),
  25 + ]
... ...
colab/plugins/migrations/__init__.py 0 → 100644
colab/plugins/models.py 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +from django.db import models
  2 +from django.utils import timezone
  3 +
  4 +
  5 +class TimeStampPlugin(models.Model):
  6 + '''
  7 + Class used to store timestamps from plugins
  8 + '''
  9 + id = models.IntegerField(primary_key=True)
  10 + name = models.CharField(max_length=255, unique=True, null=False)
  11 + timestamp = models.DateTimeField(default=timezone.datetime.min, blank=True)
  12 +
  13 + @classmethod
  14 + def update_timestamp(cls, class_name, **kwargs):
  15 + instance = TimeStampPlugin.objects.get_or_create(name=class_name)[0]
  16 + last_updated = kwargs.get('last_updated', '')
  17 +
  18 + if last_updated:
  19 + format = "%Y/%m/%d %H:%M:%S"
  20 + instance.timestamp = timezone.datetime.strptime(last_updated,
  21 + format)
  22 + else:
  23 + instance.timestamp = timezone.datetime.now()
  24 + instance.save()
  25 +
  26 + @classmethod
  27 + def get_last_updated(cls, class_name):
  28 + instance = TimeStampPlugin.objects.get_or_create(name=class_name)[0]
  29 + return instance.timestamp
... ...
colab/plugins/tests/__init__.py 0 → 100644
colab/plugins/tests/test_timestamp.py 0 → 100644
... ... @@ -0,0 +1,32 @@
  1 +import mock
  2 +
  3 +from django.test import TestCase
  4 +from django.utils import timezone
  5 +from colab.plugins.models import TimeStampPlugin
  6 +
  7 +
  8 +class UserTest(TestCase):
  9 +
  10 + def test_update_timestamp_without_last_updated(self):
  11 + result = timezone.datetime(2009, 1, 1).replace(tzinfo=timezone.utc)
  12 + with mock.patch.object(timezone, 'datetime',
  13 + mock.Mock(wraps=timezone.datetime)) as mock_:
  14 + mock_.now.return_value = result
  15 + TimeStampPlugin.get_last_updated('TestPluginUpdate')
  16 + TimeStampPlugin.update_timestamp('TestPluginUpdate')
  17 + timestamp = TimeStampPlugin.get_last_updated('TestPluginUpdate')
  18 + self.assertEquals(result, timestamp)
  19 +
  20 + def test_update_timestamp_with_last_updated(self):
  21 + TimeStampPlugin.get_last_updated('TestPluginUpdate')
  22 + date = '2015/12/23 00:00:00'
  23 + TimeStampPlugin.update_timestamp('TestPluginUpdate', last_updated=date)
  24 +
  25 + timestamp = TimeStampPlugin.get_last_updated('TestPluginUpdate')
  26 + result = timezone.datetime.strptime(date, "%Y/%m/%d %H:%M:%S")\
  27 + .replace(tzinfo=timezone.utc)
  28 + self.assertEquals(timestamp, result)
  29 +
  30 + def test_first_get_last_update(self):
  31 + timestamp = TimeStampPlugin.get_last_updated('Test')
  32 + self.assertEqual(timezone.datetime.min, timestamp)
... ...
docs/source/plugindev.rst
... ... @@ -162,3 +162,27 @@ created in this tutorial. Inside this directory, create a txt file for each
162 162 model that can be queried. Each of this files must contain the model fields that
163 163 will be search if no filter is applied. If there is any doubts to create these
164 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')
... ...