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

« back to all changes in this revision

Viewing changes to www/apps/subjects/__init__.py

  • Committer: chadnickbok
  • Date: 2009-02-02 04:00:25 UTC
  • Revision ID: svn-v4:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1189
Adding the changes from my genshi branch into trunk.

Most apps now use the Genshi templating engine, in preparation
for future changes to dispatch

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
import ivle.database
30
30
from ivle import util
31
31
 
 
32
import genshi
 
33
import genshi.template
 
34
 
32
35
def handle(req):
33
36
    """Handler for the Subjects application. Links to subject home pages."""
34
37
 
35
38
    req.styles = ["media/subjects/subjects.css"]
 
39
    ctx = genshi.template.Context()
36
40
    if req.path == "":
37
 
        handle_toplevel_menu(req)
 
41
        # This is represented as a directory. Redirect and add a slash if it is
 
42
        # missing.
 
43
        if req.uri[-1] != '/':
 
44
            req.throw_redirect(req.uri + '/')
 
45
        ctx['whichpage'] = "toplevel"
 
46
        handle_toplevel_menu(req, ctx)
38
47
    else:
39
 
        handle_subject_page(req, req.path)
 
48
        ctx['whichpage'] = "subject"
 
49
        handle_subject_page(req, req.path, ctx)
 
50
        
 
51
    loader = genshi.template.TemplateLoader(".", auto_reload=True)
 
52
    tmpl = loader.load(util.make_local_path("apps/subjects/template.html"))
 
53
    req.write(tmpl.generate(ctx).render('html')) #'xhtml', doctype='xhtml'))
40
54
 
41
 
def handle_toplevel_menu(req):
42
 
    # This is represented as a directory. Redirect and add a slash if it is
43
 
    # missing.
44
 
    if req.uri[-1] != '/':
45
 
        req.throw_redirect(req.uri + '/')
 
55
def handle_toplevel_menu(req, ctx):
46
56
 
47
57
    enrolled_subjects = req.user.subjects
48
58
    unenrolled_subjects = [subject for subject in
49
59
                           req.store.find(ivle.database.Subject)
50
60
                           if subject not in enrolled_subjects]
51
61
 
52
 
    def print_subject(subject):
53
 
        if subject.url is None:
54
 
            req.write('  <li>%s (no home page)</li>\n'
55
 
                % cgi.escape(subject.name))
56
 
        else:
57
 
            req.write('  <li><a href="%s">%s</a></li>\n'
58
 
                % (cgi.escape(subject.url),
59
 
                   cgi.escape(subject.name)))
 
62
    ctx['enrolled_subjects'] = []
 
63
    ctx['other_subjects'] = []
60
64
 
61
65
    req.content_type = "text/html"
62
66
    req.write_html_head_foot = True
63
 
    req.write('<div id="ivle_padding">\n')
64
 
    req.write("<h2>IVLE Subject Homepages</h2>\n")
65
 
    req.write("<h2>Subjects</h2>\n<ul>\n")
 
67
 
66
68
    for subject in enrolled_subjects:
67
 
        print_subject(subject)
68
 
    req.write("</ul>\n")
 
69
        new_subj = {}
 
70
        new_subj['name'] = subject.name
 
71
        new_subj['url'] = subject.url
 
72
        ctx['enrolled_subjects'].append(new_subj)
 
73
 
69
74
    if len(unenrolled_subjects) > 0:
70
 
        req.write("<h3>Other Subjects</h3>\n")
71
 
        req.write("<p>You are not currently enrolled in these subjects</p>\n")
72
 
        req.write("<ul>\n")
73
75
        for subject in unenrolled_subjects:
74
 
            print_subject(subject)
75
 
        req.write("</ul>\n")
76
 
    req.write("</div>\n")
77
 
 
78
 
def handle_subject_page(req, path):
 
76
            new_subj = {}
 
77
            new_subj['name'] = subject.name
 
78
            new_subj['url'] = subject.url
 
79
            ctx['other_subjects'].append(new_subj)
 
80
 
 
81
 
 
82
def handle_subject_page(req, path, ctx):
79
83
    req.content_type = "text/html"
80
84
    req.write_html_head_foot = True     # Have dispatch print head and foot
81
85
 
82
86
    # Just make the iframe pointing to media/subjects
83
 
    serve_loc = util.make_path(os.path.join('media', 'subjects', path))
84
 
    req.write('<object class="fullscreen" type="text/html" \
85
 
data="%s"></iframe>'% urllib.quote(serve_loc))
 
87
    ctx['serve_loc'] = urllib.quote(util.make_path(os.path.join('media', 'subjects', path)))