~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
1080.1.62 by William Grant
ivle.pulldown_subj: Use Storm, and kill ivle.db dependency. This means we use
34
1110 by William Grant
ivle-enrol now allows updating of existing enrolments. It also sets the role.
35
from ivle.database import Subject, Offering, Semester
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
36
1279 by William Grant
Drop ivle.conf usage from ivle.pulldown_subj.
37
def get_subjects(config, login):
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
38
    """
39
    Looks up the student in whatever modules are available, using login.
40
    If successful, returns a list of (subject, semester) pairs (both strings).
41
    Raises a SubjectError if unsuccessful.
42
    """
1279 by William Grant
Drop ivle.conf usage from ivle.pulldown_subj.
43
    for modname, m in get_pulldown_modules(config):
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
44
        result = m(login)
45
        if result is not None:
46
            return result
47
    return []
48
1338 by William Grant
Move the year decision responsibility to each pulldown module, not the pulldown framework.
49
def enrol_user(config, store, user):
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
50
    """
51
    Looks up the student in whatever modules are available.
52
    The pulldown does not tell us what year to enrol the student for, so the
53
    year may be specified (as a string). If unspecified, will enrol in the
54
    current year according to the system clock.
55
    If successful, enrols the student (in the database) in all subjects in the
56
    system. Subjects which the pulldown tells us the student is in, but which
57
    are not in the system are ignored.
58
    Does not unenrol the student from any subjects.
59
    Does not complain if the student is already enrolled in any subjects.
60
    Raises a SubjectError if the pulldown module says so.
61
62
    Returns the number of subjects the user was enrolled in (not including
63
    subjects outside the system, or subjects already enrolled).
64
    """
65
    count = 0
1338 by William Grant
Move the year decision responsibility to each pulldown module, not the pulldown framework.
66
    for subject, year, semester in get_subjects(config, user.login):
1080.1.62 by William Grant
ivle.pulldown_subj: Use Storm, and kill ivle.db dependency. This means we use
67
        offering = store.find(Offering,
68
                              Subject.code == subject,
69
                              Offering.subject_id == Subject.id,
70
                              Semester.year == year,
71
                              Semester.semester == semester,
72
                              Offering.semester_id == Semester.id).one()
73
74
        # We can't find a matching offering, so we don't care about it.
75
        if not offering:
76
            continue
77
1110 by William Grant
ivle-enrol now allows updating of existing enrolments. It also sets the role.
78
        offering.enrol(user)
79
        count += 1
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
80
    return count
81
1279 by William Grant
Drop ivle.conf usage from ivle.pulldown_subj.
82
def get_pulldown_modules(config):
83
    """Get the subject pulldown modules defined in the configuration.
84
85
    Returns a list of (name, function object)s. The list consists of
86
    the "get_subject" functions of all the plugin subject pulldown modules.
87
    """
88
89
    oldpath = sys.path
90
    # Allow imports to get files from this directory.
91
    # Get the directory that this module (authenticate) is in
92
    plugpath = os.path.split(sys.modules[__name__].__file__)[0]
93
    # Add it to sys.path
94
    sys.path.append(plugpath)
95
96
    subj_modules = []
97
    for modname in config['auth']['subject_pulldown_modules']:
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))
113
114
    # Restore the old path, without this directory in it.
115
    sys.path = oldpath
116
    return subj_modules