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

« back to all changes in this revision

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

  • Committer: mattgiuca
  • Date: 2008-08-18 12:15:25 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1027
Tutorial: Added new feature - previous attempt viewing. Allows users to see
    code they have previously submitted to tutorials.
    A new button ("View previous attempts") appears on each exercise box.
    This uses the getattempts and getattempt Ajax services checked in
    previously.
Note once again: Students are not (for the moment) able to see deactivated
attempts (this is a conservative approach - the ability to see deactivated
attempts can be turned on by setting HISTORY_ALLOW_INACTIVE = True in
tutorialservice).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# IVLE - Informatics Virtual Learning Environment
2
 
# Copyright (C) 2007-2009 The University of Melbourne
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
 
 
18
 
# Author: Nick Chadwick
19
 
 
20
 
import inspect
21
 
import os.path
22
 
 
23
 
import genshi.template
24
 
 
25
 
from ivle.webapp.media import media_url
26
 
from ivle.webapp.base.views import BaseView
27
 
from ivle.webapp.base.plugins import OverlayPlugin
28
 
import ivle.conf
29
 
import ivle.util
30
 
 
31
 
class XHTMLView(BaseView):
32
 
    """
33
 
    A view which provides a base class for views which need to return XHTML
34
 
    It is expected that apps which use this view will be written using Genshi
35
 
    templates.
36
 
    """
37
 
 
38
 
    template = 'template.html'
39
 
    plugin_scripts = {}
40
 
    plugin_styles = {}
41
 
    overlay_blacklist = []
42
 
 
43
 
    def __init__(self, req, **kwargs):
44
 
        for key in kwargs:
45
 
            setattr(self, key, kwargs[key])
46
 
 
47
 
    def render(self, req):
48
 
        req.content_type = 'text/html' # TODO: Detect application/xhtml+xml
49
 
 
50
 
        # View template
51
 
        viewctx = genshi.template.Context()
52
 
        self.populate(req, viewctx)
53
 
 
54
 
        # The template is found in the directory of the module containing the
55
 
        # view.
56
 
        app_template = os.path.join(os.path.dirname(
57
 
                        inspect.getmodule(self).__file__), self.template) 
58
 
        req.write_html_head_foot = False
59
 
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
60
 
        tmpl = loader.load(app_template)
61
 
        app = tmpl.generate(viewctx)
62
 
 
63
 
        for plugin in self.plugin_scripts:
64
 
            for path in self.plugin_scripts[plugin]:
65
 
                req.scripts.append(media_url(req, plugin, path))
66
 
 
67
 
        for plugin in self.plugin_styles:
68
 
            for path in self.plugin_styles[plugin]:
69
 
                req.styles.append(media_url(req, plugin, path))
70
 
 
71
 
        # Global template
72
 
        ctx = genshi.template.Context()
73
 
        # XXX: Leave this here!! (Before req.styles is read)
74
 
        ctx['overlays'] = self.render_overlays(req)
75
 
        ctx['app_styles'] = req.styles
76
 
        ctx['scripts'] = req.scripts
77
 
        ctx['scripts_init'] = req.scripts_init
78
 
        ctx['app_template'] = app
79
 
        self.populate_headings(req, ctx)
80
 
        tmpl = loader.load(os.path.join(os.path.dirname(__file__), 
81
 
                                                        'ivle-headings.html'))
82
 
        req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))
83
 
        
84
 
    def populate(self, req, ctx):
85
 
        raise NotImplementedError()
86
 
 
87
 
    def populate_headings(self, req, ctx):
88
 
        ctx['favicon'] = None
89
 
        ctx['root_dir'] = ivle.conf.root_dir
90
 
        ctx['public_host'] = ivle.conf.public_host
91
 
        ctx['write_javascript_settings'] = req.write_javascript_settings
92
 
        if req.user:
93
 
            ctx['login'] = req.user.login
94
 
            ctx['logged_in'] = True
95
 
            ctx['nick'] = req.user.nick
96
 
        else:
97
 
            ctx['login'] = None
98
 
        ctx['publicmode'] = req.publicmode
99
 
        ctx['apps_in_tabs'] = []
100
 
        for urlname in ivle.conf.apps.apps_in_tabs:
101
 
            new_app = {}
102
 
            app = ivle.conf.apps.app_url[urlname]
103
 
            new_app['this_app'] = hasattr(self, 'appname') \
104
 
                                  and urlname == self.appname
105
 
            if app.icon:
106
 
                new_app['has_icon'] = True
107
 
                icon_dir = ivle.conf.apps.app_icon_dir
108
 
                icon_url = ivle.util.make_path(os.path.join(icon_dir, app.icon))
109
 
                new_app['icon_url'] = icon_url
110
 
                if new_app['this_app']:
111
 
                    ctx['favicon'] = icon_url
112
 
            else:
113
 
                new_app['has_icon'] = False
114
 
            new_app['path'] = ivle.util.make_path(urlname)
115
 
            new_app['desc'] = app.desc
116
 
            new_app['name'] = app.name
117
 
            ctx['apps_in_tabs'].append(new_app)
118
 
            
119
 
    def render_overlays(self, req):
120
 
        """Generate XML streams for the overlays.
121
 
        
122
 
        Returns a list of streams. Populates the scripts, styles, and 
123
 
        scripts_init.
124
 
        """
125
 
        overlays = []
126
 
        for plugin in req.plugin_index[OverlayPlugin]:
127
 
            for overclass in plugin.overlays:
128
 
                if overclass in self.overlay_blacklist:
129
 
                    continue
130
 
                overlay = overclass(req)
131
 
                #TODO: Re-factor this to look nicer
132
 
                for mplugin in overlay.plugin_scripts:
133
 
                    for path in overlay.plugin_scripts[mplugin]:
134
 
                        req.scripts.append(media_url(req, mplugin, path))
135
 
 
136
 
                for mplugin in overlay.plugin_styles:
137
 
                    for path in overlay.plugin_styles[mplugin]:
138
 
                        req.styles.append(media_url(req, mplugin, path))
139
 
                
140
 
                req.scripts_init += overlay.plugin_scripts_init
141
 
                
142
 
                overlays.append(overlay.render(req))
143
 
        return overlays