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

« back to all changes in this revision

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

  • Committer: David Coles
  • Date: 2009-12-08 02:10:26 UTC
  • Revision ID: coles.david@gmail.com-20091208021026-3a27ecdzm49y39me
Configuration documentation - fixing a few references

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.webapp import ApplicationRoot
25
 
from ivle.webapp.base.forms import BaseFormView
 
24
from ivle.webapp.base.rest import JSONRESTView, require_permission
26
25
from ivle.webapp.base.xhtml import XHTMLView
27
26
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
28
 
from ivle.webapp.admin.publishing import root_to_user, user_url
29
 
from ivle.database import User
 
27
from ivle.webapp.errors import NotFound, Unauthorized
 
28
import ivle.database
30
29
import ivle.date
31
 
 
32
 
 
33
 
class UsersView(XHTMLView):
34
 
    """A list of all IVLE users."""
35
 
    template = 'templates/users.html'
36
 
    tab = 'users'
37
 
    breadcrumb_text = 'Users'
38
 
 
39
 
    def authorize(self, req):
40
 
        return req.user and req.user.admin
41
 
 
42
 
    def populate(self, req, ctx):
43
 
        ctx['req'] = req
44
 
        ctx['users'] = req.store.find(User).order_by(User.login)
45
 
 
 
30
import ivle.util
 
31
 
 
32
# List of fields returned as part of the user JSON dictionary
 
33
# (as returned by the get_user action)
 
34
user_fields_list = (
 
35
    "login", "state", "unixid", "email", "nick", "fullname",
 
36
    "admin", "studentid", "acct_exp", "pass_exp", "last_login",
 
37
    "svn_pass"
 
38
)
 
39
 
 
40
class UserRESTView(JSONRESTView):
 
41
    """
 
42
    A REST interface to the user object.
 
43
    """
 
44
    def __init__(self, req, login):
 
45
        super(UserRESTView, self).__init__(self, req, login)
 
46
        self.context = ivle.database.User.get_by_login(req.store, login)
 
47
        if self.context is None:
 
48
            raise NotFound()
 
49
 
 
50
    @require_permission('view')
 
51
    def GET(self, req):
 
52
        # XXX Check Caps
 
53
        user = ivle.util.object_to_dict(user_fields_list, self.context)
 
54
        # Convert time stamps to nice strings
 
55
        for k in 'pass_exp', 'acct_exp', 'last_login':
 
56
            if user[k] is not None:
 
57
                user[k] = unicode(user[k])
 
58
 
 
59
        user['local_password'] = self.context.passhash is not None
 
60
        return user
46
61
 
47
62
class UserEditSchema(formencode.Schema):
48
63
    nick = formencode.validators.UnicodeString(not_empty=True)
49
64
    email = formencode.validators.Email(not_empty=False,
50
65
                                        if_missing=None)
51
66
 
52
 
class UserEditView(BaseFormView):
 
67
class UserEditView(XHTMLView):
53
68
    """A form to change a user's details."""
54
69
    template = 'templates/user-edit.html'
55
 
    tab = 'users'
 
70
    tab = 'settings'
56
71
    permission = 'edit'
57
72
 
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
 
73
    def __init__(self, req, login):
 
74
        self.context = ivle.database.User.get_by_login(req.store, login)
 
75
        if self.context is None:
 
76
            raise NotFound()
 
77
 
 
78
    def filter(self, stream, ctx):
 
79
        return stream | HTMLFormFiller(data=ctx['data'])
72
80
 
73
81
    def populate(self, req, ctx):
74
 
        super(UserEditView, self).populate(req, ctx)
 
82
        if req.method == 'POST':
 
83
            data = dict(req.get_fieldstorage())
 
84
            try:
 
85
                validator = UserEditSchema()
 
86
                data = validator.to_python(data, state=req)
 
87
                self.context.nick = data['nick']
 
88
                self.context.email = unicode(data['email']) if data['email'] \
 
89
                                     else None
 
90
                req.store.commit()
 
91
                req.throw_redirect(req.uri)
 
92
            except formencode.Invalid, e:
 
93
                errors = e.unpack_errors()
 
94
        else:
 
95
            data = {'nick': self.context.nick,
 
96
                    'email': self.context.email
 
97
                   }
 
98
            errors = {}
 
99
 
75
100
        ctx['format_datetime'] = ivle.date.make_date_nice
76
101
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
77
102
 
 
103
        ctx['req'] = req
 
104
        ctx['user'] = self.context
 
105
        ctx['data'] = data
 
106
        ctx['errors'] = errors
78
107
 
79
108
class UserAdminSchema(formencode.Schema):
80
109
    admin = formencode.validators.StringBoolean(if_missing=False)
81
 
    disabled = formencode.validators.StringBoolean(if_missing=False)
82
110
    fullname = formencode.validators.UnicodeString(not_empty=True)
83
111
    studentid = formencode.validators.UnicodeString(not_empty=False,
84
112
                                                    if_missing=None
85
113
                                                    )
86
114
 
87
 
class UserAdminView(BaseFormView):
 
115
class UserAdminView(XHTMLView):
88
116
    """A form for admins to change more of a user's details."""
89
117
    template = 'templates/user-admin.html'
90
 
    tab = 'users'
 
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()
91
124
 
92
125
    def authorize(self, req):
93
126
        """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'
 
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()
112
148
        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
 
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
128
159
 
129
160
class PasswordChangeView(XHTMLView):
130
161
    """A form to change a user's password, with knowledge of the old one."""
131
162
    template = 'templates/user-password-change.html'
132
 
    tab = 'users'
 
163
    tab = 'settings'
133
164
    permission = 'edit'
134
165
 
 
166
    def __init__(self, req, login):
 
167
        self.context = ivle.database.User.get_by_login(req.store, login)
 
168
        if self.context is None:
 
169
            raise NotFound()
 
170
 
135
171
    def authorize(self, req):
136
172
        """Only allow access if the requesting user holds the permission,
137
173
           and the target user has a password set. Otherwise we might be
163
199
class PasswordResetView(XHTMLView):
164
200
    """A form to reset a user's password, without knowledge of the old one."""
165
201
    template = 'templates/user-password-reset.html'
166
 
    tab = 'users'
 
202
    tab = 'settings'
 
203
 
 
204
    def __init__(self, req, login):
 
205
        self.context = ivle.database.User.get_by_login(req.store, login)
 
206
        if self.context is None:
 
207
            raise NotFound()
167
208
 
168
209
    def authorize(self, req):
169
210
        """Only allow access if the requesting user is an admin."""
170
 
        return req.user and req.user.admin
 
211
        return req.user.admin
171
212
 
172
213
    def populate(self, req, ctx):
173
214
        error = None
189
230
    """
190
231
    The Plugin class for the user plugin.
191
232
    """
192
 
 
193
 
    forward_routes = (root_to_user,)
194
 
    reverse_routes = (user_url,)
195
 
    views = [(ApplicationRoot, 'users', UsersView),
196
 
             (User, '+index', UserEditView),
197
 
             (User, '+admin', UserAdminView),
198
 
             (User, '+changepassword', PasswordChangeView),
199
 
             (User, '+resetpassword', PasswordResetView),
200
 
             ]
201
 
 
202
 
    tabs = [
203
 
        ('users', 'Users', 'Display and edit all users',
204
 
         'users.png', 'users', 90, True)
 
233
    # Magic attribute: urls
 
234
    # Sequence of pairs/triples of
 
235
    # (regex str, handler class, kwargs dict)
 
236
    # The kwargs dict is passed to the __init__ of the view object
 
237
    urls = [
 
238
        ('~:login/+edit', UserEditView),
 
239
        ('~:login/+admin', UserAdminView),
 
240
        ('~:login/+changepassword', PasswordChangeView),
 
241
        ('~:login/+resetpassword', PasswordResetView),
 
242
        ('api/~:login', UserRESTView),
205
243
    ]
206
244
 
207
 
    public_forward_routes = forward_routes
208
 
    public_reverse_routes = reverse_routes
209
 
 
210
245
    media = 'user-media'