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

« back to all changes in this revision

Viewing changes to bin/ivle-enrol

  • Committer: William Grant
  • Date: 2012-06-28 01:52:02 UTC
  • Revision ID: me@williamgrant.id.au-20120628015202-f6ru7o367gt6nvgz
Hah

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import os
26
26
import sys
27
27
 
28
 
from ivle.database import get_store, Subject, Semester, Offering, User, \
29
 
                          AlreadyEnrolledError
 
28
from ivle.config import Config
 
29
from ivle.database import get_store, Subject, Semester, Offering, User
30
30
 
31
31
def die(error):
32
32
    print >> sys.stderr, '%s: %s' % (os.path.basename(sys.argv[0]), error)
33
33
    sys.exit(1)
34
34
 
35
 
if len(sys.argv) != 5:
36
 
    print >> sys.stderr, "usage: %s <login> <subjectcode> <year> <semester>" \
 
35
if len(sys.argv) not in (5, 6):
 
36
    print >> sys.stderr, '''usage: %s <login> <subjectcode> <year> <semestercode> [role]
 
37
 
 
38
'role' should be one of student (the default), tutor or lecturer.
 
39
If an enrolment already exists, it will be reactivated and its role set.''' \
37
40
                         % os.path.basename(sys.argv[0])
38
41
    sys.exit()
39
42
 
40
 
store = get_store()
 
43
store = get_store(Config())
41
44
 
42
45
user = User.get_by_login(store, sys.argv[1])
43
46
if user is None:
46
49
offering = store.find(Offering,
47
50
                    Subject.code == unicode(sys.argv[2]),
48
51
                    Semester.year == unicode(sys.argv[3]),
49
 
                    Semester.semester == unicode(sys.argv[4]),
 
52
                    Semester.code == unicode(sys.argv[4]),
50
53
                    Offering.subject_id == Subject.id,
51
54
                    Offering.semester_id == Semester.id).one()
52
55
if not offering:
53
56
    die("subject offering not found")
54
57
 
55
 
if not offering.semester.active:
56
 
    die("cannot enrol - semester inactive")
57
 
    
 
58
role = unicode(sys.argv[5]) if len(sys.argv) == 6 else u'student'
 
59
 
58
60
try:
59
 
    offering.enrol(user)
 
61
    offering.enrol(user, role)
60
62
except AlreadyEnrolledError:
61
63
    die("already enrolled")
62
64