root / elixir / trunk / docs / examples.rst @ 97

Revision 97, 3.1 kB (checked in by ged, 6 years ago)

As reported by Jorge Godoy, the TG example used a strange has_field statement
with a manual foreignkey instead of a belongs_to relationship, so I changed
that.

Line 
1========
2Examples
3========
4
5---------------
6The Video Store
7---------------
8
9The Elixir source distribution includes a sample web application that uses the
10`TurboGears <http://www.turbogears.org/>`_ web application framework. The
11application builds upon the tutorial's Movie model to create a simple store for
12buying movies.
13
14The Video Store sample application also includes an example of how to use Elixir
15with the TurboGears "identity" framework for security and authorization.  If
16you are planning to use Elixir with your TurboGears application, and need to
17support authorization using identity, you can use this model as a basis:
18
19::
20
21    from turbogears.database    import metadata, session
22    from elixir                 import Unicode, DateTime, String, Integer
23    from elixir                 import Entity, has_field, using_options
24    from elixir                 import has_many, belongs_to, has_and_belongs_to_many
25    from sqlalchemy             import ForeignKey
26    from datetime               import datetime
27
28    class Visit(Entity):
29        has_field('visit_key', String(40), primary_key=True)
30        has_field('created', DateTime, nullable=False, default=datetime.now)
31        has_field('expiry', DateTime)
32        using_options(tablename='visit')
33   
34        @classmethod
35        def lookup_visit(cls, visit_key):
36            return Visit.get(visit_key)
37
38    class VisitIdentity(Entity):
39        has_field('visit_key', String(40), primary_key=True)
40        belongs_to('user', of_kind='User', colname='user_id', use_alter=True)
41        using_options(tablename='visit_identity')
42
43    class Group(Entity):
44        has_field('group_id', Integer, primary_key=True)
45        has_field('group_name', Unicode(16), unique=True)
46        has_field('display_name', Unicode(255))
47        has_field('created', DateTime, default=datetime.now)
48        has_and_belongs_to_many('users', of_kind='User', inverse='groups')
49        has_and_belongs_to_many('permissions', of_kind='Permission', inverse='groups')
50        using_options(tablename='tg_group')
51
52    class User(Entity):
53        has_field('user_id', Integer, primary_key=True)
54        has_field('user_name', Unicode(16), unique=True)
55        has_field('email_address', Unicode(255), unique=True)
56        has_field('display_name', Unicode(255))
57        has_field('password', Unicode(40))
58        has_field('created', DateTime, default=datetime.now)
59        has_and_belongs_to_many('groups', of_kind='Group', inverse='users')
60        using_options(tablename='tg_user')
61   
62        @property
63        def permissions(self):
64            perms = set()
65            for g in self.groups:
66                perms = perms | set(g.permissions)
67            return perms
68
69    class Permission(Entity):
70        has_field('permission_id', Integer, primary_key=True)
71        has_field('permission_name', Unicode(16), unique=True)
72        has_field('description', Unicode(255))
73        has_and_belongs_to_many('groups', of_kind='Group', inverse='permissions')
74        using_options(tablename='permission')
75
76
77More Elixir examples are coming soon, and we would appreciate any additional
78sample applications that you could provide to illustrate more complex mappings.
Note: See TracBrowser for help on using the browser.