Source

orgtool / orgtool / ext / tracking / schema.py

Full commit
# -*- coding: utf-8 -*-

import datetime
from doqu import Document
from doqu.ext.fields import Field as f


class TrackedDocument(Document):
    """
    A document which automatically tracks date and time of its creation and of
    the last update. Of course it only tracks the changes made to records by
    saving them via TrackedDocument instances or its subclasses' instances.
    """
    date_time_created = f(datetime.datetime, essential=True,
                          default=datetime.datetime.utcnow)
    date_time_updated = f(datetime.datetime, essential=True,
                          default=datetime.datetime.utcnow)

    def save(self, *args, **kwargs):
        now = datetime.datetime.utcnow()
        self['date_time_updated'] = now
        # FIXME: Doqu's field defaults seem to be broken so we duplicate them
        # for date_time_created:
        self['date_time_created'] = self['date_time_created'] or now
        return super(TrackedDocument, self).save(*args, **kwargs)