-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-passing-groups.py
executable file
·80 lines (68 loc) · 4.1 KB
/
copy-passing-groups.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
from getpass import getpass
from devilryrestfullib import RestfulFactory
from devilrycliscriptslib.argparsewrapper import ArgumentParser
from devilrycliscriptslib.login import login_using_args
from devilrycliscriptslib.commonargs import add_common_args
from devilrycliscriptslib.queries import find_groups_in_assignment
from devilrycliscriptslib.queries import find_assignment_id_by_shortnames
from devilrycliscriptslib.managegroups import create_group
from devilrycliscriptslib.utils import split_path
argparser = ArgumentParser(description='Copy groups from one assignment to another.')
add_common_args(argparser)
argparser.add_argument('--deadline', required=True,
help=('Initial deadline of the created groups. '
'Format: "YYYY-MM-DD hh:mm". Example: "2010-02-22 22:32"'))
argparser.add_argument('--source', required=True,
help='Path to assignment to copy from. E.g: "duck1010.spring2010.assignment1"')
argparser.add_argument('--target', required=True,
help='Path to assignment to copy to. E.g: "duck1010.spring2010.assignment2"')
argparser.add_argument('--allow_nonempty', default=False, action='store_true',
help=('Allow non-empty target assignment? If you enable '
'this, students are copied to the target even if they '
'already exist in a group within the target.'))
args = argparser.parse_args()
logincookie = login_using_args(args, getpass())
restful_factory = RestfulFactory(args.url)
AssignmentGroupApi = restful_factory.make('/administrator/restfulsimplifiedassignmentgroup/')
AssignmentApi = restful_factory.make('/administrator/restfulsimplifiedassignment/')
DeadlineApi = restful_factory.make('/administrator/restfulsimplifieddeadline/')
try:
path = split_path(args.source, '--source', 3)
source_assignment_id = find_assignment_id_by_shortnames(AssignmentApi, logincookie,
*path)
except LookupError:
raise SystemExit('Assignment {0} not found.'.format(args.source))
try:
path = split_path(args.target, '--target', 3)
target_assignment_id = find_assignment_id_by_shortnames(AssignmentApi, logincookie,
*path)
except LookupError:
raise SystemExit('Assignment {0} not found.'.format(args.target))
sourcegroups = find_groups_in_assignment(AssignmentGroupApi, logincookie, source_assignment_id,
result_fieldgroups=['feedback', 'users', 'tags'])
if not args.allow_nonempty:
targetgroups = find_groups_in_assignment(AssignmentGroupApi, logincookie, target_assignment_id,
result_fieldgroups=['feedback', 'users', 'tags'])
if len(targetgroups) > 0:
raise SystemExit('Action aborted: The target assignment alread have {0} '
'groups. Use --allow_nonempty ignore this '
'check.'.format(len(targetgroups)))
copied = 0
for group in sourcegroups:
if group['feedback__is_passing_grade']:
candidates = [dict(candidate_id=None, username=username)
for username in group['candidates__student__username']]
group = create_group(AssignmentGroupApi, logincookie,
target_assignment_id,
candidates=candidates,
examiners=group['examiners__user__username'],
tags=group['tags__tag'])
DeadlineApi.create(logincookie,
deadline='{0}:00'.format(args.deadline),
assignment_group=group['id'])
copied += 1
print "Copied {copied}/{total} groups from {source} to {target}.".format(copied=copied,
total=len(sourcegroups),
source=args.source,
target=args.target)