Skip to content

Commit ba7a19d

Browse files
added managing teacher and students snippets
1 parent b4417f3 commit ba7a19d

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

classroom/snippets/snippets.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,23 @@ def update_course(self):
100100
print('Course %s updated.' % course.get('name'))
101101
# [END classroom_update_course]
102102

103+
def patch_course(self):
104+
"""
105+
Creates a course with alias specification.
106+
"""
107+
service = self.service
108+
# [START classroom_patch_course]
109+
course_id = '123456'
110+
course = {
111+
'section': 'Period 3',
112+
'room': '302'
113+
}
114+
course = service.courses().patch(id=course_id,
115+
updateMask='section,room',
116+
body=course).execute()
117+
print('Course "%s" updated.' % course.get('name'))
118+
# [END classroom_patch_course]
119+
103120
def add_alias_new(self):
104121
"""
105122
Creates a course with alias specification.
@@ -140,3 +157,57 @@ def add_alias_existing(self):
140157
except errors.HttpError:
141158
print('Alias Creation Failed')
142159
# [END classroom_existing_alias]
160+
161+
def add_teacher(self):
162+
"""
163+
Adds a teacher to a course.
164+
"""
165+
service = self.service
166+
# [START classroom_add_teacher]
167+
course_id = '123456'
168+
teacher_email = '[email protected]'
169+
teacher = {
170+
'userId': teacher_email
171+
}
172+
try:
173+
teacher = service.courses().teachers().create(courseId=course_id,
174+
body=teacher).execute()
175+
print('User %s was added as a teacher to the course with ID %s'
176+
% (teacher.get('profile').get('name').get('fullName'),
177+
course_id))
178+
except errors.HttpError as e:
179+
error = json.loads(e.content).get('error')
180+
if(error.get('code') == 409):
181+
print('User "{%s}" is already a member of this course.'
182+
% teacher_email)
183+
else:
184+
raise
185+
# [END classroom_add_teacher]
186+
187+
def add_student(self):
188+
"""
189+
Adds a student to a course.
190+
"""
191+
service = self.service
192+
# [START classroom_add_student]
193+
course_id = '123456'
194+
enrollment_code = 'abcdef'
195+
student = {
196+
'userId': 'me'
197+
}
198+
try:
199+
student = service.courses().students().create(
200+
courseId=course_id,
201+
enrollmentCode=enrollment_code,
202+
body=student).execute()
203+
print (
204+
'User {%s} was enrolled as a student in the course with ID "{%s}"'
205+
% (student.get('profile').get('name').get('fullName'),
206+
course_id))
207+
except errors.HttpError as e:
208+
error = json.loads(e.content).get('error')
209+
if(error.get('code') == 409):
210+
print('You are already a member of this course.')
211+
else:
212+
raise
213+
# [END classroom_add_student]

0 commit comments

Comments
 (0)