Reported bug with utf-8 strings
[be.git] / interfaces / web / Bugs-Everywhere-Web / beweb / model.py
1 from datetime import datetime
2
3 from sqlobject import *
4 from turbogears.database import PackageHub
5 from turbogears import identity
6
7 hub = PackageHub("beweb")
8 __connection__ = hub
9
10 class Visit(SQLObject):
11     class sqlmeta:
12         table = "visit"
13
14     visit_key = StringCol(length=40, alternateID=True,
15                           alternateMethodName="by_visit_key")
16     created = DateTimeCol(default=datetime.now)
17     expiry = DateTimeCol()
18
19     def lookup_visit(cls, visit_key):
20         try:
21             return cls.by_visit_key(visit_key)
22         except SQLObjectNotFound:
23             return None
24     lookup_visit = classmethod(lookup_visit)
25
26 class VisitIdentity(SQLObject):
27     visit_key = StringCol(length=40, alternateID=True,
28                           alternateMethodName="by_visit_key")
29     user_id = IntCol()
30
31
32 class Group(SQLObject):
33     """
34     An ultra-simple group definition.
35     """
36
37     # names like "Group", "Order" and "User" are reserved words in SQL
38     # so we set the name to something safe for SQL
39     class sqlmeta:
40         table = "tg_group"
41
42     group_name = UnicodeCol(length=16, alternateID=True,
43                             alternateMethodName="by_group_name")
44     display_name = UnicodeCol(length=255)
45     created = DateTimeCol(default=datetime.now)
46
47     # collection of all users belonging to this group
48     users = RelatedJoin("User", intermediateTable="user_group",
49                         joinColumn="group_id", otherColumn="user_id")
50
51     # collection of all permissions for this group
52     permissions = RelatedJoin("Permission", joinColumn="group_id", 
53                               intermediateTable="group_permission",
54                               otherColumn="permission_id")
55
56
57 class User(SQLObject):
58     """
59     Reasonably basic User definition. Probably would want additional attributes.
60     """
61     # names like "Group", "Order" and "User" are reserved words in SQL
62     # so we set the name to something safe for SQL
63     class sqlmeta:
64         table = "tg_user"
65
66     child_name = UnicodeCol(length=255)
67     user_name = UnicodeCol(length=16, alternateID=True,
68                            alternateMethodName="by_user_name")
69     email_address = UnicodeCol(length=255, alternateID=True,
70                                alternateMethodName="by_email_address")
71     display_name = UnicodeCol(length=255)
72     password = UnicodeCol(length=40)
73     created = DateTimeCol(default=datetime.now)
74
75     # groups this user belongs to
76     groups = RelatedJoin("Group", intermediateTable="user_group",
77                          joinColumn="user_id", otherColumn="group_id")
78
79     def _get_permissions(self):
80         perms = set()
81         for g in self.groups:
82             perms = perms | set(g.permissions)
83         return perms
84
85     def _set_password(self, cleartext_password):
86         "Runs cleartext_password through the hash algorithm before saving."
87         hash = identity.encrypt_password(cleartext_password)
88         self._SO_set_password(hash)
89
90     def set_password_raw(self, password):
91         "Saves the password as-is to the database."
92         self._SO_set_password(password)
93
94
95
96 class Permission(SQLObject):
97     permission_name = UnicodeCol(length=16, alternateID=True,
98                                  alternateMethodName="by_permission_name")
99     description = UnicodeCol(length=255)
100
101     groups = RelatedJoin("Group",
102                         intermediateTable="group_permission",
103                          joinColumn="permission_id", 
104                          otherColumn="group_id")
105
106 def people_map():
107     return dict((u.user_name, u.display_name) for u in User.select())