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

« back to all changes in this revision

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

Update example Apache config to use an entirely virtual hierarchy, placing
the mod_python directives inside a Location rather than a Directory.

The old Directory directives pointed at www/, which no longer exists.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
import inspect
21
21
import os.path
 
22
import urllib
22
23
 
23
24
import genshi.template
24
25
 
 
26
from ivle.webapp.media import media_url
 
27
from ivle.webapp.core import Plugin as CorePlugin
25
28
from ivle.webapp.base.views import BaseView
 
29
from ivle.webapp.base.plugins import ViewPlugin, OverlayPlugin
 
30
from ivle.webapp.errors import HTTPError, Unauthorized
26
31
import ivle.conf
27
32
import ivle.util
28
33
 
32
37
    It is expected that apps which use this view will be written using Genshi
33
38
    templates.
34
39
    """
 
40
 
 
41
    template = 'template.html'
 
42
    plugin_scripts = {}
 
43
    plugin_styles = {}
 
44
    allow_overlays = True
 
45
    overlay_blacklist = []
 
46
 
35
47
    def __init__(self, req, **kwargs):
36
48
        for key in kwargs:
37
 
          setattr(self, key, kwargs[key])
 
49
            setattr(self, key, kwargs[key])
38
50
 
39
51
    def render(self, req):
40
52
        req.content_type = 'text/html' # TODO: Detect application/xhtml+xml
52
64
        tmpl = loader.load(app_template)
53
65
        app = tmpl.generate(viewctx)
54
66
 
 
67
        for plugin in self.plugin_scripts:
 
68
            for path in self.plugin_scripts[plugin]:
 
69
                req.scripts.append(media_url(req, plugin, path))
 
70
 
 
71
        for plugin in self.plugin_styles:
 
72
            for path in self.plugin_styles[plugin]:
 
73
                req.styles.append(media_url(req, plugin, path))
 
74
 
55
75
        # Global template
56
76
        ctx = genshi.template.Context()
57
 
        ctx['app_styles'] = req.styles
58
 
        ctx['scripts'] = req.scripts
 
77
        # XXX: Leave this here!! (Before req.styles is read)
 
78
        ctx['overlays'] = self.render_overlays(req)
 
79
 
 
80
        ctx['styles'] = [media_url(req, CorePlugin, 'ivle.css')]
 
81
        ctx['styles'] += req.styles
 
82
 
 
83
        ctx['scripts'] = [media_url(req, CorePlugin, path) for path in
 
84
                           ('util.js', 'json2.js', 'md5.js')]
 
85
        ctx['scripts'] += req.scripts
 
86
 
59
87
        ctx['scripts_init'] = req.scripts_init
60
88
        ctx['app_template'] = app
61
89
        self.populate_headings(req, ctx)
62
90
        tmpl = loader.load(os.path.join(os.path.dirname(__file__), 
63
91
                                                        'ivle-headings.html'))
64
92
        req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))
 
93
        
 
94
    def populate(self, req, ctx):
 
95
        raise NotImplementedError()
65
96
 
66
97
    def populate_headings(self, req, ctx):
67
98
        ctx['favicon'] = None
74
105
            ctx['nick'] = req.user.nick
75
106
        else:
76
107
            ctx['login'] = None
 
108
            ctx['logged_in'] = False
77
109
        ctx['publicmode'] = req.publicmode
 
110
        if hasattr(self, 'help'):
 
111
            ctx['help_path'] = self.help
 
112
 
78
113
        ctx['apps_in_tabs'] = []
79
 
        for urlname in ivle.conf.apps.apps_in_tabs:
80
 
            new_app = {}
81
 
            app = ivle.conf.apps.app_url[urlname]
82
 
            new_app['this_app'] = hasattr(self, 'appname') \
83
 
                                  and urlname == self.appname
84
 
            if app.icon:
85
 
                new_app['has_icon'] = True
86
 
                icon_dir = ivle.conf.apps.app_icon_dir
87
 
                icon_url = ivle.util.make_path(os.path.join(icon_dir, app.icon))
88
 
                new_app['icon_url'] = icon_url
89
 
                if new_app['this_app']:
90
 
                    ctx['favicon'] = icon_url
91
 
            else:
92
 
                new_app['has_icon'] = False
93
 
            new_app['path'] = ivle.util.make_path(urlname)
94
 
            new_app['desc'] = app.desc
95
 
            new_app['name'] = app.name
96
 
            ctx['apps_in_tabs'].append(new_app)
 
114
        for plugin in req.config.plugin_index[ViewPlugin]:
 
115
            if not hasattr(plugin, 'tabs'):
 
116
                continue
 
117
 
 
118
            for tab in plugin.tabs:
 
119
                # tab is a tuple: name, title, desc, icon, path
 
120
                new_app = {}
 
121
                new_app['this_app'] = hasattr(self, 'appname') \
 
122
                                      and tab[0] == self.appname
 
123
 
 
124
                # Icon name
 
125
                if tab[3] is not None:
 
126
                    new_app['has_icon'] = True
 
127
                    icon_url = media_url(req, plugin, tab[3])
 
128
                    new_app['icon_url'] = icon_url
 
129
                    if new_app['this_app']:
 
130
                        ctx['favicon'] = icon_url
 
131
                else:
 
132
                    new_app['has_icon'] = False
 
133
                new_app['path'] = ivle.util.make_path(tab[4])
 
134
                new_app['desc'] = tab[2]
 
135
                new_app['name'] = tab[1]
 
136
                new_app['weight'] = tab[5]
 
137
                ctx['apps_in_tabs'].append(new_app)
 
138
 
 
139
        ctx['apps_in_tabs'].sort(key=lambda tab: tab['weight'])
 
140
 
 
141
    def render_overlays(self, req):
 
142
        """Generate XML streams for the overlays.
 
143
        
 
144
        Returns a list of streams. Populates the scripts, styles, and 
 
145
        scripts_init.
 
146
        """
 
147
        overlays = []
 
148
        if not self.allow_overlays:
 
149
            return overlays
 
150
 
 
151
        for plugin in req.config.plugin_index[OverlayPlugin]:
 
152
            for overclass in plugin.overlays:
 
153
                if overclass in self.overlay_blacklist:
 
154
                    continue
 
155
                overlay = overclass(req)
 
156
                #TODO: Re-factor this to look nicer
 
157
                for mplugin in overlay.plugin_scripts:
 
158
                    for path in overlay.plugin_scripts[mplugin]:
 
159
                        req.scripts.append(media_url(req, mplugin, path))
 
160
 
 
161
                for mplugin in overlay.plugin_styles:
 
162
                    for path in overlay.plugin_styles[mplugin]:
 
163
                        req.styles.append(media_url(req, mplugin, path))
 
164
 
 
165
                req.scripts_init += overlay.plugin_scripts_init
 
166
 
 
167
                overlays.append(overlay.render(req))
 
168
        return overlays
 
169
 
 
170
    @classmethod
 
171
    def get_error_view(cls, e):
 
172
        view_map = {HTTPError:    XHTMLErrorView,
 
173
                    Unauthorized: XHTMLUnauthorizedView}
 
174
        for exccls in inspect.getmro(type(e)):
 
175
            if exccls in view_map:
 
176
                return view_map[exccls]
 
177
 
 
178
class XHTMLErrorView(XHTMLView):
 
179
    template = 'xhtmlerror.html'
 
180
 
 
181
    def __init__(self, req, exception):
 
182
        self.context = exception
 
183
 
 
184
    def populate(self, req, ctx):
 
185
        ctx['exception'] = self.context
 
186
 
 
187
class XHTMLUnauthorizedView(XHTMLErrorView):
 
188
    template = 'xhtmlunauthorized.html'
 
189
 
 
190
    def __init__(self, req, exception):
 
191
        super(XHTMLUnauthorizedView, self).__init__(req, exception)
 
192
 
 
193
        if req.user is None:
 
194
            # Not logged in. Redirect to login page.
 
195
            req.throw_redirect('/+login?' + 
 
196
                               urllib.urlencode([('url', req.uri)]))
 
197
 
 
198
        req.status = 403