~ubuntu-branches/ubuntu/precise/maas/precise-security

« back to all changes in this revision

Viewing changes to src/maasserver/models/timestampedmodel.py

Tags: 1.2+bzr1373+dfsg-0ubuntu1~12.04.4
* SECURITY UPDATE: failure to authenticate downloaded content (LP: #1039513)
  - debian/patches/CVE-2013-1058.patch: Authenticate downloaded files with
    GnuPG and MD5SUM files. Thanks to Julian Edwards.
  - CVE-2013-1058
* SECURITY UPDATE: configuration options may be loaded from current working
  directory (LP: #1158425)
  - debian/patches/CVE-2013-1057-1-2.patch: Do not load configuration
    options from the current working directory. Thanks to Julian Edwards.
  - CVE-2013-1057

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Model base class with creation/update timestamps."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = [
 
14
    'TimestampedModel',
 
15
    ]
 
16
 
 
17
 
 
18
from django.db import connection
 
19
from django.db.models import (
 
20
    DateTimeField,
 
21
    Model,
 
22
    )
 
23
from maasserver import DefaultMeta
 
24
 
 
25
 
 
26
def now():
 
27
    """Current database time (as per start of current transaction)."""
 
28
    cursor = connection.cursor()
 
29
    cursor.execute("select now()")
 
30
    return cursor.fetchone()[0]
 
31
 
 
32
 
 
33
class TimestampedModel(Model):
 
34
    """Abstract base model with creation/update timestamps.
 
35
 
 
36
    Timestamps are taken from the database transaction clock.
 
37
 
 
38
    :ivar created: Object's creation time.
 
39
    :ivar updated: Time of object's latest update.
 
40
    """
 
41
 
 
42
    class Meta(DefaultMeta):
 
43
        abstract = True
 
44
 
 
45
    created = DateTimeField(editable=False)
 
46
    updated = DateTimeField(editable=False)
 
47
 
 
48
    def save(self, *args, **kwargs):
 
49
        current_time = now()
 
50
        if self.id is None:
 
51
            self.created = current_time
 
52
        self.updated = current_time
 
53
        return super(TimestampedModel, self).save(*args, **kwargs)