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

« back to all changes in this revision

Viewing changes to ivle/webapp/admin/user.py

  • Committer: William Grant
  • Date: 2010-02-11 05:09:56 UTC
  • Revision ID: grantw@unimelb.edu.au-20100211050956-t5i2z6b8iulxteza
Unbreak existing tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
from genshi.filters import HTMLFormFiller
23
23
 
24
24
from ivle.webapp import ApplicationRoot
25
 
from ivle.webapp.base.forms import BaseFormView
 
25
from ivle.webapp.base.rest import JSONRESTView, require_permission
26
26
from ivle.webapp.base.xhtml import XHTMLView
27
27
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
28
28
from ivle.webapp.admin.publishing import root_to_user, user_url
29
29
from ivle.database import User
 
30
import ivle.database
30
31
import ivle.date
 
32
import ivle.util
31
33
 
32
34
 
33
35
class UsersView(XHTMLView):
37
39
    breadcrumb_text = 'Users'
38
40
 
39
41
    def authorize(self, req):
40
 
        return req.user and req.user.admin
 
42
        return req.user.admin
41
43
 
42
44
    def populate(self, req, ctx):
43
45
        ctx['req'] = req
44
46
        ctx['users'] = req.store.find(User).order_by(User.login)
45
47
 
46
48
 
 
49
# List of fields returned as part of the user JSON dictionary
 
50
# (as returned by the get_user action)
 
51
user_fields_list = (
 
52
    "login", "state", "unixid", "email", "nick", "fullname",
 
53
    "admin", "studentid", "acct_exp", "pass_exp", "last_login",
 
54
    "svn_pass"
 
55
)
 
56
 
 
57
class UserRESTView(JSONRESTView):
 
58
    """
 
59
    A REST interface to the user object.
 
60
    """
 
61
 
 
62
    @require_permission('view')
 
63
    def GET(self, req):
 
64
        # XXX Check Caps
 
65
        user = ivle.util.object_to_dict(user_fields_list, self.context)
 
66
        # Convert time stamps to nice strings
 
67
        for k in 'pass_exp', 'acct_exp', 'last_login':
 
68
            if user[k] is not None:
 
69
                user[k] = unicode(user[k])
 
70
 
 
71
        user['local_password'] = self.context.passhash is not None
 
72
        return user
 
73
 
47
74
class UserEditSchema(formencode.Schema):
48
75
    nick = formencode.validators.UnicodeString(not_empty=True)
49
76
    email = formencode.validators.Email(not_empty=False,
50
77
                                        if_missing=None)
51
78
 
52
 
class UserEditView(BaseFormView):
 
79
class UserEditView(XHTMLView):
53
80
    """A form to change a user's details."""
54
81
    template = 'templates/user-edit.html'
55
82
    tab = 'users'
56
83
    permission = 'edit'
57
84
 
58
 
    @property
59
 
    def validator(self):
60
 
        return UserEditSchema()
61
 
 
62
 
    def get_default_data(self, req):
63
 
        return {'nick': self.context.nick,
64
 
                'email': self.context.email
65
 
                }
66
 
 
67
 
    def save_object(self, req, data):
68
 
        self.context.nick = data['nick']
69
 
        self.context.email = unicode(data['email']) if data['email'] \
70
 
                             else None
71
 
        return self.context
 
85
    def filter(self, stream, ctx):
 
86
        return stream | HTMLFormFiller(data=ctx['data'])
72
87
 
73
88
    def populate(self, req, ctx):
74
 
        super(UserEditView, self).populate(req, ctx)
 
89
        if req.method == 'POST':
 
90
            data = dict(req.get_fieldstorage())
 
91
            try:
 
92
                validator = UserEditSchema()
 
93
                data = validator.to_python(data, state=req)
 
94
                self.context.nick = data['nick']
 
95
                self.context.email = unicode(data['email']) if data['email'] \
 
96
                                     else None
 
97
                req.store.commit()
 
98
                req.throw_redirect(req.uri)
 
99
            except formencode.Invalid, e:
 
100
                errors = e.unpack_errors()
 
101
        else:
 
102
            data = {'nick': self.context.nick,
 
103
                    'email': self.context.email
 
104
                   }
 
105
            errors = {}
 
106
 
75
107
        ctx['format_datetime'] = ivle.date.make_date_nice
76
108
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
77
109
 
 
110
        ctx['req'] = req
 
111
        ctx['user'] = self.context
 
112
        ctx['data'] = data
 
113
        ctx['errors'] = errors
78
114
 
79
115
class UserAdminSchema(formencode.Schema):
80
116
    admin = formencode.validators.StringBoolean(if_missing=False)
81
 
    disabled = formencode.validators.StringBoolean(if_missing=False)
82
117
    fullname = formencode.validators.UnicodeString(not_empty=True)
83
118
    studentid = formencode.validators.UnicodeString(not_empty=False,
84
119
                                                    if_missing=None
85
120
                                                    )
86
121
 
87
 
class UserAdminView(BaseFormView):
 
122
class UserAdminView(XHTMLView):
88
123
    """A form for admins to change more of a user's details."""
89
124
    template = 'templates/user-admin.html'
90
125
    tab = 'users'
91
126
 
92
127
    def authorize(self, req):
93
128
        """Only allow access if the requesting user is an admin."""
94
 
        return req.user and req.user.admin
95
 
 
96
 
    @property
97
 
    def validator(self):
98
 
        return UserAdminSchema()
99
 
 
100
 
    def get_default_data(self, req):
101
 
        return {'admin': self.context.admin,
102
 
                'disabled': self.context.state == u'disabled',
103
 
                'fullname': self.context.fullname,
104
 
                'studentid': self.context.studentid,
105
 
                }
106
 
 
107
 
    def save_object(self, req, data):
108
 
        if self.context is req.user:
109
 
            # Admin checkbox is disabled -- assume unchanged
110
 
            data['admin'] = self.context.admin
111
 
            data['disabled'] = self.context.state == u'disabled'
 
129
        return req.user.admin
 
130
 
 
131
    def filter(self, stream, ctx):
 
132
        return stream | HTMLFormFiller(data=ctx['data'])
 
133
 
 
134
    def populate(self, req, ctx):
 
135
        if req.method == 'POST':
 
136
            data = dict(req.get_fieldstorage())
 
137
            try:
 
138
                validator = UserAdminSchema()
 
139
                data = validator.to_python(data, state=req)
 
140
 
 
141
                self.context.admin = data['admin']
 
142
                self.context.fullname = data['fullname'] \
 
143
                                        if data['fullname'] else None
 
144
                self.context.studentid = data['studentid'] \
 
145
                                         if data['studentid'] else None
 
146
                req.store.commit()
 
147
                req.throw_redirect(req.uri)
 
148
            except formencode.Invalid, e:
 
149
                errors = e.unpack_errors()
112
150
        else:
113
 
            self.context.admin = data['admin']
114
 
            if self.context.state in (u'enabled', u'disabled'):
115
 
                self.context.state = (u'disabled' if data['disabled']
116
 
                        else u'enabled')
117
 
        self.context.fullname = data['fullname'] \
118
 
                                if data['fullname'] else None
119
 
        self.context.studentid = data['studentid'] \
120
 
                                 if data['studentid'] else None
121
 
        return self.context
122
 
 
123
 
    def populate(self, req, ctx):
124
 
        super(UserAdminView, self).populate(req, ctx)
125
 
 
126
 
        # Disable the admin checkbox if editing oneself
127
 
        ctx['disable_admin'] = self.context is req.user
 
151
            data = {'admin': self.context.admin,
 
152
                    'fullname': self.context.fullname,
 
153
                    'studentid': self.context.studentid,
 
154
                   }
 
155
            errors = {}
 
156
 
 
157
        ctx['req'] = req
 
158
        ctx['user'] = self.context
 
159
        ctx['data'] = data
 
160
        ctx['errors'] = errors
128
161
 
129
162
class PasswordChangeView(XHTMLView):
130
163
    """A form to change a user's password, with knowledge of the old one."""
167
200
 
168
201
    def authorize(self, req):
169
202
        """Only allow access if the requesting user is an admin."""
170
 
        return req.user and req.user.admin
 
203
        return req.user.admin
171
204
 
172
205
    def populate(self, req, ctx):
173
206
        error = None
193
226
    forward_routes = (root_to_user,)
194
227
    reverse_routes = (user_url,)
195
228
    views = [(ApplicationRoot, 'users', UsersView),
196
 
             (User, '+index', UserEditView),
197
 
             (User, '+admin', UserAdminView),
198
 
             (User, '+changepassword', PasswordChangeView),
199
 
             (User, '+resetpassword', PasswordResetView),
 
229
             (ivle.database.User, '+index', UserEditView),
 
230
             (ivle.database.User, '+admin', UserAdminView),
 
231
             (ivle.database.User, '+changepassword', PasswordChangeView),
 
232
             (ivle.database.User, '+resetpassword', PasswordResetView),
 
233
             (ivle.database.User, '+index', UserRESTView, 'api'),
200
234
             ]
201
235
 
202
236
    tabs = [
203
237
        ('users', 'Users', 'Display and edit all users',
204
 
         'users.png', 'users', 90, True)
 
238
         'users.png', 'users', 0, True)
205
239
    ]
206
240
 
207
241
    public_forward_routes = forward_routes