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

1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
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: Subject Pulldown
19
# Author: Matt Giuca
20
# Date:   7/7/2008
21
22
# Pluggable subject pulldown module.
23
# Grabs a student's subject list from a source specified in conf.py.
24
25
# Each module should have a get_subjects(login) function, which takes login
26
# and returns a list of (subject, semester) pairs (both strings), or None
27
# if the user can't be found. May also raise a SubjectError which is fatal.
28
29
import os
30
import sys
1080.1.72 by William Grant
ivle.pulldown_subj.enrol_user: Actually allow year to be None, and set it
31
import datetime
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
32
33
from subjecterror import SubjectError
34
import ivle.conf
1080.1.62 by William Grant
ivle.pulldown_subj: Use Storm, and kill ivle.db dependency. This means we use
35
1110 by William Grant
ivle-enrol now allows updating of existing enrolments. It also sets the role.
36
from ivle.database import Subject, Offering, Semester
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
37
38
def get_subjects(login):
39
    """
40
    Looks up the student in whatever modules are available, using login.
41
    If successful, returns a list of (subject, semester) pairs (both strings).
42
    Raises a SubjectError if unsuccessful.
43
    """
44
    for modname, m in subj_modules:
45
        result = m(login)
46
        if result is not None:
47
            return result
48
    return []
49
1080.1.62 by William Grant
ivle.pulldown_subj: Use Storm, and kill ivle.db dependency. This means we use
50
def enrol_user(store, user, year=None):
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
51
    """
52
    Looks up the student in whatever modules are available.
53
    The pulldown does not tell us what year to enrol the student for, so the
54
    year may be specified (as a string). If unspecified, will enrol in the
55
    current year according to the system clock.
56
    If successful, enrols the student (in the database) in all subjects in the
57
    system. Subjects which the pulldown tells us the student is in, but which
58
    are not in the system are ignored.
59
    Does not unenrol the student from any subjects.
60
    Does not complain if the student is already enrolled in any subjects.
61
    Raises a SubjectError if the pulldown module says so.
62
63
    Returns the number of subjects the user was enrolled in (not including
64
    subjects outside the system, or subjects already enrolled).
65
    """
1080.1.72 by William Grant
ivle.pulldown_subj.enrol_user: Actually allow year to be None, and set it
66
    if year is None:
67
        year = unicode(datetime.datetime.now().year)
68
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
69
    count = 0
1080.1.62 by William Grant
ivle.pulldown_subj: Use Storm, and kill ivle.db dependency. This means we use
70
    for subject, semester in get_subjects(user.login):
71
        offering = store.find(Offering,
72
                              Subject.code == subject,
73
                              Offering.subject_id == Subject.id,
74
                              Semester.year == year,
75
                              Semester.semester == semester,
76
                              Offering.semester_id == Semester.id).one()
77
78
        # We can't find a matching offering, so we don't care about it.
79
        if not offering:
80
            continue
81
1110 by William Grant
ivle-enrol now allows updating of existing enrolments. It also sets the role.
82
        offering.enrol(user)
83
        count += 1
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
84
    return count
85
86
# Allow imports to get files from this directory.
87
# Get the directory that this module (authenticate) is in
88
plugpath = os.path.split(sys.modules[__name__].__file__)[0]
89
# Add it to sys.path
90
sys.path.append(plugpath)
91
92
# Create a global variable "subj_modules", a list of (name, function object)s.
93
# This list consists of null_subj, plus the "get_subject" functions of all the
94
# plugin subject pulldown modules.
95
96
subj_modules = []
97
for modname in ivle.conf.subject_pulldown_modules.split(','):
98
    try:
99
        mod = __import__(modname)
100
    except ImportError:
101
        raise SubjectError("Internal error: "
102
            "Can't import subject pulldown module %s"
103
            % repr(modname))
104
    except ValueError:
105
        # If auth_modules is "", we may get an empty string - ignore
106
        continue
107
    try:
108
        subjfunc = mod.get_subjects
109
    except AttributeError:
110
        raise SubjectError("Internal error: Subject pulldown module %r has no "
111
            "'get_subjects' function" % modname)
112
    subj_modules.append((modname, subjfunc))