This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenroll.py
146 lines (127 loc) · 6.3 KB
/
enroll.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from nbgrader.api import Gradebook, Student
from nbgrader.auth import JupyterHubAuthPlugin, Authenticator
from nbgrader.auth.jupyterhub import _query_jupyterhub_api, JupyterhubApiError
from traitlets.config import Config
import os
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
class SubAuthenticator(Authenticator):
def add_grader_to_course(self, student_id: str, course_id: str) -> None:
logger.debug('Calling add_grader_to_course in SubAuthenticator')
self.plugin.add_grader_to_course(student_id, course_id)
def add_student_to_course(self, student_id: str, course_id: str) -> None:
logger.debug('Calling add_student')
self.plugin.add_student_to_course(student_id, course_id)
class SubAuthPlugin(JupyterHubAuthPlugin):
def add_student_to_course(self, student_id: str, course_id: str) -> None:
logger.debug('Calling add_student_to_course in plugin')
logger.debug('JUPYTERHUB_API_URL: %s' % os.environ.get('JUPYTERHUB_API_URL'))
if not course_id:
logger.error(
"Could not add student to course because the course_id has not "
"been provided. Has it been set in the nbgrader_config.py?")
return
try:
logger.debug('try block')
group_name = "nbgrader-{}".format(course_id)
jup_groups = _query_jupyterhub_api(
method="GET",
api_path="/groups",
)
logger.debug('jup_groups: %s' % jup_groups)
if group_name not in [x['name'] for x in jup_groups]:
# This could result in a bad request(JupyterhubApiError) if
# there is already a group so first we check above if there is a
# group
_query_jupyterhub_api(
method="POST",
api_path="/groups/{name}".format(name=group_name),
)
logger.info("Jupyterhub group: {group_name} created.".format(
group_name=group_name))
_query_jupyterhub_api(
method="POST",
api_path="/groups/{name}/users".format(name=group_name),
post_data={"users": [student_id]}
)
logger.debug(f'Added {student_id} to {group_name}')
# Saying student could be already here is because the post request
# returns 200 even if the student_id was already in the group
logger.info(
"Student {student} added or was already in the Jupyterhub group: {group_name}".format(
student=student_id,
group_name=group_name))
except JupyterhubApiError as e:
# We assume user might be using Jupyterhub but something is not working
err_msg = "Student {student} NOT added to the Jupyterhub group {group_name}: ".format(
student=student_id,
group_name=group_name
)
logger.error(err_msg + str(e))
logger.error(
"Make sure you set a valid admin_user 'api_token' in your config file before starting the service")
def add_grader_to_course(self, student_id: str, course_id: str) -> None:
logger.debug('Adding grader to course')
if not course_id:
logger.error(
"Could not add grader to course because the course_id has not "
"been provided. Has it been set in the nbgrader_config.py?")
return
try:
group_name = "formgrader-{}".format(course_id)
jup_groups = _query_jupyterhub_api(
method="GET",
api_path="/groups",
)
logger.debug('jup_groups %s' % jup_groups)
if group_name not in [x['name'] for x in jup_groups]:
# This could result in a bad request(JupyterhubApiError) if
# there is already a group so first we check above if there is a
# group
_query_jupyterhub_api(
method="POST",
api_path="/groups/{name}".format(name=group_name),
)
logger.info("Jupyterhub group: {group_name} created.".format(
group_name=group_name))
logger.debug('About to query Jupyterhub API')
_query_jupyterhub_api(
method="POST",
api_path="/groups/{name}/users".format(name=group_name),
post_data={"users": [student_id]}
)
# Saying instructor could be already here is because the post request
# returns 200 even if the student_id was already in the group
logger.info(
"Instructor {instructor} added or was already in the Jupyterhub group: {group_name}".format(
instructor=student_id,
group_name=group_name))
except JupyterhubApiError as e:
# We assume user might be using Jupyterhub but something is not working
err_msg = "Instructor {instructor} NOT added to the Jupyterhub group {group_name}: ".format(
instructor=student_id,
group_name=group_name
)
logger.error(err_msg + str(e))
logger.error("Make sure you set a valid admin_user 'api_token' in your config file before starting the service")
if __name__ == '__main__':
config = Config()
config.Authenticator.plugin_class = SubAuthPlugin
authenticator = SubAuthenticator(config=config)
env = os.environ
firstname = env['FIRST_NAME']
surname = env['LAST_NAME']
nbgrader_db = env['GRADEBOOK_DB']
course_name = env['COURSE']
unix_name = env['USERNAME']
logger.debug('calling Gradebook %s %s %s' % (nbgrader_db, course_name, authenticator))
with Gradebook(nbgrader_db, course_name, authenticator) as gb:
logger.debug('\n\n\nInside Gradebook context\n\n\n %s' % vars(gb))
student = {"first_name": firstname, "last_name": surname, "email": ''}
gb.add_student(unix_name, **student)
logger.debug('Created student')
logger.debug('Created student\n\n')
if unix_name == 'instructor':
logger.debug('unix_name == instructor')
authenticator.add_grader_to_course('instructor', course_name)