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

« back to all changes in this revision

Viewing changes to www/dispatch/html.py

  • Committer: wagrant
  • Date: 2008-09-20 07:02:28 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1053
www.apps.server: Bail out early if the user doesn't exist. This
                 has frequently been crashing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# IVLE - Informatics Virtual Learning Environment
 
2
# Copyright (C) 2007-2008 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
# Module: dispatch.html
 
19
# Author: Matt Giuca
 
20
# Date: 12/12/2007
 
21
 
 
22
# Provides functions for writing the dispatch-generated HTML header and footer
 
23
# content (the common parts of the HTML pages shared across the entire site).
 
24
# Does not include the login page. See login.py.
 
25
 
 
26
import cgi
 
27
import urllib
 
28
import os.path
 
29
 
 
30
import conf
 
31
import conf.apps
 
32
from common import util
 
33
import plugins.console
 
34
 
 
35
def write_html_head(req):
 
36
    """Writes the HTML header, given a request object.
 
37
 
 
38
    req: An IVLE request object. Reads attributes such as title. Also used to
 
39
    write to."""
 
40
 
 
41
    # Write the XHTML opening and head element
 
42
    # Note the inline JavaScript, which provides the client with constants
 
43
    # derived from the server configuration.
 
44
    if req.title != None:
 
45
        titlepart = req.title + ' - '
 
46
    else:
 
47
        titlepart = ''
 
48
    req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 
49
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
50
<html xmlns="http://www.w3.org/1999/xhtml">
 
51
<head>
 
52
  <title>%sIVLE</title>
 
53
  <meta http-equiv="Content-Type" content="%s; charset=utf-8" />
 
54
""" % (cgi.escape(titlepart), cgi.escape(req.content_type)))
 
55
    # Write inline JavaScript which gives the client code access to certain
 
56
    # server-side variables.
 
57
    if req.user:
 
58
        username = repr(req.user.login)
 
59
    else:
 
60
        username = "null"
 
61
    req.write("""  <script type="text/javascript">
 
62
    root_dir = %s;
 
63
    public_host = %s;
 
64
    username = %s;
 
65
  </script>
 
66
""" % (repr(conf.root_dir), repr(conf.public_host), username))
 
67
    iconurl = get_icon_url(req.app, small=True)
 
68
    if iconurl:
 
69
        req.write("""  <link rel="shortcut icon" href="%s" />
 
70
""" % cgi.escape(iconurl))
 
71
    req.write("""  <link rel="stylesheet" type="text/css" href="%s" />
 
72
""" % cgi.escape(util.make_path('media/common/ivle.css')))
 
73
 
 
74
    # Write any app-specific style and script links
 
75
    for style in req.styles:
 
76
        req.write('  <link rel="stylesheet" type="text/css" href="%s" />\n'
 
77
            % cgi.escape(util.make_path(style)))
 
78
    for script in req.scripts:
 
79
        req.write('  <script type="text/javascript" src="%s"></script>\n'
 
80
            % cgi.escape(util.make_path(script)))
 
81
    req.write('  <script type="text/javascript">\n    /* Init Functions */\n')
 
82
    for init in req.scripts_init:
 
83
        req.write('    window.addEventListener("load", %s, false);\n'%init)
 
84
    req.write('  </script>\n')
 
85
 
 
86
    req.write("</head>\n\n")
 
87
 
 
88
    # Open the body element and write a bunch of stuff there (the header)
 
89
    req.write("""<body>
 
90
<div id="ivleheader"></div>
 
91
<div id="ivleheader_text">
 
92
  <h1>IVLE</h1>
 
93
  <h2>Informatics Virtual Learning Environment</h2>
 
94
""")
 
95
 
 
96
    if req.publicmode:
 
97
        req.write('   <p class="userhello">Running in public mode.</p>')
 
98
    elif req.user:
 
99
        # Get the user's nickname from the request session
 
100
        nickname = req.user.nick
 
101
        req.write('  <p class="userhello"><span id="usernick">%s</span> '
 
102
            '(<span class="username">%s</span>) |\n'
 
103
            '    <a href="%s">Settings</a> |\n'
 
104
            '    <a href="%s">Help</a> |\n'
 
105
            '    <a href="%s">Sign out</a>\n'
 
106
            '  </p>\n' %
 
107
            (cgi.escape(nickname), cgi.escape(req.user.login),
 
108
             cgi.escape(util.make_path('settings')),
 
109
             cgi.escape(get_help_url(req)),
 
110
             cgi.escape(util.make_path('logout'))))
 
111
    else:
 
112
        req.write('  <p class="userhello">Not logged in.</p>')
 
113
 
 
114
    # ivleheader_tabs is a separate div, so it can be positioned absolutely
 
115
    req.write('</div>\n<div id="ivleheader_tabs">\n')
 
116
 
 
117
    # If the "debuginfo" app is installed, display a warning to the admin to
 
118
    # make sure it is removed in production.
 
119
    if "debuginfo" in conf.apps.app_url:
 
120
        req.write("  <p><small>Warning: debuginfo is enabled. Set "
 
121
            "enable_debuginfo = False in lib/conf/apps.py, when placing IVLE "
 
122
            "into production.</small></p>\n")
 
123
 
 
124
    # If req has a "no_agreement" attribute, then it is because the user has
 
125
    # not signed the agreement; therefore we are displaying the TOS page.
 
126
    # Do not show apps (see dispatch.login).
 
127
    if req.user and not req.user.state == 'no_agreement':
 
128
        # Only print app tabs if logged in
 
129
        print_apps_list(req, req.app)
 
130
    req.write('</div>\n<div id="ivlebody">\n')
 
131
 
 
132
def write_html_foot(req):
 
133
    """Writes the HTML footer, given a request object.
 
134
 
 
135
    req: An IVLE request object. Written to.
 
136
    """
 
137
    req.write("</div>\n</body>\n</html>\n")
 
138
 
 
139
def get_help_url(req):
 
140
    """Gets the help URL most relevant to this page, to place as the
 
141
    "help" link at the top of the page."""
 
142
    reqapp = req.app if hasattr(req, 'app') else None
 
143
    if reqapp == 'help':
 
144
        # We're already in help. Link to the exact current page
 
145
        # instead of the generic help page.
 
146
        return req.uri
 
147
    if reqapp is not None and reqapp in conf.apps.app_url and \
 
148
        conf.apps.app_url[reqapp].hashelp:
 
149
        help_path = os.path.join('help', reqapp)
 
150
    else:
 
151
        help_path = 'help'
 
152
    return util.make_path(help_path)
 
153
 
 
154
def get_icon_url(appurl, small=False):
 
155
    """Given an app's url name, gets the URL of the icon image for this app,
 
156
    relative to the site root. Returns None if the app has no icon."""
 
157
    if appurl is None: return None
 
158
    try:
 
159
        app = conf.apps.app_url[appurl]
 
160
    except KeyError:
 
161
        # Due to navigating to a bad app
 
162
        return None
 
163
    if small:
 
164
        icon_dir = conf.apps.app_icon_dir_small
 
165
    else:
 
166
        icon_dir = conf.apps.app_icon_dir
 
167
    if app.icon is None: return None
 
168
    return util.make_path(os.path.join(icon_dir, app.icon))
 
169
 
 
170
def print_apps_list(file, thisapp):
 
171
    """Prints all app tabs, as a UL. Prints a list item for each app that has
 
172
    a tab.
 
173
 
 
174
    file: Object with a "write" method - ie. the request object.
 
175
    Reads from: conf
 
176
    """
 
177
    file.write('  <ul id="apptabs">\n')
 
178
 
 
179
    for urlname in conf.apps.apps_in_tabs:
 
180
        app = conf.apps.app_url[urlname]
 
181
        if urlname == thisapp:
 
182
            li_attr = ' class="thisapp"'
 
183
        else:
 
184
            li_attr = ''
 
185
        file.write('    <li%s>' % li_attr)
 
186
        if app.icon:
 
187
            file.write('<img src="%s" alt="" /> '
 
188
                % urllib.quote(get_icon_url(urlname)))
 
189
        file.write('<a href="%s" title="%s">%s</a></li>\n'
 
190
            % (urllib.quote(util.make_path(urlname)), cgi.escape(app.desc),
 
191
                cgi.escape(app.name)))
 
192
 
 
193
    file.write('  </ul>\n')