def copy_from_canvas_template(request):
    try:
        data = json.loads(request.body)
        canvas_course_id = data['canvas_course_id']
        template_id = data['template_id']
    except Exception:
        message = (u'Failed to extract canvas parameters from posted data; '
                   u'request body={}'.format(request.body))
        logger.exception(message)
        return JsonResponse({'error': message}, status=400)

    request_parameters = dict(request_ctx=SDK_CONTEXT,
                              course_id=canvas_course_id,
                              migration_type='course_copy_importer',
                              settings_source_course_id=template_id,
    )
    try:
        migration_result = create_content_migration_courses(
            **request_parameters).json()
        logger.debug('content migration API call result: %s' % migration_result)

    except Exception as e:
        message = u'Error creating content migration via SDK with request={}'\
            .format(request_parameters)
        if isinstance(e, CanvasAPIError):
            message += u', SDK error details={}'.format(e)
        logger.exception(message)
        return JsonResponse({}, status=500)
    return JsonResponse(migration_result, status=200)
def create_canvas_content_migration(keyword, canvas_course_id):
    try:
        root_folder = _get_root_folder_for_canvas_course(canvas_course_id)
        export_file_url = _get_s3_download_url(settings.AWS_EXPORT_BUCKET_ISITES_FILES,
                                               "%s.zip" % keyword,
                                               settings.AWS_EXPORT_DOWNLOAD_TIMEOUT_SECONDS)
        logger.info("Importing iSites file export from %s to Canvas course %s", export_file_url, canvas_course_id)
        response = content_migrations.create_content_migration_courses(
            SDK_CONTEXT,
            canvas_course_id,
            'zip_file_importer',
            settings_file_url=export_file_url,
            settings_folder_id=root_folder['id']
        ).json()
        progress_url = response['progress_url']
        logger.info(
            "Created Canvas content migration %s for file import from %s to Canvas course %s",
            progress_url,
            export_file_url,
            canvas_course_id
        )
    except Exception:
        message = "Failed to complete file import for keyword %s", keyword
        logger.exception(message)
        raise RuntimeError(message)

    return progress_url
Beispiel #3
0
def copy_from_canvas_template(request):
    try:
        data = json.loads(request.body)
        canvas_course_id = data['canvas_course_id']
        template_id = data['template_id']
    except Exception:
        message = (u'Failed to extract canvas parameters from posted data; '
                   u'request body={}'.format(request.body))
        logger.exception(message)
        return JsonResponse({'error': message}, status=400)

    request_parameters = dict(
        request_ctx=SDK_CONTEXT,
        course_id=canvas_course_id,
        migration_type='course_copy_importer',
        settings_source_course_id=template_id,
    )
    try:
        migration_result = create_content_migration_courses(
            **request_parameters).json()
        logger.debug('content migration API call result: %s' %
                     migration_result)

    except Exception as e:
        message = u'Error creating content migration via SDK with request={}'\
            .format(request_parameters)
        if isinstance(e, CanvasAPIError):
            message += u', SDK error details={}'.format(e)
        logger.exception(message)
        return JsonResponse({}, status=500)
    return JsonResponse(migration_result, status=200)
def start_course_template_copy(sis_course, canvas_course_id, user_id, course_job_id=None,
                               bulk_job_id=None, template_id=None):
    """
    This method will retrieve the template site associated with an SISCourseData object and start the
    Canvas process of copying the template content into the canvas course site.  A CanvasCourseGenerationJob
    row will be created with the async_operations process data from Canvas and the resulting data object will be
    returned.  If the school associated with the sis data object does not have a template, a
    NoTemplateExistsForSchool exception will be raised.
    Based on the bulk_jb_id being passed, the copy process will handle singletons differently from bulk
    course creation in terms of email generation, etc.
    """

    school_code = sis_course.school_code
    if not template_id:
        # If a template was not given, see if there is a default template for the school
        template_id = get_default_template_for_school(school_code).template_id

    course_generation_job = get_course_generation_data_for_sis_course_id(
        sis_course.pk,
        course_job_id=course_job_id,
        bulk_job_id=bulk_job_id
    )

    # Initiate course copy for template_id
    logger.debug('Requesting content migration from Canvas for canvas_course_id=%s...' % canvas_course_id)
    try:
        content_migration = content_migrations.create_content_migration_courses(
            SDK_CONTEXT,
            canvas_course_id,
            migration_type='course_copy_importer',
            settings_source_course_id=template_id,
        ).json()
    except Exception as e:
        logger.exception('Error in creating content migration for '
                         'canvas_course_id=%s' % canvas_course_id)

    logger.debug('content migration API call result: %s' % content_migration)

    #  Update the status of   course generation job  with metadata (canvas id, workflow_state, progress url, etc)
    logger.debug('Update course generation job tracking row...')

    course_generation_job.canvas_course_id = canvas_course_id
    course_generation_job.content_migration_id = content_migration['id']
    course_generation_job.workflow_state = CanvasCourseGenerationJob.STATUS_QUEUED
    course_generation_job.status_url = content_migration['progress_url']
    course_generation_job.created_by_user_id = user_id

    course_generation_job.save(update_fields=['canvas_course_id', 'content_migration_id', 'status_url', 'workflow_state',
                                      'created_by_user_id'])

    logger.debug('Job row updated: %s' % course_generation_job)

    return course_generation_job
def start_course_template_copy(sis_course, canvas_course_id, user_id, course_job_id=None,
                               bulk_job_id=None, template_id=None):
    """
    This method will retrieve the template site associated with an SISCourseData object and start the
    Canvas process of copying the template content into the canvas course site.  A CanvasCourseGenerationJob
    row will be created with the async process data from Canvas and the resulting data object will be
    returned.  If the school associated with the sis data object does not have a template, a
    NoTemplateExistsForSchool exception will be raised.
    Based on the bulk_jb_id being passed, the copy process will handle singletons differently from bulk
    course creation in terms of email generation, etc.
    """

    school_code = sis_course.school_code
    if not template_id:
        # If a template was not given, see if there is a default template for the school
        template_id = get_default_template_for_school(school_code).template_id

    course_generation_job = get_course_generation_data_for_sis_course_id(
        sis_course.pk,
        course_job_id=course_job_id,
        bulk_job_id=bulk_job_id
    )

    # Initiate course copy for template_id
    logger.debug('Requesting content migration from Canvas for canvas_course_id=%s...' % canvas_course_id)
    content_migration = content_migrations.create_content_migration_courses(
        SDK_CONTEXT,
        canvas_course_id,
        migration_type='course_copy_importer',
        settings_source_course_id=template_id,
    ).json()
    logger.debug('content migration API call result: %s' % content_migration)

    #  Update the status of   course generation job  with metadata (canvas id, workflow_state, progress url, etc)
    logger.debug('Update course generation job tracking row...')

    course_generation_job.canvas_course_id = canvas_course_id
    course_generation_job.content_migration_id = content_migration['id']
    course_generation_job.workflow_state = CanvasCourseGenerationJob.STATUS_QUEUED
    course_generation_job.status_url = content_migration['progress_url']
    course_generation_job.created_by_user_id = user_id

    course_generation_job.save(update_fields=['canvas_course_id', 'content_migration_id', 'status_url', 'workflow_state',
                                      'created_by_user_id'])

    logger.debug('Job row updated: %s' % course_generation_job)

    return course_generation_job
 def _import_isite(self, keyword, canvas_course_id):
     import_folder = self._get_or_create_import_folder(canvas_course_id, settings.CANVAS_IMPORT_FOLDER_NAME)
     export_file_url = self._get_export_s3_url(keyword)
     logger.info("Importing iSites file export from %s to Canvas course %s", export_file_url, canvas_course_id)
     response = json.loads(content_migrations.create_content_migration_courses(
         SDK_CONTEXT,
         canvas_course_id,
         'zip_file_importer',
         settings_file_url=self._get_export_s3_url(keyword),
         settings_folder_id=import_folder['id']
     ).text)
     progress_url = response['progress_url']
     self.canvas_progress_urls.append(progress_url)
     logger.info(
         "Created Canvas content migration %s for import from %s to Canvas course %s",
         progress_url,
         export_file_url,
         canvas_course_id
     )