~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: 2009-06-30 02:18:26 UTC
  • mto: This revision was merged to the branch mainline in revision 1324.
  • Revision ID: grantw@unimelb.edu.au-20090630021826-wairb9lk0ezm2vtp
Add a UserAdminView, to set fullname/studentid/admin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
105
105
        ctx['data'] = data
106
106
        ctx['errors'] = errors
107
107
 
 
108
class UserAdminSchema(formencode.Schema):
 
109
    admin = formencode.validators.StringBoolean(if_missing=False)
 
110
    fullname = formencode.validators.UnicodeString(not_empty=True)
 
111
    studentid = formencode.validators.UnicodeString(not_empty=False,
 
112
                                                    if_missing=None
 
113
                                                    )
 
114
 
 
115
class UserAdminView(XHTMLView):
 
116
    """A form for admins to change more of a user's details."""
 
117
    template = 'templates/user-admin.html'
 
118
    tab = 'settings'
 
119
 
 
120
    def __init__(self, req, login):
 
121
        self.context = ivle.database.User.get_by_login(req.store, login)
 
122
        if self.context is None:
 
123
            raise NotFound()
 
124
 
 
125
    def authorize(self, req):
 
126
        """Only allow access if the requesting user is an admin."""
 
127
        return req.user.admin
 
128
 
 
129
    def filter(self, stream, ctx):
 
130
        return stream | HTMLFormFiller(data=ctx['data'])
 
131
 
 
132
    def populate(self, req, ctx):
 
133
        if req.method == 'POST':
 
134
            data = dict(req.get_fieldstorage())
 
135
            try:
 
136
                validator = UserAdminSchema()
 
137
                data = validator.to_python(data, state=req)
 
138
 
 
139
                self.context.admin = data['admin']
 
140
                self.context.fullname = data['fullname'] \
 
141
                                        if data['fullname'] else None
 
142
                self.context.studentid = data['studentid'] \
 
143
                                         if data['studentid'] else None
 
144
                req.store.commit()
 
145
                req.throw_redirect(req.uri)
 
146
            except formencode.Invalid, e:
 
147
                errors = e.unpack_errors()
 
148
        else:
 
149
            data = {'admin': self.context.admin,
 
150
                    'fullname': self.context.fullname,
 
151
                    'studentid': self.context.studentid,
 
152
                   }
 
153
            errors = {}
 
154
 
 
155
        ctx['req'] = req
 
156
        ctx['user'] = self.context
 
157
        ctx['data'] = data
 
158
        ctx['errors'] = errors
 
159
 
108
160
class PasswordChangeView(XHTMLView):
109
161
    """A form to change a user's password, with knowledge of the old one."""
110
162
    template = 'templates/user-password-change.html'
184
236
    # The kwargs dict is passed to the __init__ of the view object
185
237
    urls = [
186
238
        ('~:login/+edit', UserEditView),
 
239
        ('~:login/+admin', UserAdminView),
187
240
        ('~:login/+changepassword', PasswordChangeView),
188
241
        ('~:login/+resetpassword', PasswordResetView),
189
242
        ('api/~:login', UserRESTView),