~jtaylor/ubuntu/oneiric/genshi/dh_python2

« back to all changes in this revision

Viewing changes to examples/turbogears/genshitest/model.py

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2010-05-05 02:05:51 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100505020551-zmiep9cjs3s5c093
Tags: 0.6-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - debian/rules: Enable testsuite.
  - Update maintainer according to specification.

Show diffs side-by-side

added added

removed removed

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