39
38
import ivle.pulldown_subj
41
40
p = optparse.OptionParser()
42
41
p.add_option('--user', '-u', metavar="<login>",
43
42
help="Just perform enrolment for user <login>")
44
43
p.add_option('--verbose', '-v', action='store_true')
44
p.add_option('--year', '-y', metavar="<year>",
45
help="If specified, year to make enrolments for (default: "
45
47
options, arguments = p.parse_args()
47
49
if options.verbose:
52
54
print >> sys.stderr, "%s must be run as root" % sys.argv[0]
55
config = ivle.config.Config()
56
store = ivle.database.get_store(config)
58
if options.user is None:
59
users = store.find(ivle.database.User).order_by(ivle.database.User.login)
61
users = [ivle.database.User.get_by_login(store, options.user)]
59
if options.user is None:
60
users = db.get_users()
62
users = [db.get_user(options.user)]
64
return (user.login, user.unixid)
65
uids = dict(map(repack,users))
66
except Exception, message:
67
logging.error(str(message))
63
70
if options.user is None:
64
71
logging.info("enrolment started")
66
73
logging.info("enrolment started for user %s" % options.user)
75
if options.year is not None and not options.year.isdigit():
76
logging.error("Year must be numeric")
79
users.sort(key=lambda user: user.login)
70
82
# Get all subjects this user is enrolled in, and add them to the DB if
71
83
# they match one of our local subject codes
72
res = ivle.pulldown_subj.enrol_user(config, store, user)
84
res = ivle.pulldown_subj.enrol_user(user.login, options.year)
73
85
logging.info("Enrolled user %s in %d subject%s." % (user.login, res,
74
86
'' if res == 1 else 's'))
75
87
except Exception, message:
76
88
logging.warning(str(message))
80
91
logging.info("enrolment completed successfully")