~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-02-25 01:53:19 UTC
  • Revision ID: matt.giuca@gmail.com-20100225015319-7j0oounlhi1bj6fp
Fixed broken console, due to function called with not enough arguments.

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