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

« back to all changes in this revision

Viewing changes to www/dispatch/html.py

  • Committer: matt.giuca
  • Date: 2009-01-12 01:03:59 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1077
Reverted revisions 1059 and 1069 - renaming the DB user from 'postgres' to
    'ivleuser'.
    Heart was in the right place, but currently doesn't work because postgres
    owns the relations and we've no way to correct that yet (since we manually
    run the .sql script as postgres).

    This will be reverted back again once that's solved.

Show diffs side-by-side

added added

removed removed

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