Use the Content Migration API to copy courses but without their calendar events (or remove them after)

Jump to solution
lchallen
Community Contributor

Hi,

My end goal is to make a script that copies over courses without calendar events.

I have edited a script I found to use the content_migration api call to copy a canvas course from an old one to a new one. I have also explored using the course_copy api call to copy the course without calendar events but I can see it's been retired for a reason - other settings are not coming through and you are left with the default module view.

My long winded solution is to somehow use the content_migration and then check the course for calendar events without dates and remove them... Seems like a really long workaround and, as a total novice, I'm not even sure how to get a list of calendar events by course.

I saw an old feature request asking for a solution here but nothing else helpful. 

Does anybody else have any ideas or solutions?

Also - thanks to whoever originally made the code that I've based this all on.

import requests
import time

token='token goes here'

def migrateContents(old_course, new_course):
    payload={'migration_type':'course_copy_importer','settings[source_course_id]': str(old_course), 'date_shift_options[remove_dates]': 'True'}
    r = requests.post('https://your institution.instructure.com/api/v1/courses/'+ str(new_course)+ '/content_migrations/', params=payload, headers = {'Authorization': 'Bearer ' + token})
    data = r.json()
    print data
    progress_url = data[u'progress_url']
    print "Migration URL is: " + str(progress_url)

    progress = 0
    while progress != 100:
        progress_check = requests.get(progress_url, headers = {'Authorization': 'Bearer ' + token})
        progress_result = progress_check.json()
        print "Migration Status is: " + str(data[u'workflow_state']) + ", | progress: " + str(progress_result[u'completion']) + "%"
        #print progress_result
        progress = progress_result[u'completion']

        time.sleep(2)

        if progress_result[u'completion'] == 100:
                print "------------------------------"
                print "Migration completed."
                break
    

def copyContents(old_course, new_course):
    payload={'source_course':str(old_course),'except[]': 'calendar_events'}
    r = requests.post('https://your institution.instructure.com/api/v1/courses/'+ str(new_course)+ '/course_copy/', params=payload, headers = {'Authorization': 'Bearer ' + token})
    data = r.json()
    print data
    

#comment out the def you don't want to call before running!    
copyContents(1840,3592)
    

migrateContents(1840,3591)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Labels (1)
1 Solution
lchallen
Community Contributor
Author

Hi  @James ,

Very helpful, I updated the payload and added in those copy parameters and tested it - all seems to work fine and no calendar events so question answered. I have included the updated code below. My next step is to read in a CSV file of old and new courses to automate our course rollover process. If anybody is interested in the complete end code let me know and I'll keep you updated. In the meantime here is what I have right now (you can see the additional parameters in line 7):

import requests
import time

token='token here'

def migrateContents(old_course, new_course):
    payload={'migration_type':'course_copy_importer','settings[source_course_id]': str(old_course), 'date_shift_options[remove_dates]':'True','copy[all_course_settings]':'1','copy[all_syllabus_body]':'1','copy[all_context_modules]':'1','copy[all_assignments]':'1','copy[all_quizzes]':'1','copy[all_assessment_question_banks]':'1','copy[all_discussion_topics]':'1','copy[all_wiki_pages]':'1','copy[all_context_external_tools]':'1','copy[all_rubrics]':'1','copy[all_attachments]':'1'}
    r = requests.post('https://yourinstitution.instructure.com/api/v1/courses/'+ str(new_course)+ '/content_migrations/', params=payload, headers = {'Authorization': 'Bearer ' + token})
    data = r.json()
    print data
    progress_url = data[u'progress_url']
    print "Migration URL is: " + str(progress_url)

    progress = 0
    while progress != 100:
        progress_check = requests.get(progress_url, headers = {'Authorization': 'Bearer ' + token})
        progress_result = progress_check.json()
        print "Migration Status is: " + str(data[u'workflow_state']) + ", | progress: " + str(progress_result[u'completion']) + "%"
        #print progress_result
        progress = progress_result[u'completion']

        time.sleep(2)

        if progress_result[u'completion'] == 100:
                print "------------------------------"
                print "Migration completed."
                break
     


migrateContents(1840,3595)
#first number is source course and second is destination

View solution in original post