~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to lib/common/user.py

  • Committer: matt.giuca
  • Date: 2009-01-11 23:39:33 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1071
Moved all script files into newly created 'bin' directory (cleanup).
Not ./setup.py -- too important.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
# user. Objects of this class are expected to be stored in the request
24
24
# session.
25
25
 
 
26
import db
 
27
import time
26
28
import common.caps
27
29
 
28
30
# Similar to db.login_fields_list but contains a different set of fields.
32
34
))
33
35
user_fields_list = (
34
36
    "login", "state", "unixid", "email", "nick", "fullname",
35
 
    "role", "studentid", "acct_exp", "pass_exp"
 
37
    "role", "studentid", "acct_exp", "pass_exp", "last_login",
 
38
    "svn_pass", "local_password",
36
39
)
 
40
timestamp_fields = frozenset((
 
41
    "acct_exp", "pass_exp", "last_login",
 
42
))
37
43
# Fields not included: passhash, last_login
38
44
 
39
45
class UserException(Exception):
51
57
        # XXX Will ignore unknown fields instead of erroring
52
58
        if "rolenm" in kwargs and "role" not in kwargs:
53
59
            kwargs['role'] = common.caps.Role(kwargs['rolenm'])
54
 
            del kwargs['rolenm']
 
60
        if "passhash" in kwargs and "local_password" not in kwargs:
 
61
            kwargs['local_password'] = kwargs['passhash'] is not None
55
62
        for r in user_fields_list:
56
63
            if r in kwargs:
57
64
                self.__setattr__(r, kwargs[r])
68
75
                            "'fullname' missing")
69
76
                else:
70
77
                    self.__setattr__(r, None)
 
78
        for field in timestamp_fields:
 
79
            # Convert all timestamp fields from strings to Timestamp objects
 
80
            if hasattr(self, field):
 
81
                val = self.__getattribute__(field)
 
82
                if val is not None:
 
83
                    val = time.strptime(val, db.TIMESTAMP_FORMAT)
 
84
                    self.__setattr__(field, val)
 
85
 
71
86
    def __repr__(self):
72
87
        items = ["%s=%s" % (r, repr(self.__getattribute__(r)))
73
88
            for r in user_fields_list]
74
89
        return "User(" + ', '.join(items) + ")"
 
90
    def __iter__(self):
 
91
        """Iteration yielding field:value pairs.
 
92
        (Allows the "dict" function to work on Users)
 
93
        """
 
94
        for r in user_fields_list:
 
95
            yield (r, self.__getattribute__(r))
75
96
 
76
97
    def hasCap(self, capability):
77
98
        """Given a capability (which is a Role object), returns True if this