Package Camelot :: Package camelot :: Package model :: Module fixture
[frames] | no frames]

Source Code for Module Camelot.camelot.model.fixture

  1  #  ============================================================================
 
  2  #
 
  3  #  Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved.
 
  4  #  www.conceptive.be / project-camelot@conceptive.be
 
  5  #
 
  6  #  This file is part of the Camelot Library.
 
  7  #
 
  8  #  This file may be used under the terms of the GNU General Public
 
  9  #  License version 2.0 as published by the Free Software Foundation
 
 10  #  and appearing in the file LICENSE.GPL included in the packaging of
 
 11  #  this file.  Please review the following information to ensure GNU
 
 12  #  General Public Licensing requirements will be met:
 
 13  #  http://www.trolltech.com/products/qt/opensource.html
 
 14  #
 
 15  #  If you are unsure which license is appropriate for your use, please
 
 16  #  review the following information:
 
 17  #  http://www.trolltech.com/products/qt/licensing.html or contact
 
 18  #  project-camelot@conceptive.be.
 
 19  #
 
 20  #  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
 21  #  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
 22  #
 
 23  #  For use of this library in commercial applications, please contact
 
 24  #  project-camelot@conceptive.be
 
 25  #
 
 26  #  ============================================================================
 
 27  from camelot.model import metadata 
 28  from elixir.entity import Entity 
 29  from elixir.options import using_options 
 30  from elixir.fields import Field 
 31  from sqlalchemy.types import Unicode, INT 
 32  """Classes to support the loading of required datasets into the 
 
 33  database""" 
 34  
 
 35  
 
 36  __metadata__ = metadata 
37 38 -class Fixture( Entity ):
39 """Keep track of static data loaded into the database""" 40 using_options( tablename = 'fixture' ) 41 model = Field( Unicode( 256 ), index = True, required = True ) 42 primary_key = Field( INT(), index = True, required = True ) 43 fixture_key = Field( Unicode( 256 ), index = True, required = True ) 44 fixture_class = Field( Unicode( 256 ), index = True, required = False ) 45 46 @classmethod
47 - def findFixtureReference( cls, entity, fixture_key, fixture_class = None ):
48 entity_name = unicode( entity.__name__ ) 49 return cls.query.filter_by( model = unicode( entity_name ), fixture_key = fixture_key, fixture_class = fixture_class ).first()
50 51 @classmethod
52 - def findFixture( cls, entity, fixture_key, fixture_class = None ):
53 """Find a registered fixture, return None if no fixture is found""" 54 reference = cls.findFixtureReference( entity, fixture_key, fixture_class ) 55 if reference: 56 return entity.get( reference.primary_key )
57 58 @classmethod
59 - def findFixtureKeyAndClass( cls, obj ):
60 """Find the fixture key and class of an object 61 @param obj: the object we are looking for 62 @return: (fixture_key, fixture_class) if the object is a registered fixture, (None, None) otherwise 63 """ 64 entity_name = unicode( obj.__class__.__name__ ) 65 fixture = cls.query.filter_by( model = entity_name, primary_key = obj.id ).first() 66 if fixture: 67 return ( fixture.fixture_key, fixture.fixture_class ) 68 else: 69 return ( None, None )
70 71 @classmethod
72 - def findFixtureKeysAnClasses( cls, entity ):
73 """Load all fixture keys of a certain entity in batch 74 :param entity: the model class for which the fixtures should be found 75 :return: a dictionary mapping the primary key of a on object of type entity to its (fixture key, fixture class) 76 """ 77 entity_name = unicode( entity.__name__ ) 78 return dict((fixture.primary_key, (fixture.fixture_key, fixture.fixture_class)) for fixture in cls.query.filter_by( model = entity_name ).all())
79 80 @classmethod
81 - def insertOrUpdateFixture( cls, entity, fixture_key, values, fixture_class = None ):
82 from sqlalchemy.orm.session import Session 83 obj = cls.findFixture( entity, fixture_key, fixture_class ) 84 store_fixture = False 85 if not obj: 86 obj = entity() 87 store_fixture = True 88 obj.from_dict( values ) 89 Session.object_session( obj ).flush( [obj] ) 90 if store_fixture: 91 # 92 # The fixture itself might have been deleted, but the reference might be intact, 93 # so this should be updated 94 # 95 reference = cls.findFixtureReference( entity, fixture_key, fixture_class ) 96 if not reference: 97 reference = cls( model = unicode( entity.__name__ ), primary_key = obj.id, fixture_key = fixture_key, fixture_class = fixture_class ) 98 else: 99 reference.primary_key = obj.id 100 Session.object_session( reference ).flush( [reference] ) 101 return obj
102
103 -class FixtureVersion( Entity ):
104 """Keep track of the version the fixtures have in the current database, the subversion 105 revision number is a good candidate to be used as a fixture version. 106 107 :return: an integer representing the current version, 0 if no version found 108 """ 109 using_options( tablename = 'fixture_version' ) 110 fixture_version = Field( INT(), index = True, required = True, default=0 ) 111 fixture_class = Field( Unicode( 256 ), index = True, required = False, unique=True ) 112 113 @classmethod
114 - def get_current_version( cls, fixture_class = None ):
115 """Get the current version of the fixtures in the database for a certain 116 fixture class. 117 118 :param fixture_class: the fixture class for which to get the version 119 """ 120 obj = cls.query.filter_by( fixture_class = fixture_class ).first() 121 if obj: 122 return obj.fixture_version 123 return 0
124 125 @classmethod
126 - def set_current_version( cls, fixture_class = None, fixture_version = 0 ):
127 """Set the current version of the fixtures in the database for a certain 128 fixture class. 129 130 :param fixture_class: the fixture class for which to get the version 131 :param fixture_version: the version number to which to set the fixture version 132 """ 133 from sqlalchemy.orm.session import Session 134 obj = cls.query.filter_by( fixture_class = fixture_class ).first() 135 if not obj: 136 obj = FixtureVersion( fixture_class = fixture_class ) 137 obj.fixture_version = fixture_version 138 Session.object_session( obj ).flush( [obj] )
139