~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-24 01:32:50 UTC
  • Revision ID: grantw@unimelb.edu.au-20100224013250-3pfbslg7a5i2eb8g
Refactor UserAdminView to use BaseFormView.

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
26
from ivle.webapp.base.xhtml import XHTMLView
26
27
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
27
28
from ivle.webapp.admin.publishing import root_to_user, user_url
92
93
                                                    if_missing=None
93
94
                                                    )
94
95
 
95
 
class UserAdminView(XHTMLView):
 
96
class UserAdminView(BaseFormView):
96
97
    """A form for admins to change more of a user's details."""
97
98
    template = 'templates/user-admin.html'
98
99
    tab = 'users'
101
102
        """Only allow access if the requesting user is an admin."""
102
103
        return req.user and req.user.admin
103
104
 
104
 
    def filter(self, stream, ctx):
105
 
        return stream | HTMLFormFiller(data=ctx['data'])
 
105
    @property
 
106
    def validator(self):
 
107
        return UserAdminSchema()
 
108
 
 
109
    def get_default_data(self, req):
 
110
        return {'admin': self.context.admin,
 
111
                'disabled': self.context.state == u'disabled',
 
112
                'fullname': self.context.fullname,
 
113
                'studentid': self.context.studentid,
 
114
                }
 
115
 
 
116
    def save_object(self, req, data):
 
117
        if self.context is req.user:
 
118
            # Admin checkbox is disabled -- assume unchanged
 
119
            data['admin'] = self.context.admin
 
120
            data['disabled'] = self.context.state == u'disabled'
 
121
        else:
 
122
            self.context.admin = data['admin']
 
123
            if self.context.state in (u'enabled', u'disabled'):
 
124
                self.context.state = (u'disabled' if data['disabled']
 
125
                        else u'enabled')
 
126
        self.context.fullname = data['fullname'] \
 
127
                                if data['fullname'] else None
 
128
        self.context.studentid = data['studentid'] \
 
129
                                 if data['studentid'] else None
 
130
        return self.context
 
131
 
106
132
 
107
133
    def populate(self, req, ctx):
108
 
        if req.method == 'POST':
109
 
            data = dict(req.get_fieldstorage())
110
 
            try:
111
 
                validator = UserAdminSchema()
112
 
                data = validator.to_python(data, state=req)
113
 
 
114
 
                if self.context is req.user:
115
 
                    # Admin checkbox is disabled -- assume unchanged
116
 
                    data['admin'] = self.context.admin
117
 
                    data['disabled'] = self.context.state == u'disabled'
118
 
                else:
119
 
                    self.context.admin = data['admin']
120
 
                    if self.context.state in (u'enabled', u'disabled'):
121
 
                        self.context.state = (u'disabled' if data['disabled']
122
 
                                else u'enabled')
123
 
                self.context.fullname = data['fullname'] \
124
 
                                        if data['fullname'] else None
125
 
                self.context.studentid = data['studentid'] \
126
 
                                         if data['studentid'] else None
127
 
                req.store.commit()
128
 
                req.throw_redirect(req.uri)
129
 
            except formencode.Invalid, e:
130
 
                errors = e.unpack_errors()
131
 
        else:
132
 
            data = {'admin': self.context.admin,
133
 
                    'disabled': self.context.state == u'disabled',
134
 
                    'fullname': self.context.fullname,
135
 
                    'studentid': self.context.studentid,
136
 
                   }
137
 
            errors = {}
138
 
 
139
 
        ctx['req'] = req
140
 
        ctx['user'] = self.context
141
 
        # Disable the Admin checkbox if editing oneself
 
134
        super(UserAdminView, self).populate(req, ctx)
 
135
 
 
136
        # Disable the admin checkbox if editing oneself
142
137
        ctx['disable_admin'] = self.context is req.user
143
 
        ctx['data'] = data
144
 
        ctx['errors'] = errors
145
138
 
146
139
class PasswordChangeView(XHTMLView):
147
140
    """A form to change a user's password, with knowledge of the old one."""