annotate examples/turbogears/markuptest/model.py @ 110:44fbc30d78cd

update the example TurboGears app and include an example of using TurboGears wigets
author mgood
date Fri, 28 Jul 2006 18:57:55 +0000
parents f8612f05af99
children
rev   line source
110
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
1 from datetime import datetime
4
f8612f05af99 Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff changeset
2
110
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
3 from sqlobject import *
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
4
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
5 from turbogears import identity
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
6 from turbogears.database import PackageHub
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
7
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
8 hub = PackageHub("markuptest")
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
9 __connection__ = hub
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
10
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
11 # class YourDataClass(SQLObject):
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
12 # pass
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
13
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
14 class Visit(SQLObject):
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
15 class sqlmeta:
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
16 table="visit"
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
17
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
18 visit_key= StringCol( length=40, alternateID=True,
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
19 alternateMethodName="by_visit_key" )
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
20 created= DateTimeCol( default=datetime.now )
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
21 expiry= DateTimeCol()
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
22
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
23 def lookup_visit( cls, visit_key ):
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
24 try:
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
25 return cls.by_visit_key( visit_key )
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
26 except SQLObjectNotFound:
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
27 return None
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
28 lookup_visit= classmethod(lookup_visit)
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
29
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
30 class VisitIdentity(SQLObject):
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
31 visit_key = StringCol(length=40, alternateID=True,
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
32 alternateMethodName="by_visit_key")
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
33 user_id = IntCol()
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
34
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
35
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
36 class Group(SQLObject):
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
37 """
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
38 An ultra-simple group definition.
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
39 """
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
40
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
41 # names like "Group", "Order" and "User" are reserved words in SQL
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
42 # so we set the name to something safe for SQL
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
43 class sqlmeta:
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
44 table="tg_group"
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
45
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
46 group_name = UnicodeCol(length=16, alternateID=True,
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
47 alternateMethodName="by_group_name")
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
48 display_name = UnicodeCol(length=255)
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
49 created = DateTimeCol(default=datetime.now)
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
50
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
51 # collection of all users belonging to this group
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
52 users = RelatedJoin("User", intermediateTable="user_group",
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
53 joinColumn="group_id", otherColumn="user_id")
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
54
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
55 # collection of all permissions for this group
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
56 permissions = RelatedJoin("Permission", joinColumn="group_id",
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
57 intermediateTable="group_permission",
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
58 otherColumn="permission_id")
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
59
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
60
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
61 class User(SQLObject):
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
62 """
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
63 Reasonably basic User definition. Probably would want additional attributes.
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
64 """
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
65 # names like "Group", "Order" and "User" are reserved words in SQL
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
66 # so we set the name to something safe for SQL
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
67 class sqlmeta:
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
68 table="tg_user"
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
69
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
70 user_name = UnicodeCol(length=16, alternateID=True,
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
71 alternateMethodName="by_user_name")
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
72 email_address = UnicodeCol(length=255, alternateID=True,
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
73 alternateMethodName="by_email_address")
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
74 display_name = UnicodeCol(length=255)
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
75 password = UnicodeCol(length=40)
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
76 created = DateTimeCol(default=datetime.now)
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
77
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
78 # groups this user belongs to
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
79 groups = RelatedJoin("Group", intermediateTable="user_group",
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
80 joinColumn="user_id", otherColumn="group_id")
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
81
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
82 def _get_permissions(self):
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
83 perms = set()
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
84 for g in self.groups:
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
85 perms = perms | set(g.permissions)
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
86 return perms
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
87
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
88 def _set_password(self, cleartext_password):
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
89 "Runs cleartext_password through the hash algorithm before saving."
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
90 hash = identity.encrypt_password(cleartext_password)
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
91 self._SO_set_password(hash)
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
92
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
93 def set_password_raw(self, password):
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
94 "Saves the password as-is to the database."
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
95 self._SO_set_password(password)
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
96
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
97
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
98
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
99 class Permission(SQLObject):
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
100 permission_name = UnicodeCol(length=16, alternateID=True,
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
101 alternateMethodName="by_permission_name")
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
102 description = UnicodeCol(length=255)
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
103
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
104 groups = RelatedJoin("Group",
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
105 intermediateTable="group_permission",
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
106 joinColumn="permission_id",
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
107 otherColumn="group_id")
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
108
44fbc30d78cd update the example TurboGears app and include an example of using TurboGears wigets
mgood
parents: 4
diff changeset
109
Copyright (C) 2012-2017 Edgewall Software