~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-26 06:33:35 UTC
  • Revision ID: grantw@unimelb.edu.au-20100226063335-25ka31up8cegwaiz
ivle.interpret.execute_raw now sets a clean environment, in particular with HOME set correctly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import formencode.validators
22
22
from genshi.filters import HTMLFormFiller
23
23
 
 
24
from ivle.database import User
 
25
import ivle.date
 
26
from ivle.pulldown_subj import enrol_user
24
27
from ivle.webapp import ApplicationRoot
25
 
from ivle.webapp.base.rest import JSONRESTView, require_permission
 
28
from ivle.webapp.base.forms import BaseFormView, URLNameValidator
26
29
from ivle.webapp.base.xhtml import XHTMLView
27
30
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
28
31
from ivle.webapp.admin.publishing import root_to_user, user_url
29
 
from ivle.database import User
30
 
import ivle.database
31
 
import ivle.date
32
 
import ivle.util
33
32
 
34
33
 
35
34
class UsersView(XHTMLView):
46
45
        ctx['users'] = req.store.find(User).order_by(User.login)
47
46
 
48
47
 
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
 
 
74
48
class UserEditSchema(formencode.Schema):
75
49
    nick = formencode.validators.UnicodeString(not_empty=True)
76
50
    email = formencode.validators.Email(not_empty=False,
77
51
                                        if_missing=None)
78
52
 
79
 
class UserEditView(XHTMLView):
 
53
class UserEditView(BaseFormView):
80
54
    """A form to change a user's details."""
81
55
    template = 'templates/user-edit.html'
82
56
    tab = 'users'
83
57
    permission = 'edit'
84
58
 
85
 
    def filter(self, stream, ctx):
86
 
        return stream | HTMLFormFiller(data=ctx['data'])
 
59
    @property
 
60
    def validator(self):
 
61
        return UserEditSchema()
 
62
 
 
63
    def get_default_data(self, req):
 
64
        return {'nick': self.context.nick,
 
65
                'email': self.context.email
 
66
                }
 
67
 
 
68
    def save_object(self, req, data):
 
69
        self.context.nick = data['nick']
 
70
        self.context.email = unicode(data['email']) if data['email'] \
 
71
                             else None
 
72
        return self.context
87
73
 
88
74
    def populate(self, 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
        super(UserEditView, self).populate(req, ctx)
107
76
        ctx['format_datetime'] = ivle.date.make_date_nice
108
77
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
109
78
 
110
 
        ctx['req'] = req
111
 
        ctx['user'] = self.context
112
 
        ctx['data'] = data
113
 
        ctx['errors'] = errors
114
79
 
115
80
class UserAdminSchema(formencode.Schema):
116
81
    admin = formencode.validators.StringBoolean(if_missing=False)
120
85
                                                    if_missing=None
121
86
                                                    )
122
87
 
123
 
class UserAdminView(XHTMLView):
 
88
class UserAdminView(BaseFormView):
124
89
    """A form for admins to change more of a user's details."""
125
90
    template = 'templates/user-admin.html'
126
91
    tab = 'users'
129
94
        """Only allow access if the requesting user is an admin."""
130
95
        return req.user and req.user.admin
131
96
 
132
 
    def filter(self, stream, ctx):
133
 
        return stream | HTMLFormFiller(data=ctx['data'])
 
97
    @property
 
98
    def validator(self):
 
99
        return UserAdminSchema()
 
100
 
 
101
    def get_default_data(self, req):
 
102
        return {'admin': self.context.admin,
 
103
                'disabled': self.context.state == u'disabled',
 
104
                'fullname': self.context.fullname,
 
105
                'studentid': self.context.studentid,
 
106
                }
 
107
 
 
108
    def save_object(self, req, data):
 
109
        if self.context is req.user:
 
110
            # Admin checkbox is disabled -- assume unchanged
 
111
            data['admin'] = self.context.admin
 
112
            data['disabled'] = self.context.state == u'disabled'
 
113
        else:
 
114
            self.context.admin = data['admin']
 
115
            if self.context.state in (u'enabled', u'disabled'):
 
116
                self.context.state = (u'disabled' if data['disabled']
 
117
                        else u'enabled')
 
118
        self.context.fullname = data['fullname'] \
 
119
                                if data['fullname'] else None
 
120
        self.context.studentid = data['studentid'] \
 
121
                                 if data['studentid'] else None
 
122
        return self.context
134
123
 
135
124
    def populate(self, req, ctx):
136
 
        if req.method == 'POST':
137
 
            data = dict(req.get_fieldstorage())
138
 
            try:
139
 
                validator = UserAdminSchema()
140
 
                data = validator.to_python(data, state=req)
141
 
 
142
 
                if self.context is req.user:
143
 
                    # Admin checkbox is disabled -- assume unchanged
144
 
                    data['admin'] = self.context.admin
145
 
                    data['disabled'] = self.context.state == u'disabled'
146
 
                else:
147
 
                    self.context.admin = data['admin']
148
 
                    if self.context.state in (u'enabled', u'disabled'):
149
 
                        self.context.state = (u'disabled' if data['disabled']
150
 
                                else u'enabled')
151
 
                self.context.fullname = data['fullname'] \
152
 
                                        if data['fullname'] else None
153
 
                self.context.studentid = data['studentid'] \
154
 
                                         if data['studentid'] else None
155
 
                req.store.commit()
156
 
                req.throw_redirect(req.uri)
157
 
            except formencode.Invalid, e:
158
 
                errors = e.unpack_errors()
159
 
        else:
160
 
            data = {'admin': self.context.admin,
161
 
                    'disabled': self.context.state == u'disabled',
162
 
                    'fullname': self.context.fullname,
163
 
                    'studentid': self.context.studentid,
164
 
                   }
165
 
            errors = {}
166
 
 
167
 
        ctx['req'] = req
168
 
        ctx['user'] = self.context
169
 
        # Disable the Admin checkbox if editing oneself
 
125
        super(UserAdminView, self).populate(req, ctx)
 
126
 
 
127
        # Disable the admin checkbox if editing oneself
170
128
        ctx['disable_admin'] = self.context is req.user
171
 
        ctx['data'] = data
172
 
        ctx['errors'] = errors
173
129
 
174
130
class PasswordChangeView(XHTMLView):
175
131
    """A form to change a user's password, with knowledge of the old one."""
230
186
        ctx['user'] = self.context
231
187
        ctx['error'] = error
232
188
 
 
189
 
 
190
class UserNewSchema(formencode.Schema):
 
191
    login = URLNameValidator() # XXX: Validate uniqueness.
 
192
    admin = formencode.validators.StringBoolean(if_missing=False)
 
193
    fullname = formencode.validators.UnicodeString(not_empty=True)
 
194
    studentid = formencode.validators.UnicodeString(not_empty=False,
 
195
                                                    if_missing=None
 
196
                                                    )
 
197
    email = formencode.validators.Email(not_empty=False,
 
198
                                        if_missing=None)
 
199
 
 
200
 
 
201
class UserNewView(BaseFormView):
 
202
    """A form for admins to create new users."""
 
203
    template = 'templates/user-new.html'
 
204
    tab = 'users'
 
205
 
 
206
    def authorize(self, req):
 
207
        """Only allow access if the requesting user is an admin."""
 
208
        return req.user and req.user.admin
 
209
 
 
210
    @property
 
211
    def validator(self):
 
212
        return UserNewSchema()
 
213
 
 
214
    def get_default_data(self, req):
 
215
        return {}
 
216
 
 
217
    def save_object(self, req, data):
 
218
        data['nick'] = data['fullname']
 
219
        data['email'] = unicode(data['email']) if data['email'] else None
 
220
        userobj = User(**data)
 
221
        req.store.add(userobj)
 
222
        enrol_user(req.config, req.store, userobj)
 
223
 
 
224
        return userobj
 
225
 
 
226
 
233
227
class Plugin(ViewPlugin, MediaPlugin):
234
228
    """
235
229
    The Plugin class for the user plugin.
237
231
 
238
232
    forward_routes = (root_to_user,)
239
233
    reverse_routes = (user_url,)
240
 
    views = [(ApplicationRoot, 'users', UsersView),
241
 
             (ivle.database.User, '+index', UserEditView),
242
 
             (ivle.database.User, '+admin', UserAdminView),
243
 
             (ivle.database.User, '+changepassword', PasswordChangeView),
244
 
             (ivle.database.User, '+resetpassword', PasswordResetView),
245
 
             (ivle.database.User, '+index', UserRESTView, 'api'),
 
234
    views = [(ApplicationRoot, ('users', '+index'), UsersView),
 
235
             (ApplicationRoot, ('users', '+new'), UserNewView),
 
236
             (User, '+index', UserEditView),
 
237
             (User, '+admin', UserAdminView),
 
238
             (User, '+changepassword', PasswordChangeView),
 
239
             (User, '+resetpassword', PasswordResetView),
246
240
             ]
247
241
 
248
242
    tabs = [