def create(self, request, *args, **kwargs):
        lti_session = getattr(self.request, 'LTI', {})
        audit_user_id = lti_session.get('custom_canvas_user_login_id')
        account_sis_id = lti_session.get('custom_canvas_account_sis_id')
        if not all((audit_user_id, account_sis_id)):
            raise DRFValidationError(
                'Invalid LTI session: custom_canvas_user_login_id and '
                'custom_canvas_account_sis_id required')

        account = self.request.data.get('account')
        term = self.request.data.get('term')
        if not all((account, term)):
            raise DRFValidationError('Both account and term are required')

        # for the moment, only the current school account can be operated on
        if not account_sis_id[len('school:'):] == account:
            raise PermissionDenied

        selected_courses = self.request.data.get('course_list')

        process = Process.enqueue(
            bulk_publish_canvas_sites,
            settings.RQWORKER_QUEUE_NAME,
            account='sis_account_id:school:{}'.format(account),
            term='sis_term_id:{}'.format(term),
            audit_user=audit_user_id,
            course_list=selected_courses)

        logger.debug('Enqueued Process job for bulk_publish_canvas_sites: '
                     '{}'.format(process))

        return Response(self.serializer_class(process).data,
                        status=status.HTTP_201_CREATED)
def index(request):

    course_instance_id = request.LTI.get('lis_course_offering_sourcedid')
    canvas_course_id = request.LTI.get('custom_canvas_course_id')
    audit_user_id = request.LTI.get('custom_canvas_user_login_id')

    if request.method == 'POST':
        keyword = request.POST.get('keyword')
        title = request.POST.get('title')
        term = request.POST.get('term')

        Process.enqueue(
            migrate_files,
            settings.ISITES_MIGRATION_QUEUE_NAME,
            keyword=keyword,
            canvas_course_id=canvas_course_id,
            term=term,
            title=title,
            audit_user=audit_user_id,
        )

        # try to get the school.
        # if we have a course instance id, try that first.
        school = get_school(course_instance_id, canvas_course_id)

        logger.info(u'"migration started" %s %s "%s" "%s" %s %s %s' % (request.user.username, keyword, title, term,
                                                                 course_instance_id, canvas_course_id, school))
        return redirect('isites_migration:index')

    processes = Process.objects.filter(
        name='isites_migration.jobs.migrate_files',
        details__canvas_course_id=canvas_course_id
    ).order_by('-date_created')

    has_active_process = len([p for p in processes if p.state != Process.COMPLETE]) > 0
    return render(request, 'isites_migration/index.html', {
        'isites': get_previous_isites(course_instance_id),
        'processes': processes,
        'link_to_files_page': settings.CANVAS_URL + '/courses/%s/files' % canvas_course_id,
        'has_active_process': has_active_process,
        # Harcode to EST for now, but eventually set based on the launch user's profile setting
        'user_timezone': 'America/New_York',
    })
    def setUp(self):
        super(BulkPublishCanvasSitesBaseTestCase, self).setUp()
        self.account = 'colgsas'
        self.term = '2017-1'
        self.test_user_id = '12345678'
        self.process = Process.enqueue(
            bulk_publish_canvas_sites,
            QUEUE_NAME,
            account='sis_account_id:school:{}'.format(self.account),
            term='sis_term_id:{}'.format(self.term),
            audit_user=self.test_user_id,
            course_list=None)

        self._refresh_from_db()
        self.assertIsNotNone(self.process.date_created)
        self.assertIsNone(self.process.date_active)
        self.assertEqual(self.process.state, Process.QUEUED)
        self.assertEqual(self.process.status, '')
    def test_publish_selected(self, *args):
        self.process = Process.enqueue(
            bulk_publish_canvas_sites,
            QUEUE_NAME,
            account='sis_account_id:school:{}'.format(self.account),
            term='sis_term_id:{}'.format(self.term),
            audit_user=self.test_user_id,
            course_list=[123, 124, 125])

        self._refresh_from_db()
        self.assertIsNotNone(self.process.date_created)
        self.assertEqual(self.process.state, Process.QUEUED)
        self.assertEqual(self.process.details['course_list'], [123, 124, 125])

        self._flush_jobs()
        self._refresh_from_db()
        self.assertEqual(self.process.state, Process.COMPLETE)
        self.assertEqual(self.process.details['course_list'], [123, 124, 125])
        self.assertEqual(self.process.details['op_config']['courses'],
                         [123, 124, 125])