Changeset 513
- Timestamp:
- 11/09/09 21:10:55 (4 years ago)
- Location:
- elixir/trunk
- Files:
-
- 3 modified
-
elixir/entity.py (modified) (5 diffs)
-
elixir/options.py (modified) (1 diff)
-
tests/test_abstract.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
elixir/trunk/elixir/entity.py
r512 r513 48 48 def __init__(self, entity): 49 49 self.entity = entity 50 # entity.__module__ is not always reliable (eg in mod_python)51 self.module = sys.modules.get(entity.__module__)52 53 self.builders = []54 55 50 self.parent = None 56 #XXX: use entity.__subclasses__ ?57 self.children = []58 51 59 52 bases = [] … … 73 66 bases.append(base) 74 67 self.bases = bases 75 76 if not is_entity(entity): 68 if not is_entity(entity) or is_abstract_entity(entity): 77 69 return 70 71 # entity.__module__ is not always reliable (eg in mod_python) 72 self.module = sys.modules.get(entity.__module__) 73 74 self.builders = [] 75 76 #XXX: use entity.__subclasses__ ? 77 self.children = [] 78 78 79 79 # used for multi-table inheritance … … 98 98 99 99 # base class(es) options_defaults 100 base_defaults = {} 101 for base in self.bases: 102 base_defaults.update(getattr(base, 'options_defaults', {})) 100 options_defaults = self.options_defaults() 103 101 104 102 complete_defaults = options.options_defaults.copy() … … 111 109 # set default value for other options 112 110 for key in options.valid_options: 113 value = base_defaults.get(key, complete_defaults[key])111 value = options_defaults.get(key, complete_defaults[key]) 114 112 if isinstance(value, dict): 115 113 value = value.copy() … … 121 119 if hasattr(self.module, attr): 122 120 setattr(self, key, getattr(self.module, attr)) 121 122 def options_defaults(self): 123 base_defaults = {} 124 for base in self.bases: 125 base_defaults.update(base._descriptor.options_defaults()) 126 base_defaults.update(getattr(self.entity, 'options_defaults', {})) 127 return base_defaults 123 128 124 129 def setup_options(self): -
elixir/trunk/elixir/options.py
r511 r513 242 242 raise Exception("'%s' is not a valid option for Elixir entities." 243 243 % kwarg) 244 if not hasattr(entity, 'options_defaults'): 244 245 # We use __dict__ instead of hasattr to not check its presence within the 246 # parent, and thus update the parent dict instead of creating a local dict. 247 if not entity.__dict__.get('options_defaults'): 245 248 entity.options_defaults = {} 246 249 entity.options_defaults.update(kwargs) -
elixir/trunk/tests/test_abstract.py
r512 r513 33 33 class AbstractPerson(Entity): 34 34 using_options(abstract=True) 35 using_options_defaults(tablename=camel_to_underscore) 35 36 36 37 firstname = Field(String(30)) 37 38 lastname = Field(String(30)) 38 39 39 class AbstractEmploye d(AbstractPerson):40 class AbstractEmployee(AbstractPerson): 40 41 using_options(abstract=True) 42 using_options_defaults(identity=camel_to_underscore) 41 43 42 44 corporation = Field(String(30)) 43 45 44 class Employed(AbstractEmployed):46 class ConcreteEmployee(AbstractEmployee): 45 47 service = Field(String(30)) 46 48 47 class C itizen(AbstractPerson):49 class ConcreteCitizen(AbstractPerson): 48 50 country = Field(String(30)) 49 51 … … 51 53 52 54 assert not hasattr(AbstractPerson, 'table') 53 assert not hasattr(AbstractEmployed, 'table') 54 assert hasattr(Employed, 'table') 55 assert hasattr(Citizen, 'table') 55 assert not hasattr(AbstractEmployee, 'table') 56 assert hasattr(ConcreteEmployee, 'table') 57 assert hasattr(ConcreteCitizen, 'table') 58 assert ConcreteEmployee.table.name == 'concrete_employee' 59 assert ConcreteCitizen.table.name == 'concrete_citizen' 56 60 57 assert 'firstname' in Employed.table.columns58 assert 'lastname' in Employed.table.columns59 assert 'corporation' in Employed.table.columns60 assert 'service' in Employed.table.columns61 assert 'firstname' in ConcreteEmployee.table.columns 62 assert 'lastname' in ConcreteEmployee.table.columns 63 assert 'corporation' in ConcreteEmployee.table.columns 64 assert 'service' in ConcreteEmployee.table.columns 61 65 62 assert 'firstname' in Citizen.table.columns 63 assert 'lastname' in Citizen.table.columns 64 assert 'corporation' not in Citizen.table.columns 65 assert 'country' in Citizen.table.columns 66 assert 'firstname' in ConcreteCitizen.table.columns 67 assert 'lastname' in ConcreteCitizen.table.columns 68 assert 'corporation' not in ConcreteCitizen.table.columns 69 assert 'country' in ConcreteCitizen.table.columns 70 # test that the options_defaults do not leak into the parent base 71 assert ConcreteCitizen._descriptor.identity == 'concretecitizen' 66 72 67 73 def test_simple_relation(self): … … 166 172 using_options(inheritance='multi') 167 173 168 class DatedContact(AbstractDated, Contact):174 class MixedDatedContact(AbstractDated, Contact): 169 175 using_options(inheritance='multi') 170 176 171 177 setup_all(True) 172 178 173 assert 'created_date' in DatedContact.table.columns174 assert 'modified_date' in DatedContact.table.columns175 assert DatedContact._descriptor.identity == 'dated_contact'176 assert DatedContact.table.name == 'dated_contact'179 assert 'created_date' in MixedDatedContact.table.columns 180 assert 'modified_date' in MixedDatedContact.table.columns 181 assert MixedDatedContact._descriptor.identity == 'mixed_dated_contact' 182 assert MixedDatedContact.table.name == 'mixed_dated_contact' 177 183 178 contact1 = DatedContact(first_name=u"Guido", last_name=u"van Rossum") 184 contact1 = MixedDatedContact(first_name=u"Guido", 185 last_name=u"van Rossum") 179 186 session.commit() 180 187 188
