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

« back to all changes in this revision

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

  • Committer: Matt Giuca
  • Date: 2010-07-21 04:12:30 UTC
  • Revision ID: matt.giuca@gmail.com-20100721041230-hlrn2q3fx2uvaprd
ivle.webapp.admin.user: Fixed call to req.user.get_svn_url (now takes 1 argument, as of r1810).
Fixes Internal server error on Settings page (since r1810).

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
from ivle.webapp.base.views import BaseView
29
29
from ivle.webapp.base.plugins import ViewPlugin, OverlayPlugin
30
30
from ivle.webapp.errors import HTTPError, Unauthorized
31
 
 
32
 
class XHTMLView(BaseView):
 
31
from ivle.webapp.publisher import NoPath
 
32
from ivle.webapp.breadcrumbs import Breadcrumber
 
33
 
 
34
 
 
35
class GenshiLoaderMixin(object):
 
36
    """Mixin for classes which need to render Genshi templates.
 
37
 
 
38
    A TemplateLoader is shared between all instances, so templates are
 
39
    cached across multiple instances and therefore also requests.
 
40
    """
 
41
    _loader = None
 
42
 
 
43
    def __init__(self, *args, **kwargs):
 
44
        super(GenshiLoaderMixin, self).__init__(*args, **kwargs)
 
45
 
 
46
        # We use a single loader for all views, so we can cache the
 
47
        # parsed templates. auto_reload is convenient and has a minimal
 
48
        # performance penalty, so we'll leave it on.
 
49
        if GenshiLoaderMixin._loader is None:
 
50
            GenshiLoaderMixin._loader = genshi.template.TemplateLoader(
 
51
                ".", auto_reload=True,
 
52
                max_cache_size=100)
 
53
 
 
54
 
 
55
class XHTMLView(GenshiLoaderMixin, BaseView):
33
56
    """
34
57
    A view which provides a base class for views which need to return XHTML
35
58
    It is expected that apps which use this view will be written using Genshi
37
60
    """
38
61
 
39
62
    template = 'template.html'
40
 
    plugin_scripts = {}
41
 
    plugin_styles = {}
42
63
    allow_overlays = True
43
 
    overlay_blacklist = []
44
 
 
45
 
    def __init__(self, req, **kwargs):
46
 
        for key in kwargs:
47
 
            setattr(self, key, kwargs[key])
 
64
    breadcrumb_text = None
 
65
 
 
66
    def __init__(self, *args, **kwargs):
 
67
        super(XHTMLView, self).__init__(*args, **kwargs)
 
68
 
 
69
        self.overlay_blacklist = []
 
70
 
 
71
        self.plugin_scripts = {}
 
72
        self.plugin_styles = {}
 
73
        self.scripts_init = []
 
74
 
 
75
        self.extra_breadcrumbs = []
 
76
        self.overlay_blacklist = []
 
77
 
 
78
    def get_context_ancestry(self, req):
 
79
        return req.publisher.get_ancestors(self.context)
48
80
 
49
81
    def filter(self, stream, ctx):
50
82
        return stream
60
92
        # view.
61
93
        app_template = os.path.join(os.path.dirname(
62
94
                        inspect.getmodule(self).__file__), self.template) 
63
 
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
64
 
        tmpl = loader.load(app_template)
 
95
        tmpl = self._loader.load(app_template)
65
96
        app = self.filter(tmpl.generate(viewctx), viewctx)
66
97
 
 
98
        view_scripts = []
67
99
        for plugin in self.plugin_scripts:
68
100
            for path in self.plugin_scripts[plugin]:
69
 
                req.scripts.append(media_url(req, plugin, path))
 
101
                view_scripts.append(media_url(req, plugin, path))
70
102
 
 
103
        view_styles = []
71
104
        for plugin in self.plugin_styles:
72
105
            for path in self.plugin_styles[plugin]:
73
 
                req.styles.append(media_url(req, plugin, path))
 
106
                view_styles.append(media_url(req, plugin, path))
74
107
 
75
108
        # Global template
76
109
        ctx = genshi.template.Context()
77
 
        # XXX: Leave this here!! (Before req.styles is read)
78
 
        ctx['overlays'] = self.render_overlays(req) if req.user else []
 
110
 
 
111
        overlay_bits = self.render_overlays(req) if req.user else [[]]*4
 
112
        ctx['overlays'] = overlay_bits[0]
79
113
 
80
114
        ctx['styles'] = [media_url(req, CorePlugin, 'ivle.css')]
81
 
        ctx['styles'] += req.styles
 
115
        ctx['styles'] += view_styles
 
116
        ctx['styles'] += overlay_bits[1]
82
117
 
83
118
        ctx['scripts'] = [media_url(req, CorePlugin, path) for path in
84
119
                           ('util.js', 'json2.js', 'md5.js')]
85
120
        ctx['scripts'].append(media_url(req, '+external/jquery', 'jquery.js'))
86
 
        ctx['scripts'] += req.scripts
 
121
        ctx['scripts'] += view_scripts
 
122
        ctx['scripts'] += overlay_bits[2]
87
123
 
88
 
        ctx['scripts_init'] = req.scripts_init
 
124
        ctx['scripts_init'] = self.scripts_init + overlay_bits[3]
89
125
        ctx['app_template'] = app
90
126
        ctx['title_img'] = media_url(req, CorePlugin,
91
 
                                     "images/chrome/title.png")
 
127
                                     "images/chrome/root-breadcrumb.png")
 
128
        try:
 
129
            ancestry = self.get_context_ancestry(req)
 
130
        except NoPath:
 
131
            ancestry = []
 
132
 
 
133
        crumber = Breadcrumber(req)
 
134
 
 
135
        ctx['breadcrumbs'] = []
 
136
        if not req.publicmode:
 
137
            for ancestor in ancestry:
 
138
                crumb = crumber.crumb(ancestor)
 
139
                if crumb is None:
 
140
                    continue
 
141
 
 
142
                if hasattr(crumb, 'extra_breadcrumbs_before'):
 
143
                    ctx['breadcrumbs'].extend(crumb.extra_breadcrumbs_before)
 
144
                ctx['breadcrumbs'].append(crumb)
 
145
                if hasattr(crumb, 'extra_breadcrumbs_after'):
 
146
                    ctx['breadcrumbs'].extend(crumb.extra_breadcrumbs_after)
 
147
 
 
148
            # If the view has specified text for a breadcrumb, add one.
 
149
            if self.breadcrumb_text:
 
150
                ctx['breadcrumbs'].append(ViewBreadcrumb(req, self))
 
151
 
 
152
            # Allow the view to add its own fake breadcrumbs.
 
153
            ctx['breadcrumbs'].extend(self.extra_breadcrumbs)
 
154
 
92
155
        self.populate_headings(req, ctx)
93
 
        tmpl = loader.load(os.path.join(os.path.dirname(__file__), 
 
156
        tmpl = self._loader.load(os.path.join(os.path.dirname(__file__), 
94
157
                                                        'ivle-headings.html'))
95
158
        req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))
96
159
        
120
183
                continue
121
184
 
122
185
            for tab in plugin.tabs:
123
 
                # tab is a tuple: name, title, desc, icon, path
 
186
                # tab is a tuple: name, title, desc, icon, path, weight, admin
 
187
                # (Admin is optional, defaults to false)
124
188
                new_app = {}
125
189
                new_app['this_app'] = hasattr(self, 'tab') \
126
190
                                      and tab[0] == self.tab
134
198
                        ctx['favicon'] = icon_url
135
199
                else:
136
200
                    new_app['has_icon'] = False
 
201
                # The following check is here, so it is AFTER setting the
 
202
                # icon, but BEFORE actually installing the tab in the menu
 
203
                if len(tab) > 6 and tab[6]:
 
204
                    # Admin-only tab
 
205
                    if not (req.user and req.user.admin):
 
206
                        break
137
207
                new_app['path'] = req.make_path(tab[4])
138
208
                new_app['desc'] = tab[2]
139
209
                new_app['name'] = tab[1]
149
219
        scripts_init.
150
220
        """
151
221
        overlays = []
 
222
        styles = []
 
223
        scripts = []
 
224
        scripts_init = []
152
225
        if not self.allow_overlays:
153
 
            return overlays
 
226
            return (overlays, styles, scripts, scripts_init)
154
227
 
155
228
        for plugin in req.config.plugin_index[OverlayPlugin]:
156
229
            for overclass in plugin.overlays:
160
233
                #TODO: Re-factor this to look nicer
161
234
                for mplugin in overlay.plugin_scripts:
162
235
                    for path in overlay.plugin_scripts[mplugin]:
163
 
                        req.scripts.append(media_url(req, mplugin, path))
 
236
                        scripts.append(media_url(req, mplugin, path))
164
237
 
165
238
                for mplugin in overlay.plugin_styles:
166
239
                    for path in overlay.plugin_styles[mplugin]:
167
 
                        req.styles.append(media_url(req, mplugin, path))
 
240
                        styles.append(media_url(req, mplugin, path))
168
241
 
169
 
                req.scripts_init += overlay.plugin_scripts_init
 
242
                scripts_init += overlay.plugin_scripts_init
170
243
 
171
244
                overlays.append(overlay.render(req))
172
 
        return overlays
 
245
        return (overlays, styles, scripts, scripts_init)
173
246
 
174
247
    @classmethod
175
248
    def get_error_view(cls, e):
182
255
class XHTMLErrorView(XHTMLView):
183
256
    template = 'xhtmlerror.html'
184
257
 
185
 
    def __init__(self, req, exception):
186
 
        self.context = exception
 
258
    def __init__(self, req, context, lastobj):
 
259
        super(XHTMLErrorView, self).__init__(req, context)
 
260
        self.lastobj = lastobj
 
261
 
 
262
    def get_context_ancestry(self, req):
 
263
        return req.publisher.get_ancestors(self.lastobj)
187
264
 
188
265
    def populate(self, req, ctx):
 
266
        ctx['req'] = req
189
267
        ctx['exception'] = self.context
 
268
        req.headers_out['X-IVLE-Error'] = self.context.message
190
269
 
191
270
class XHTMLUnauthorizedView(XHTMLErrorView):
192
271
    template = 'xhtmlunauthorized.html'
193
272
 
194
 
    def __init__(self, req, exception):
195
 
        super(XHTMLUnauthorizedView, self).__init__(req, exception)
 
273
    def __init__(self, req, exception, lastobj):
 
274
        super(XHTMLUnauthorizedView, self).__init__(req, exception, lastobj)
196
275
 
197
 
        if req.user is None:
 
276
        if not req.publicmode and req.user is None:
198
277
            # Not logged in. Redirect to login page.
199
278
            if req.uri == '/':
200
279
                query_string = ''
203
282
            req.throw_redirect('/+login' + query_string)
204
283
 
205
284
        req.status = 403
 
285
 
 
286
class ViewBreadcrumb(object):
 
287
    def __init__(self, req, context):
 
288
        self.req = req
 
289
        self.context = context
 
290
 
 
291
    @property
 
292
    def text(self):
 
293
        return self.context.breadcrumb_text