Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

I think I've seen this ORM somewhere before...

root/trunk/zoo.py

Revision 38 (checked in by fumanchu, 8 years ago)

1. Added Unit.first(farClass, **kwargs) method to retrieve a single related Unit.
2. Fixed doc bug in related_units.
3. associate() is now module-level, not a method of arena. Arenas discover associations when each Unit is registered.
4. Added Arena.register_all().
5. Doc updates.
6. Added engines.register_classes(arena) function.
7. Rewrote tests to import common zoo module.

Line 
1 import datetime
2 import dejavu
3 from dejavu import Unit, UnitProperty, associate
4
5
6 class Zoo(Unit):
7     Name = UnitProperty()
8     Founded = UnitProperty(datetime.date)
9     Opens = UnitProperty(datetime.time)
10     LastEscape = UnitProperty(datetime.datetime)
11
12
13 class EscapeProperty(UnitProperty):
14     def post(self, unit, value):
15         z = unit.first(Zoo)
16         if z:
17             z.LastEscape = unit.LastEscape
18
19 class Animal(Unit):
20     Name = UnitProperty()
21     ZooID = UnitProperty(int, index=True)
22     Legs = UnitProperty(int)
23     Options = UnitProperty(dict)
24     LastEscape = EscapeProperty(datetime.datetime)
25
26 associate(Zoo, 'ID', Animal, 'ZooID')
27
28
29 class Exhibit(Unit):
30     ZooID = UnitProperty(int)
31     Animals = UnitProperty(list)
32
33 associate(Zoo, 'ID', Exhibit, 'ZooID')
34
35 arena = dejavu.Arena()
36 arena.register_all(globals())
Note: See TracBrowser for help on using the browser.