~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: 2009-02-24 02:02:03 UTC
  • mto: This revision was merged to the branch mainline in revision 1119.
  • Revision ID: matt.giuca@gmail.com-20090224020203-aqdcjnsj6y7wl32o
Added a new look to the IVLE header bar. Mmmm... Web 2.0.
Added top-level directory image-source, containing SVG and script files for
    generating the images.
ivle/webapp/coremedia/images: Added 'chrome' directory containing the rendered
    images.
Modified ivle/webapp/base/ivle-headings.html and
    ivle/webapp/coremedia/ivle.css to support the new images.
    Note that the H1 and H2 at the top of the page are no longer displayed
    (and their custom styles have been removed). There is a heading image
    instead.

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
import ivle.conf
 
32
import ivle.util
33
33
 
34
34
class XHTMLView(BaseView):
35
35
    """
39
39
    """
40
40
 
41
41
    template = 'template.html'
 
42
    plugin_scripts = {}
 
43
    plugin_styles = {}
42
44
    allow_overlays = True
43
 
    breadcrumb_text = None
44
 
 
45
 
    def __init__(self, *args, **kwargs):
46
 
        super(XHTMLView, self).__init__(*args, **kwargs)
47
 
 
48
 
        self.overlay_blacklist = []
49
 
 
50
 
        self.plugin_scripts = {}
51
 
        self.plugin_styles = {}
52
 
        self.scripts_init = []
53
 
 
54
 
        self.extra_breadcrumbs = []
55
 
        self.overlay_blacklist = []
56
 
 
57
 
    def get_context_ancestry(self, req):
58
 
        return req.publisher.get_ancestors(self.context)
59
 
 
60
 
    def filter(self, stream, ctx):
61
 
        return stream
 
45
    overlay_blacklist = []
 
46
 
 
47
    def __init__(self, req, **kwargs):
 
48
        for key in kwargs:
 
49
            setattr(self, key, kwargs[key])
62
50
 
63
51
    def render(self, req):
64
52
        req.content_type = 'text/html' # TODO: Detect application/xhtml+xml
73
61
                        inspect.getmodule(self).__file__), self.template) 
74
62
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
75
63
        tmpl = loader.load(app_template)
76
 
        app = self.filter(tmpl.generate(viewctx), viewctx)
 
64
        app = tmpl.generate(viewctx)
77
65
 
78
 
        view_scripts = []
79
66
        for plugin in self.plugin_scripts:
80
67
            for path in self.plugin_scripts[plugin]:
81
 
                view_scripts.append(media_url(req, plugin, path))
 
68
                req.scripts.append(media_url(req, plugin, path))
82
69
 
83
 
        view_styles = []
84
70
        for plugin in self.plugin_styles:
85
71
            for path in self.plugin_styles[plugin]:
86
 
                view_styles.append(media_url(req, plugin, path))
 
72
                req.styles.append(media_url(req, plugin, path))
87
73
 
88
74
        # Global template
89
75
        ctx = genshi.template.Context()
90
 
 
91
 
        overlay_bits = self.render_overlays(req) if req.user else [[]]*4
92
 
        ctx['overlays'] = overlay_bits[0]
 
76
        # XXX: Leave this here!! (Before req.styles is read)
 
77
        ctx['overlays'] = self.render_overlays(req) if req.user else []
93
78
 
94
79
        ctx['styles'] = [media_url(req, CorePlugin, 'ivle.css')]
95
 
        ctx['styles'] += view_styles
96
 
        ctx['styles'] += overlay_bits[1]
 
80
        ctx['styles'] += req.styles
97
81
 
98
82
        ctx['scripts'] = [media_url(req, CorePlugin, path) for path in
99
83
                           ('util.js', 'json2.js', 'md5.js')]
100
 
        ctx['scripts'].append(media_url(req, '+external/jquery', 'jquery.js'))
101
 
        ctx['scripts'] += view_scripts
102
 
        ctx['scripts'] += overlay_bits[2]
 
84
        ctx['scripts'] += req.scripts
103
85
 
104
 
        ctx['scripts_init'] = self.scripts_init + overlay_bits[3]
 
86
        ctx['scripts_init'] = req.scripts_init
105
87
        ctx['app_template'] = app
106
 
        ctx['title_img'] = media_url(req, CorePlugin,
107
 
                                     "images/chrome/root-breadcrumb.png")
108
 
        try:
109
 
            ancestry = self.get_context_ancestry(req)
110
 
        except NoPath:
111
 
            ancestry = []
112
 
 
113
 
        crumber = Breadcrumber(req)
114
 
 
115
 
        ctx['breadcrumbs'] = []
116
 
        for ancestor in ancestry:
117
 
            crumb = crumber.crumb(ancestor)
118
 
            if crumb is None:
119
 
                continue
120
 
 
121
 
            if hasattr(crumb, 'extra_breadcrumbs_before'):
122
 
                ctx['breadcrumbs'].extend(crumb.extra_breadcrumbs_before)
123
 
            ctx['breadcrumbs'].append(crumb)
124
 
            if hasattr(crumb, 'extra_breadcrumbs_after'):
125
 
                ctx['breadcrumbs'].extend(crumb.extra_breadcrumbs_after)
126
 
 
127
 
        # If the view has specified text for a breadcrumb, add one.
128
 
        if self.breadcrumb_text:
129
 
            ctx['breadcrumbs'].append(ViewBreadcrumb(req, self))
130
 
 
131
 
        # Allow the view to add its own fake breadcrumbs.
132
 
        ctx['breadcrumbs'].extend(self.extra_breadcrumbs)
133
 
 
134
88
        self.populate_headings(req, ctx)
135
89
        tmpl = loader.load(os.path.join(os.path.dirname(__file__), 
136
90
                                                        'ivle-headings.html'))
141
95
 
142
96
    def populate_headings(self, req, ctx):
143
97
        ctx['favicon'] = None
144
 
        ctx['root_dir'] = req.config['urls']['root']
145
 
        ctx['public_host'] = req.config['urls']['public_host']
146
 
        ctx['svn_base'] = req.config['urls']['svn_addr']
 
98
        ctx['root_dir'] = ivle.conf.root_dir
 
99
        ctx['public_host'] = ivle.conf.public_host
147
100
        ctx['write_javascript_settings'] = req.write_javascript_settings
148
101
        if req.user:
149
102
            ctx['login'] = req.user.login
164
117
            for tab in plugin.tabs:
165
118
                # tab is a tuple: name, title, desc, icon, path
166
119
                new_app = {}
167
 
                new_app['this_app'] = hasattr(self, 'tab') \
168
 
                                      and tab[0] == self.tab
 
120
                new_app['this_app'] = hasattr(self, 'appname') \
 
121
                                      and tab[0] == self.appname
169
122
 
170
123
                # Icon name
171
124
                if tab[3] is not None:
176
129
                        ctx['favicon'] = icon_url
177
130
                else:
178
131
                    new_app['has_icon'] = False
179
 
                new_app['path'] = req.make_path(tab[4])
 
132
                new_app['path'] = ivle.util.make_path(tab[4])
180
133
                new_app['desc'] = tab[2]
181
134
                new_app['name'] = tab[1]
182
135
                new_app['weight'] = tab[5]
191
144
        scripts_init.
192
145
        """
193
146
        overlays = []
194
 
        styles = []
195
 
        scripts = []
196
 
        scripts_init = []
197
147
        if not self.allow_overlays:
198
 
            return (overlays, styles, scripts, scripts_init)
 
148
            return overlays
199
149
 
200
150
        for plugin in req.config.plugin_index[OverlayPlugin]:
201
151
            for overclass in plugin.overlays:
205
155
                #TODO: Re-factor this to look nicer
206
156
                for mplugin in overlay.plugin_scripts:
207
157
                    for path in overlay.plugin_scripts[mplugin]:
208
 
                        scripts.append(media_url(req, mplugin, path))
 
158
                        req.scripts.append(media_url(req, mplugin, path))
209
159
 
210
160
                for mplugin in overlay.plugin_styles:
211
161
                    for path in overlay.plugin_styles[mplugin]:
212
 
                        styles.append(media_url(req, mplugin, path))
 
162
                        req.styles.append(media_url(req, mplugin, path))
213
163
 
214
 
                scripts_init += overlay.plugin_scripts_init
 
164
                req.scripts_init += overlay.plugin_scripts_init
215
165
 
216
166
                overlays.append(overlay.render(req))
217
 
        return (overlays, styles, scripts, scripts_init)
 
167
        return overlays
218
168
 
219
169
    @classmethod
220
170
    def get_error_view(cls, e):
227
177
class XHTMLErrorView(XHTMLView):
228
178
    template = 'xhtmlerror.html'
229
179
 
230
 
    def __init__(self, req, context, lastobj):
231
 
        super(XHTMLErrorView, self).__init__(req, context)
232
 
        self.lastobj = lastobj
233
 
 
234
 
    def get_context_ancestry(self, req):
235
 
        return req.publisher.get_ancestors(self.lastobj)
 
180
    def __init__(self, req, exception):
 
181
        self.context = exception
236
182
 
237
183
    def populate(self, req, ctx):
238
 
        ctx['req'] = req
239
184
        ctx['exception'] = self.context
240
185
 
241
186
class XHTMLUnauthorizedView(XHTMLErrorView):
242
187
    template = 'xhtmlunauthorized.html'
243
188
 
244
 
    def __init__(self, req, exception, lastobj):
245
 
        super(XHTMLUnauthorizedView, self).__init__(req, exception, lastobj)
 
189
    def __init__(self, req, exception):
 
190
        super(XHTMLUnauthorizedView, self).__init__(req, exception)
246
191
 
247
 
        if not req.publicmode and req.user is None:
 
192
        if req.user is None:
248
193
            # Not logged in. Redirect to login page.
249
194
            if req.uri == '/':
250
195
                query_string = ''
253
198
            req.throw_redirect('/+login' + query_string)
254
199
 
255
200
        req.status = 403
256
 
 
257
 
class ViewBreadcrumb(object):
258
 
    def __init__(self, req, context):
259
 
        self.req = req
260
 
        self.context = context
261
 
 
262
 
    @property
263
 
    def text(self):
264
 
        return self.context.breadcrumb_text