~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
1279 by William Grant
Drop ivle.conf usage from ivle.pulldown_subj.
49
def enrol_user(config, store, user, year=None):
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
    """
1080.1.72 by William Grant
ivle.pulldown_subj.enrol_user: Actually allow year to be None, and set it
65
    if year is None:
66
        year = unicode(datetime.datetime.now().year)
67
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
68
    count = 0
1279 by William Grant
Drop ivle.conf usage from ivle.pulldown_subj.
69
    for subject, 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
70
        offering = store.find(Offering,
71
                              Subject.code == subject,
72
                              Offering.subject_id == Subject.id,
73
                              Semester.year == year,
74
                              Semester.semester == semester,
75
                              Offering.semester_id == Semester.id).one()
76
77
        # We can't find a matching offering, so we don't care about it.
78
        if not offering:
79
            continue
80
1110 by William Grant
ivle-enrol now allows updating of existing enrolments. It also sets the role.
81
        offering.enrol(user)
82
        count += 1
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
83
    return count
84
1279 by William Grant
Drop ivle.conf usage from ivle.pulldown_subj.
85
def get_pulldown_modules(config):
86
    """Get the subject pulldown modules defined in the configuration.
87
88
    Returns a list of (name, function object)s. The list consists of
89
    the "get_subject" functions of all the plugin subject pulldown modules.
90
    """
91
92
    oldpath = sys.path
93
    # Allow imports to get files from this directory.
94
    # Get the directory that this module (authenticate) is in
95
    plugpath = os.path.split(sys.modules[__name__].__file__)[0]
96
    # Add it to sys.path
97
    sys.path.append(plugpath)
98
99
    subj_modules = []
100
    for modname in config['auth']['subject_pulldown_modules']:
101
        try:
102
            mod = __import__(modname)
103
        except ImportError:
104
            raise SubjectError("Internal error: "
105
                "Can't import subject pulldown module %s"
106
                % repr(modname))
107
        except ValueError:
108
            # If auth_modules is "", we may get an empty string - ignore
109
            continue
110
        try:
111
            subjfunc = mod.get_subjects
112
        except AttributeError:
113
            raise SubjectError("Internal error: Subject pulldown module %r has no "
114
                "'get_subjects' function" % modname)
115
        subj_modules.append((modname, subjfunc))
116
117
    # Restore the old path, without this directory in it.
118
    sys.path = oldpath
119
    return subj_modules