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

« back to all changes in this revision

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

Remove the setup.configure import from setup.

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
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):
 
31
import ivle.conf
 
32
import ivle.util
 
33
 
 
34
class XHTMLView(BaseView):
56
35
    """
57
36
    A view which provides a base class for views which need to return XHTML
58
37
    It is expected that apps which use this view will be written using Genshi
60
39
    """
61
40
 
62
41
    template = 'template.html'
63
 
    allow_overlays = True
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)
80
 
 
81
 
    def filter(self, stream, ctx):
82
 
        return stream
 
42
    plugin_scripts = {}
 
43
    plugin_styles = {}
 
44
    overlay_blacklist = []
 
45
 
 
46
    def __init__(self, req, **kwargs):
 
47
        for key in kwargs:
 
48
            setattr(self, key, kwargs[key])
83
49
 
84
50
    def render(self, req):
85
51
        req.content_type = 'text/html' # TODO: Detect application/xhtml+xml
92
58
        # view.
93
59
        app_template = os.path.join(os.path.dirname(
94
60
                        inspect.getmodule(self).__file__), self.template) 
95
 
        tmpl = self._loader.load(app_template)
96
 
        app = self.filter(tmpl.generate(viewctx), viewctx)
 
61
        req.write_html_head_foot = False
 
62
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
 
63
        tmpl = loader.load(app_template)
 
64
        app = tmpl.generate(viewctx)
97
65
 
98
 
        view_scripts = []
99
66
        for plugin in self.plugin_scripts:
100
67
            for path in self.plugin_scripts[plugin]:
101
 
                view_scripts.append(media_url(req, plugin, path))
 
68
                req.scripts.append(media_url(req, plugin, path))
102
69
 
103
 
        view_styles = []
104
70
        for plugin in self.plugin_styles:
105
71
            for path in self.plugin_styles[plugin]:
106
 
                view_styles.append(media_url(req, plugin, path))
 
72
                req.styles.append(media_url(req, plugin, path))
107
73
 
108
74
        # Global template
109
75
        ctx = genshi.template.Context()
110
 
 
111
 
        overlay_bits = self.render_overlays(req) if req.user else [[]]*4
112
 
        ctx['overlays'] = overlay_bits[0]
 
76
        # XXX: Leave this here!! (Before req.styles is read)
 
77
        ctx['overlays'] = self.render_overlays(req)
113
78
 
114
79
        ctx['styles'] = [media_url(req, CorePlugin, 'ivle.css')]
115
 
        ctx['styles'] += view_styles
116
 
        ctx['styles'] += overlay_bits[1]
 
80
        ctx['styles'] += req.styles
117
81
 
118
82
        ctx['scripts'] = [media_url(req, CorePlugin, path) for path in
119
83
                           ('util.js', 'json2.js', 'md5.js')]
120
 
        ctx['scripts'].append(media_url(req, '+external/jquery', 'jquery.js'))
121
 
        ctx['scripts'] += view_scripts
122
 
        ctx['scripts'] += overlay_bits[2]
 
84
        ctx['scripts'] += req.scripts
123
85
 
124
 
        ctx['scripts_init'] = self.scripts_init + overlay_bits[3]
 
86
        ctx['scripts_init'] = req.scripts_init
125
87
        ctx['app_template'] = app
126
 
        ctx['title_img'] = media_url(req, CorePlugin,
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
 
 
155
88
        self.populate_headings(req, ctx)
156
 
        tmpl = self._loader.load(os.path.join(os.path.dirname(__file__), 
 
89
        tmpl = loader.load(os.path.join(os.path.dirname(__file__), 
157
90
                                                        'ivle-headings.html'))
158
91
        req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))
159
92
        
162
95
 
163
96
    def populate_headings(self, req, ctx):
164
97
        ctx['favicon'] = None
165
 
        ctx['root_dir'] = req.config['urls']['root']
166
 
        ctx['public_host'] = req.config['urls']['public_host']
167
 
        ctx['svn_base'] = req.config['urls']['svn_addr']
 
98
        ctx['root_dir'] = ivle.conf.root_dir
 
99
        ctx['public_host'] = ivle.conf.public_host
168
100
        ctx['write_javascript_settings'] = req.write_javascript_settings
169
101
        if req.user:
170
102
            ctx['login'] = req.user.login
178
110
            ctx['help_path'] = self.help
179
111
 
180
112
        ctx['apps_in_tabs'] = []
181
 
        for plugin in req.config.plugin_index[ViewPlugin]:
 
113
        for plugin in req.plugin_index[ViewPlugin]:
182
114
            if not hasattr(plugin, 'tabs'):
183
115
                continue
184
116
 
185
117
            for tab in plugin.tabs:
186
 
                # tab is a tuple: name, title, desc, icon, path, weight, admin
187
 
                # (Admin is optional, defaults to false)
 
118
                # tab is a tuple: name, title, desc, icon, path
188
119
                new_app = {}
189
 
                new_app['this_app'] = hasattr(self, 'tab') \
190
 
                                      and tab[0] == self.tab
 
120
                new_app['this_app'] = hasattr(self, 'appname') \
 
121
                                      and tab[0] == self.appname
191
122
 
192
123
                # Icon name
193
124
                if tab[3] is not None:
198
129
                        ctx['favicon'] = icon_url
199
130
                else:
200
131
                    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
207
 
                new_app['path'] = req.make_path(tab[4])
 
132
                new_app['path'] = ivle.util.make_path(tab[4])
208
133
                new_app['desc'] = tab[2]
209
134
                new_app['name'] = tab[1]
210
135
                new_app['weight'] = tab[5]
219
144
        scripts_init.
220
145
        """
221
146
        overlays = []
222
 
        styles = []
223
 
        scripts = []
224
 
        scripts_init = []
225
 
        if not self.allow_overlays:
226
 
            return (overlays, styles, scripts, scripts_init)
227
 
 
228
 
        for plugin in req.config.plugin_index[OverlayPlugin]:
 
147
        for plugin in req.plugin_index[OverlayPlugin]:
229
148
            for overclass in plugin.overlays:
230
149
                if overclass in self.overlay_blacklist:
231
150
                    continue
233
152
                #TODO: Re-factor this to look nicer
234
153
                for mplugin in overlay.plugin_scripts:
235
154
                    for path in overlay.plugin_scripts[mplugin]:
236
 
                        scripts.append(media_url(req, mplugin, path))
 
155
                        req.scripts.append(media_url(req, mplugin, path))
237
156
 
238
157
                for mplugin in overlay.plugin_styles:
239
158
                    for path in overlay.plugin_styles[mplugin]:
240
 
                        styles.append(media_url(req, mplugin, path))
 
159
                        req.styles.append(media_url(req, mplugin, path))
241
160
 
242
 
                scripts_init += overlay.plugin_scripts_init
 
161
                req.scripts_init += overlay.plugin_scripts_init
243
162
 
244
163
                overlays.append(overlay.render(req))
245
 
        return (overlays, styles, scripts, scripts_init)
 
164
        return overlays
246
165
 
247
166
    @classmethod
248
167
    def get_error_view(cls, e):
255
174
class XHTMLErrorView(XHTMLView):
256
175
    template = 'xhtmlerror.html'
257
176
 
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)
 
177
    def __init__(self, req, exception):
 
178
        self.context = exception
264
179
 
265
180
    def populate(self, req, ctx):
266
 
        ctx['req'] = req
267
181
        ctx['exception'] = self.context
268
 
        req.headers_out['X-IVLE-Error'] = self.context.message
269
182
 
270
183
class XHTMLUnauthorizedView(XHTMLErrorView):
271
184
    template = 'xhtmlunauthorized.html'
272
185
 
273
 
    def __init__(self, req, exception, lastobj):
274
 
        super(XHTMLUnauthorizedView, self).__init__(req, exception, lastobj)
 
186
    def __init__(self, req, exception):
 
187
        super(XHTMLUnauthorizedView, self).__init__(req, exception)
275
188
 
276
 
        if not req.publicmode and req.user is None:
 
189
        if req.user is None:
277
190
            # Not logged in. Redirect to login page.
278
 
            if req.uri == '/':
279
 
                query_string = ''
280
 
            else:
281
 
                query_string = '?url=' + urllib.quote(req.uri, safe="/~")
282
 
            req.throw_redirect('/+login' + query_string)
 
191
            req.throw_redirect('/+login?' + 
 
192
                               urllib.urlencode([('url', req.uri)]))
283
193
 
284
194
        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