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

« back to all changes in this revision

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

Merge from object-publishing.

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
 
 
44
    def __init__(self, *args, **kwargs):
 
45
        super(XHTMLView, self).__init__(*args, **kwargs)
 
46
 
 
47
        self.overlay_blacklist = []
 
48
 
 
49
        self.plugin_scripts = {}
 
50
        self.plugin_styles = {}
 
51
        self.scripts_init = []
 
52
 
 
53
        self.extra_breadcrumbs = []
 
54
        self.overlay_blacklist = []
 
55
 
 
56
    def get_context_ancestry(self, req):
 
57
        return req.publisher.get_ancestors(self.context)
48
58
 
49
59
    def filter(self, stream, ctx):
50
60
        return stream
64
74
        tmpl = loader.load(app_template)
65
75
        app = self.filter(tmpl.generate(viewctx), viewctx)
66
76
 
 
77
        view_scripts = []
67
78
        for plugin in self.plugin_scripts:
68
79
            for path in self.plugin_scripts[plugin]:
69
 
                req.scripts.append(media_url(req, plugin, path))
 
80
                view_scripts.append(media_url(req, plugin, path))
70
81
 
 
82
        view_styles = []
71
83
        for plugin in self.plugin_styles:
72
84
            for path in self.plugin_styles[plugin]:
73
 
                req.styles.append(media_url(req, plugin, path))
 
85
                view_styles.append(media_url(req, plugin, path))
74
86
 
75
87
        # Global template
76
88
        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 []
 
89
 
 
90
        overlay_bits = self.render_overlays(req) if req.user else [[]]*4
 
91
        ctx['overlays'] = overlay_bits[0]
79
92
 
80
93
        ctx['styles'] = [media_url(req, CorePlugin, 'ivle.css')]
81
 
        ctx['styles'] += req.styles
 
94
        ctx['styles'] += view_styles
 
95
        ctx['styles'] += overlay_bits[1]
82
96
 
83
97
        ctx['scripts'] = [media_url(req, CorePlugin, path) for path in
84
98
                           ('util.js', 'json2.js', 'md5.js')]
85
99
        ctx['scripts'].append(media_url(req, '+external/jquery', 'jquery.js'))
86
 
        ctx['scripts'] += req.scripts
 
100
        ctx['scripts'] += view_scripts
 
101
        ctx['scripts'] += overlay_bits[2]
87
102
 
88
 
        ctx['scripts_init'] = req.scripts_init
 
103
        ctx['scripts_init'] = self.scripts_init + overlay_bits[3]
89
104
        ctx['app_template'] = app
90
105
        ctx['title_img'] = media_url(req, CorePlugin,
91
 
                                     "images/chrome/title.png")
 
106
                                     "images/chrome/root-breadcrumb.png")
 
107
        try:
 
108
            ctx['ancestry'] = self.get_context_ancestry(req)
 
109
        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
92
116
        self.populate_headings(req, ctx)
93
117
        tmpl = loader.load(os.path.join(os.path.dirname(__file__), 
94
118
                                                        'ivle-headings.html'))
149
173
        scripts_init.
150
174
        """
151
175
        overlays = []
 
176
        styles = []
 
177
        scripts = []
 
178
        scripts_init = []
152
179
        if not self.allow_overlays:
153
 
            return overlays
 
180
            return (overlays, styles, scripts, scripts_init)
154
181
 
155
182
        for plugin in req.config.plugin_index[OverlayPlugin]:
156
183
            for overclass in plugin.overlays:
160
187
                #TODO: Re-factor this to look nicer
161
188
                for mplugin in overlay.plugin_scripts:
162
189
                    for path in overlay.plugin_scripts[mplugin]:
163
 
                        req.scripts.append(media_url(req, mplugin, path))
 
190
                        scripts.append(media_url(req, mplugin, path))
164
191
 
165
192
                for mplugin in overlay.plugin_styles:
166
193
                    for path in overlay.plugin_styles[mplugin]:
167
 
                        req.styles.append(media_url(req, mplugin, path))
 
194
                        styles.append(media_url(req, mplugin, path))
168
195
 
169
 
                req.scripts_init += overlay.plugin_scripts_init
 
196
                scripts_init += overlay.plugin_scripts_init
170
197
 
171
198
                overlays.append(overlay.render(req))
172
 
        return overlays
 
199
        return (overlays, styles, scripts, scripts_init)
173
200
 
174
201
    @classmethod
175
202
    def get_error_view(cls, e):
182
209
class XHTMLErrorView(XHTMLView):
183
210
    template = 'xhtmlerror.html'
184
211
 
185
 
    def __init__(self, req, exception):
186
 
        self.context = exception
 
212
    def __init__(self, req, context, lastobj):
 
213
        super(XHTMLErrorView, self).__init__(req, context)
 
214
        self.lastobj = lastobj
 
215
 
 
216
    def get_context_ancestry(self, req):
 
217
        return req.publisher.get_ancestors(self.lastobj)
187
218
 
188
219
    def populate(self, req, ctx):
 
220
        ctx['req'] = req
189
221
        ctx['exception'] = self.context
190
222
 
191
223
class XHTMLUnauthorizedView(XHTMLErrorView):
192
224
    template = 'xhtmlunauthorized.html'
193
225
 
194
 
    def __init__(self, req, exception):
195
 
        super(XHTMLUnauthorizedView, self).__init__(req, exception)
 
226
    def __init__(self, req, exception, lastobj):
 
227
        super(XHTMLUnauthorizedView, self).__init__(req, exception, lastobj)
196
228
 
197
229
        if req.user is None:
198
230
            # Not logged in. Redirect to login page.