~ubuntu-branches/ubuntu/vivid/ironic/vivid-updates

« back to all changes in this revision

Viewing changes to ironic/openstack/common/db/sqlalchemy/models.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-03-06 13:23:35 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20140306132335-5b49ji56jffxvtn4
Tags: 2014.1~b3-0ubuntu1
* New upstream release:
  - debian/patches/fix-requirements.patch: Dropped no longer needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
1
# Copyright (c) 2011 X.commerce, a business unit of eBay Inc.
4
2
# Copyright 2010 United States Government as represented by the
5
3
# Administrator of the National Aeronautics and Space Administration.
41
39
        if not session:
42
40
            session = sa.get_session()
43
41
        # NOTE(boris-42): This part of code should be look like:
44
 
        #                       sesssion.add(self)
 
42
        #                       session.add(self)
45
43
        #                       session.flush()
46
44
        #                 But there is a bug in sqlalchemy and eventlet that
47
45
        #                 raises NoneType exception if there is no running
48
46
        #                 transaction and rollback is called. As long as
49
47
        #                 sqlalchemy has this bug we have to create transaction
50
 
        #                 explicity.
 
48
        #                 explicitly.
51
49
        with session.begin(subtransactions=True):
52
50
            session.add(self)
53
51
            session.flush()
61
59
    def get(self, key, default=None):
62
60
        return getattr(self, key, default)
63
61
 
64
 
    def _get_extra_keys(self):
 
62
    @property
 
63
    def _extra_keys(self):
 
64
        """Specifies custom fields
 
65
 
 
66
        Subclasses can override this property to return a list
 
67
        of custom fields that should be included in their dict
 
68
        representation.
 
69
 
 
70
        For reference check tests/db/sqlalchemy/test_models.py
 
71
        """
65
72
        return []
66
73
 
67
74
    def __iter__(self):
69
76
        # NOTE(russellb): Allow models to specify other keys that can be looked
70
77
        # up, beyond the actual db columns.  An example would be the 'name'
71
78
        # property for an Instance.
72
 
        columns.extend(self._get_extra_keys())
 
79
        columns.extend(self._extra_keys)
73
80
        self._i = iter(columns)
74
81
        return self
75
82
 
91
98
        joined = dict([(k, v) for k, v in six.iteritems(self.__dict__)
92
99
                      if not k[0] == '_'])
93
100
        local.update(joined)
94
 
        return local.iteritems()
 
101
        return six.iteritems(local)
95
102
 
96
103
 
97
104
class TimestampMixin(object):
98
 
    created_at = Column(DateTime, default=timeutils.utcnow)
99
 
    updated_at = Column(DateTime, onupdate=timeutils.utcnow)
 
105
    created_at = Column(DateTime, default=lambda: timeutils.utcnow())
 
106
    updated_at = Column(DateTime, onupdate=lambda: timeutils.utcnow())
100
107
 
101
108
 
102
109
class SoftDeleteMixin(object):