Example Output:
Here's an example Python script that uses the Canvas API to achieve the requested tasks. Before you use this script, make sure you have the "requests" library installed in your Python environment. You can install it using pip:
pip install requests
Also, replace YOUR_ACCESS_TOKEN with your actual Canvas API access token:
import requests
import datetime
# Define your Canvas domain and access token
CANVAS_DOMAIN = 'https://your-school.instructure.com'
API_URL = CANVAS_DOMAIN + '/api/v1'
ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN'
headers = {
'Authorization': 'Bearer ' + ACCESS_TOKEN,
}
# 1. Take input of course ID
course_id = input('Enter course ID: ')
# 2. Take input of student login ID
student_login_id = input('Enter student login ID: ')
# 3. Take input of end date
end_date = input('Enter end date (YYYY-MM-DD): ')
# 4. Create a new section called "Extended Access"
create_section_url = f"{API_URL}/courses/{course_id}/sections"
current_date = datetime.datetime.now().isoformat()
payload = {
'course_section[name]': 'Extended Access',
'course_section[start_at]': current_date,
'course_section[end_at]': end_date,
'course_section[restrict_enrollments_to_section_dates]': 'true'
}
response = requests.post(create_section_url, headers=headers, data=payload)
section_id = response.json()['id']
# 5. Enroll the student from step 2 in the newly created section.
# First, we need to retrieve the student's user ID from their login ID
get_user_url = f"{API_URL}/accounts/self/users?search_term={student_login_id}"
response = requests.get(get_user_url, headers=headers)
user_id = response.json()[0]['id']
enroll_student_url = f"{API_URL}/sections/{section_id}/enrollments"
payload = {
'enrollment[user_id]': user_id,
'enrollment[type]': 'StudentEnrollment',
'enrollment[enrollment_state]': 'active'
}
response = requests.post(enroll_student_url, headers=headers, data=payload)
# 6. Enroll the instructor of the course in the newly created section.
# First, we need to retrieve the course's instructor ID
get_course_url = f"{API_URL}/courses/{course_id}?include[]=teachers"
response = requests.get(get_course_url, headers=headers)
teacher_id = response.json()['teachers'][0]['id']
enroll_teacher_url = f"{API_URL}/sections/{section_id}/enrollments"
payload = {
'enrollment[user_id]': teacher_id,
'enrollment[type]': 'TeacherEnrollment',
'enrollment[enrollment_state]': 'active'
}
response = requests.post(enroll_teacher_url, headers=headers, data=payload)
Note:
- You should replace your-school.instructure.com with the URL of your school's Canvas instance.
- This script assumes the first teacher of the course is the instructor to be enrolled in the section. If there are multiple teachers, you may need to modify the script accordingly.
- Error handling is not included in this script. You should add appropriate error handling for production use. For example, checking the HTTP status codes of the responses to ensure the requests were successful.
- The script assumes the provided end date is in the format 'YYYY-MM-DD'.