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

« back to all changes in this revision

Viewing changes to www/apps/groups/__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:
27
27
 
28
28
from ivle import caps
29
29
from ivle.database import Subject
 
30
from ivle import util
 
31
 
 
32
import genshi
 
33
import genshi.core
 
34
import genshi.template
30
35
 
31
36
def handle(req):
32
37
    # Set request attributes
39
44
    ]
40
45
    req.write_html_head_foot = True     # Have dispatch print head and foot
41
46
 
42
 
    req.write('<div id="ivle_padding">\n')
43
 
 
 
47
    ctx = genshi.template.Context()
 
48
    
 
49
    ctx['enrolments'] = []
44
50
    # Show a group panel per enrolment
45
51
    enrolments = req.user.active_enrolments
46
52
    if enrolments.count() == 0:
47
 
        req.write("<p>Error: You are not currently enrolled in any subjects."
48
 
                  "</p>\n")
 
53
        ctx['no_enrolments'] = True
 
54
    else:
 
55
        ctx['no_enrolments'] = False
 
56
    
49
57
    for enrolment in enrolments:
50
 
        show_subject_panel(req, enrolment.offering)
 
58
        add_subject_panel(req, enrolment.offering, ctx)
 
59
        
51
60
    if req.user.hasCap(caps.CAP_MANAGEGROUPS):
52
 
        show_groupadmin_panel(req)
53
 
 
54
 
    req.write("</div>\n")
55
 
 
56
 
def show_groupadmin_panel(req):
57
 
    """
58
 
    Shows the group admin panel
59
 
    """
60
 
    req.write("<hr/>\n")
61
 
    req.write("<h1>Group Administration</h1>")
62
 
    # Choose subject
63
 
    subjects = req.store.find(Subject)
64
 
    req.write("<label for=\"subject_select\">Subject:</label>\n")
65
 
    req.write("<select id=\"subject_select\">\n")
66
 
    for s in subjects:
67
 
        req.write("    <option value=\"%d\">%s (%s)</option>\n"%
68
 
            (s.id, s.name, s.code))
69
 
    req.write("</select>\n")
70
 
    req.write("<input type=\"button\" value=\"Manage\" \
71
 
        onclick=\"manage_subject()\" />\n")
72
 
    req.write("<div id=\"subject_div\"></div>")
73
 
 
74
 
def show_subject_panel(req, offering):
 
61
        ctx['manage_groups'] = True
 
62
        ctx['manage_subjects'] = []
 
63
        subjects = req.store.find(Subject)
 
64
        for s in subjects:
 
65
            new_s = {}
 
66
            new_s['id'] = s.id
 
67
            new_s['name'] = s.name
 
68
            new_s['code'] = s.code
 
69
            ctx['manage_subjects'].append(new_s)
 
70
    else:
 
71
        ctx['manage_groups'] = False
 
72
  
 
73
    loader = genshi.template.TemplateLoader(".", auto_reload=True)
 
74
    tmpl = loader.load(util.make_local_path("apps/groups/template.html"))
 
75
    
 
76
    req.write(tmpl.generate(ctx).render('html'))
 
77
 
 
78
def add_subject_panel(req, offering, ctx):
75
79
    """
76
80
    Show the group management panel for a particular subject.
77
81
    Prints to req.
80
84
    groups = req.user.get_groups(offering)
81
85
    if groups.count() == 0:
82
86
        return
83
 
 
84
 
    req.write("<div id=\"subject%d\"class=\"subject\">"%offering.id)
85
 
    req.write("<h1>%s</h1>\n" % cgi.escape(offering.subject.name))
 
87
    
 
88
    offering_groups = {}
 
89
    
 
90
    offering_groups['offering_id'] = offering.id
 
91
    offering_groups['offering_name'] = offering.subject.name
 
92
    offering_groups['groups'] = []
 
93
    
 
94
    #TODO: Use a better way to manage group membership and invitations
86
95
    for group in groups:
87
 
        req.write("<h2>%s (%s)</h2>\n" %
88
 
            (cgi.escape(group.nick if group.nick else ''),
89
 
             cgi.escape(group.name)))
90
 
        if True: # XXX - was is_member (whether real member or just invited)
91
 
            req.write('<p>You are a member of this group.</p>\n')
92
 
        else:
93
 
            req.write('<p>You have been invited to this group.</p>\n')
94
 
            req.write('<p>'
95
 
                '<input type="button" '
96
 
                'onclick="accept(&quot;%(groupnm)s&quot;)" '
97
 
                'value="Accept" />\n'
98
 
                '<input type="button" '
99
 
                'onclick="decline(&quot;%(groupnm)s&quot;)" '
100
 
                'value="Decline" />\n'
101
 
                '</p>\n' % {"groupnm": cgi.escape(group.name)})
102
 
        req.write("<h3>Members</h3>\n")
103
 
        req.write("<ul>\n")
 
96
        new_group = {}
 
97
        new_group['nick'] = cgi.escape(group.nick if group.nick else '')
 
98
        new_group['name'] = cgi.escape(group.name)
 
99
        
 
100
        # XXX - This should be set to reflect whether or not a user is invited
 
101
        #     - or if they have accepted the offer
 
102
        new_group['is_member'] = True
 
103
        new_group['members'] = []
 
104
        
104
105
        for user in group.members:
105
 
            req.write("<li>%s (%s)</li>" %
106
 
                      (cgi.escape(user.fullname),
107
 
                       cgi.escape(user.login)))
108
 
        req.write("</ul>\n")
 
106
            member = {}
 
107
            member['fullname'] = cgi.escape(user.fullname)
 
108
            member['login'] = cgi.escape(user.login)
 
109
            new_group['members'].append(member)
 
110
        offering_groups['groups'].append(new_group)
109
111
 
110
 
    req.write("</div>")
 
112
    ctx['enrolments'].append(offering_groups)