~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-03-22 06:05:32 UTC
  • Revision ID: matt.giuca@gmail.com-20100322060532-5365361xrx9mh32v
Changed database.py get_svn_url to take a req; include the req.user.login in the Subversion URL. This allows you to check out repositories without separately supplying the IVLE URL (as Subversion won't ask for a username by default). Also removed --username= from the lecturer project view, as it's redundant now. This fixes Launchpad bug #543936.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
from ivle.webapp.publisher import NoPath
32
32
from ivle.webapp.breadcrumbs import Breadcrumber
33
33
 
34
 
class XHTMLView(BaseView):
 
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):
35
56
    """
36
57
    A view which provides a base class for views which need to return XHTML
37
58
    It is expected that apps which use this view will be written using Genshi
40
61
 
41
62
    template = 'template.html'
42
63
    allow_overlays = True
 
64
    breadcrumb_text = None
43
65
 
44
66
    def __init__(self, *args, **kwargs):
45
67
        super(XHTMLView, self).__init__(*args, **kwargs)
70
92
        # view.
71
93
        app_template = os.path.join(os.path.dirname(
72
94
                        inspect.getmodule(self).__file__), self.template) 
73
 
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
74
 
        tmpl = loader.load(app_template)
 
95
        tmpl = self._loader.load(app_template)
75
96
        app = self.filter(tmpl.generate(viewctx), viewctx)
76
97
 
77
98
        view_scripts = []
105
126
        ctx['title_img'] = media_url(req, CorePlugin,
106
127
                                     "images/chrome/root-breadcrumb.png")
107
128
        try:
108
 
            ctx['ancestry'] = self.get_context_ancestry(req)
 
129
            ancestry = self.get_context_ancestry(req)
109
130
        except NoPath:
110
 
            ctx['ancestry'] = []
111
 
 
112
 
        # Allow the view to add its own fake breadcrumbs.
113
 
        ctx['extra_breadcrumbs'] = self.extra_breadcrumbs
114
 
 
115
 
        ctx['crumb'] = Breadcrumber(req).crumb
 
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
 
116
155
        self.populate_headings(req, ctx)
117
 
        tmpl = loader.load(os.path.join(os.path.dirname(__file__), 
 
156
        tmpl = self._loader.load(os.path.join(os.path.dirname(__file__), 
118
157
                                                        'ivle-headings.html'))
119
158
        req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))
120
159
        
144
183
                continue
145
184
 
146
185
            for tab in plugin.tabs:
147
 
                # 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)
148
188
                new_app = {}
149
189
                new_app['this_app'] = hasattr(self, 'tab') \
150
190
                                      and tab[0] == self.tab
158
198
                        ctx['favicon'] = icon_url
159
199
                else:
160
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
161
207
                new_app['path'] = req.make_path(tab[4])
162
208
                new_app['desc'] = tab[2]
163
209
                new_app['name'] = tab[1]
219
265
    def populate(self, req, ctx):
220
266
        ctx['req'] = req
221
267
        ctx['exception'] = self.context
 
268
        req.headers_out['X-IVLE-Error'] = self.context.message
222
269
 
223
270
class XHTMLUnauthorizedView(XHTMLErrorView):
224
271
    template = 'xhtmlunauthorized.html'
226
273
    def __init__(self, req, exception, lastobj):
227
274
        super(XHTMLUnauthorizedView, self).__init__(req, exception, lastobj)
228
275
 
229
 
        if req.user is None:
 
276
        if not req.publicmode and req.user is None:
230
277
            # Not logged in. Redirect to login page.
231
278
            if req.uri == '/':
232
279
                query_string = ''
235
282
            req.throw_redirect('/+login' + query_string)
236
283
 
237
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