Skip to content

Commit 5b67c7c

Browse files
RajeshGovosqrrrlanuraggooglerhimanshupr2627
authored
Created classroom_create_coursework.py to the classroom snippets API (googleworkspace#275)
* Added pull request template Added pull request template * updated header updated header * Update pull_request_template.md * Update pull_request_template.md * Add unit/integration test to checklist * Gmail snippets original code from devral repo. * Gmail snippets original code from devral repo. * Create and send an email message with and without attachment * Update send_message.py * Update send_message_with_attachment.py * Update send_message.py * Delete send_message.py * Delete send_message_with_attachment.py * Added 4 files in classroom snippet. * Adds a teacher to a course. * user ID changed user ID changed * Made changes to the classroom snippets files as per review. * Added classroom_patch_course.py file to the classroom snippets API. * Made changes to the classroom snippets files as per review. * Added files to the classroom snippets API. * Delint imports + adjust region tag * Delint imports + adjust region tag * Delint imports + adjust region tag * Delint imports + adjust region tag * Remove blank line at start of region tag * Delint - remove extra blank line * Created classroom_create_coursework.py to the classroom snippets API. * Created 2 new files and, as per review made changes to 1 file in classroom snippets API . * Made changes to the classroom snippets files as per review. * Created a file to get course details in classroom snippets API. Co-authored-by: Steve Bazyl <[email protected]> Co-authored-by: anuraggoogler <[email protected]> Co-authored-by: himanshupr2627 <[email protected]>
1 parent 1c4917b commit 5b67c7c

4 files changed

Lines changed: 260 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Copyright 2022 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
"""
17+
from __future__ import print_function
18+
19+
import google.auth
20+
from googleapiclient.discovery import build
21+
from googleapiclient.errors import HttpError
22+
23+
# [START classroom_create_coursework]
24+
25+
26+
def classroom_create_coursework(course_id):
27+
28+
"""
29+
Creates the coursework the user has access to.
30+
Load pre-authorized user credentials from the environment.
31+
TODO(developer) - See https://developers.google.com/identity
32+
for guides on implementing OAuth2 for the application.\n"
33+
"""
34+
35+
creds, _ = google.auth.default()
36+
# pylint: disable=maybe-no-member
37+
38+
try:
39+
service = build('classroom', 'v1', credentials=creds)
40+
coursework = {
41+
'title': 'Ant colonies',
42+
'description': '''Read the article about ant colonies
43+
and complete the quiz.''',
44+
'materials': [
45+
{'link': {'url': 'http://example.com/ant-colonies'}},
46+
{'link': {'url': 'http://example.com/ant-quiz'}}
47+
],
48+
'workType': 'ASSIGNMENT',
49+
'state': 'PUBLISHED',
50+
}
51+
coursework = service.courses().courseWork().create(
52+
courseId=course_id, body=coursework).execute()
53+
print(f"Assignment created with ID {coursework.get('id')}")
54+
return coursework
55+
56+
except HttpError as error:
57+
print(f"An error occurred: {error}")
58+
return error
59+
60+
61+
if __name__ == '__main__':
62+
# Put the course_id of course whose coursework needs to be created,
63+
# the user has access to.
64+
classroom_create_coursework(453686957652)
65+
# [END classroom_create_coursework]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
Copyright 2022 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
"""
17+
18+
# [START classroom_get_course]
19+
20+
from __future__ import print_function
21+
22+
import google.auth
23+
from googleapiclient.discovery import build
24+
from googleapiclient.errors import HttpError
25+
26+
27+
def classroom_get_course(course_id):
28+
29+
"""
30+
Prints the name of the with specific course_id.
31+
Load pre-authorized user credentials from the environment.
32+
TODO(developer) - See https://developers.google.com/identity
33+
for guides on implementing OAuth2 for the application.\n"
34+
"""
35+
36+
creds, _ = google.auth.default()
37+
# pylint: disable=maybe-no-member
38+
course = None
39+
try:
40+
service = build('classroom', 'v1', credentials=creds)
41+
course = service.courses().get(id=course_id).execute()
42+
print(f"Course found : {course.get('name')}")
43+
except HttpError as error:
44+
print(f"An error occurred: {error}")
45+
print(f"Course not found: {course_id}")
46+
return error
47+
return course
48+
49+
50+
if __name__ == '__main__':
51+
# Put the course_id of course whose information needs to be fetched.
52+
classroom_get_course(466190187326)
53+
54+
# [END classroom_list_courses]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Copyright 2022 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
"""
17+
from __future__ import print_function
18+
19+
import google.auth
20+
from googleapiclient.discovery import build
21+
from googleapiclient.errors import HttpError
22+
23+
# [START classroom_list_student_submissions]
24+
25+
26+
def classroom_list_student_submissions(course_id, coursework_id, user_id):
27+
"""
28+
Creates the courses the user has access to.
29+
Load pre-authorized user credentials from the environment.
30+
TODO(developer) - See https://developers.google.com/identity
31+
for guides on implementing OAuth2 for the application.\n"
32+
"""
33+
34+
creds, _ = google.auth.default()
35+
# pylint: disable=maybe-no-member
36+
submissions = []
37+
page_token = None
38+
39+
try:
40+
service = build('classroom', 'v1', credentials=creds)
41+
while True:
42+
coursework = service.courses().courseWork()
43+
response = coursework.studentSubmissions().list(
44+
pageToken=page_token,
45+
courseId=course_id,
46+
courseWorkId=coursework_id,
47+
userId=user_id).execute()
48+
submissions.extend(response.get('studentSubmissions', []))
49+
page_token = response.get('nextPageToken', None)
50+
if not page_token:
51+
break
52+
53+
if not submissions:
54+
print('No student submissions found.')
55+
56+
print('Student Submissions:')
57+
for submission in submissions:
58+
print(f"Submitted at:"
59+
f"{(submission.get('id'), submission.get('creationTime'))}")
60+
61+
except HttpError as error:
62+
print(f"An error occurred: {error}")
63+
return submissions
64+
65+
66+
if __name__ == '__main__':
67+
# Put the course_id, coursework_id and user_id of course whose list needs
68+
# to be submitted.
69+
classroom_list_student_submissions(453686957652, 466086979658, "me")
70+
# [END classroom_list_student_submissions]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
Copyright 2022 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
"""
17+
from __future__ import print_function
18+
19+
import google.auth
20+
from googleapiclient.discovery import build
21+
from googleapiclient.errors import HttpError
22+
23+
# [START classroom_list_submissions]
24+
25+
26+
def classroom_list_submissions(course_id, coursework_id):
27+
"""
28+
Creates the courses the user has access to.
29+
Load pre-authorized user credentials from the environment.
30+
TODO(developer) - See https://developers.google.com/identity
31+
for guides on implementing OAuth2 for the application.\n"
32+
"""
33+
34+
creds, _ = google.auth.default()
35+
# pylint: disable=maybe-no-member
36+
submissions = []
37+
page_token = None
38+
39+
try:
40+
service = build('classroom', 'v1', credentials=creds)
41+
while True:
42+
coursework = service.courses().courseWork()
43+
response = coursework.studentSubmissions().list(
44+
pageToken=page_token,
45+
courseId=course_id,
46+
courseWorkId=coursework_id,
47+
pageSize=10).execute()
48+
submissions.extend(response.get('studentSubmissions', []))
49+
page_token = response.get('nextPageToken', None)
50+
if not page_token:
51+
break
52+
53+
if not submissions:
54+
print('No student submissions found.')
55+
56+
print('Student Submissions:')
57+
for submission in submissions:
58+
print(f"Submitted at:"
59+
f"{(submission.get('id'), submission.get('creationTime'))}")
60+
61+
except HttpError as error:
62+
print(f"An error occurred: {error}")
63+
submissions = None
64+
return submissions
65+
66+
67+
if __name__ == '__main__':
68+
# Put the course_id and coursework_id of course whose list needs to be
69+
# submitted.
70+
classroom_list_submissions(453686957652, 466086979658)
71+
# [END classroom_list_submissions]

0 commit comments

Comments
 (0)