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

« back to all changes in this revision

Viewing changes to ivle/webapp/base/xhtml.py

Add an XHTMLUnauthorizedView which redirects unauthenticated users to the
login page if a page raises an Unauthorized. Alter UserSettingsView to raise
one in the right cases, for testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
from ivle.webapp.media import media_url
26
26
from ivle.webapp.base.views import BaseView
27
27
from ivle.webapp.base.plugins import OverlayPlugin
28
 
from ivle.webapp.errors import HTTPError
 
28
from ivle.webapp.errors import HTTPError, Unauthorized
29
29
import ivle.conf
30
30
import ivle.util
31
31
 
146
146
 
147
147
    @classmethod
148
148
    def get_error_view(cls, e):
149
 
        view_map = {HTTPError:    XHTMLErrorView,}
150
 
                    #Unauthorized: XHTMLUnauthorizedView}
 
149
        view_map = {HTTPError:    XHTMLErrorView,
 
150
                    Unauthorized: XHTMLUnauthorizedView}
151
151
        for exccls in inspect.getmro(type(e)):
152
152
            if exccls in view_map:
153
153
                return view_map[exccls]
160
160
 
161
161
    def populate(self, req, ctx):
162
162
        ctx['exception'] = self.context
 
163
 
 
164
class XHTMLUnauthorizedView(XHTMLErrorView):
 
165
    template = 'xhtmlunauthorized.html'
 
166
 
 
167
    def __init__(self, req, exception):
 
168
        super(XHTMLUnauthorizedView, self).__init__(req, exception)
 
169
        if req.user is None:
 
170
            # Not logged in. Redirect to login page.
 
171
            req.throw_redirect('/') # XXX: Need proper URL.
 
172
 
 
173
        req.status = 403