def test_getCourseCollections(self): card_template = CardTemplate.objects.create(title='Test', description='Test') collection = Collection.objects.create(title='Test', description='Test', card_template=card_template) collection2 = Collection.objects.create(title='Test2', description='Test2', card_template=card_template) collection3 = Collection.objects.create(title='Test3', description='Test3', card_template=card_template) canvas_course_id = 123 request = self.createMockRequest(canvas_course_id, [const.INSTRUCTOR]) lti_service = LTIService(request) course_id = lti_service.getCourseId() self.assertTrue(lti_service.associateCourse(collection.id)) self.assertTrue(lti_service.associateCourse(collection3.id)) self.assertTrue( lti_service.isCourseAssociated(course_id, collection.id)) self.assertFalse( lti_service.isCourseAssociated(course_id, collection2.id)) self.assertTrue( lti_service.isCourseAssociated(course_id, collection3.id)) course_collections = lti_service.getCourseCollections() expected_collections = [c.id for c in (collection, collection3)] self.assertEqual(len(course_collections), len(expected_collections)) self.assertEqual(course_collections, expected_collections)
def create(request): """Creates a collection.""" role_bucket = services.get_or_update_role_bucket(request) course_collections = LTIService(request).getCourseCollections() collection_filters = dict( collection_ids=course_collections, can_filter=not queries.is_superuser_or_staff(request.user)) collection_list = queries.getCollectionList(role_bucket, **collection_filters) if request.method == 'POST': lti_req = LTIService(request) published = not lti_req.isTeacher() request.POST = request.POST.copy() request.POST['published'] = published collection_form = CollectionForm(request.POST) card_template_id = collection_form.data['card_template'] if collection_form.is_valid(): collection = collection_form.save() lti_req.associateCourse(collection.id) services.add_user_to_collection( user=request.user, collection=collection, role=Users_Collections.ADMINISTRATOR) #update role_bucket to add admin permission to the user for this newly created collection services.get_or_update_role_bucket( request, collection.id, Users_Collections.role_map[Users_Collections.ADMINISTRATOR]) log.info('Collection %s created.' % collection.id, extra={'user': request.user}) analytics.track(actor=request.user, verb=analytics.VERBS.created, object=analytics.OBJECTS.collection, context={"custom": False}) return redirect(collection) else: rel_templates = CardTemplate.objects.filter( Q(owner__isnull=True) | Q(owner=request.user)) initial = {'card_template': '1'} card_template_id = initial['card_template'] collection_form = CollectionForm(query_set=rel_templates, initial=initial) # Pre-populate the "preview" of the card template # This view is also called via AJAX on the page. prev_request = HttpRequest() prev_request.method = 'GET' prev_request.GET['card_template_id'] = card_template_id prev_response = card_template.preview(prev_request) card_template_preview_html = prev_response.content context = { "nav_collections": collection_list, "active_collection": None, "collection_form": collection_form, "card_template_preview_html": card_template_preview_html } return render(request, 'collections/create.html', context)
def create(request): """Creates a collection.""" role_bucket = services.get_or_update_role_bucket(request) course_collections = LTIService(request).getCourseCollections() collection_filters = dict(collection_ids=course_collections, can_filter=not queries.is_superuser_or_staff(request.user)) collection_list = queries.getCollectionList(role_bucket, **collection_filters) if request.method == 'POST': lti_req = LTIService(request) published = not lti_req.isTeacher() request.POST = request.POST.copy() request.POST['published'] = published collection_form = CollectionForm(request.POST) card_template_id = collection_form.data['card_template'] if collection_form.is_valid(): collection = collection_form.save() lti_req.associateCourse(collection.id) services.add_user_to_collection(user=request.user, collection=collection, role=Users_Collections.ADMINISTRATOR) #update role_bucket to add admin permission to the user for this newly created collection services.get_or_update_role_bucket(request, collection.id, Users_Collections.role_map[Users_Collections.ADMINISTRATOR]) log.info('Collection %s created.' %collection.id, extra={'user': request.user}) analytics.track( actor=request.user, verb=analytics.VERBS.created, object=analytics.OBJECTS.collection, context={"custom": False} ) return redirect(collection) else: rel_templates = CardTemplate.objects.filter(Q(owner__isnull=True) | Q(owner=request.user)) initial = {'card_template': '1'} card_template_id = initial['card_template'] collection_form = CollectionForm(query_set=rel_templates,initial=initial) # Pre-populate the "preview" of the card template # This view is also called via AJAX on the page. prev_request = HttpRequest() prev_request.method = 'GET' prev_request.GET['card_template_id'] = card_template_id prev_response = card_template.preview(prev_request) card_template_preview_html = prev_response.content context = { "nav_collections": collection_list, "active_collection": None, "collection_form": collection_form, "card_template_preview_html": card_template_preview_html } return render(request, 'collections/create.html', context)
def custom_create(request): """ Creates a collection with custom template. """ upload_error = '' course_name = '' role_bucket = services.get_or_update_role_bucket(request) lti_req = LTIService(request) course_collections = lti_req.getCourseCollections() collection_filters = dict( collection_ids=course_collections, can_filter=not queries.is_superuser_or_staff(request.user)) collection_list = queries.getCollectionList(role_bucket, **collection_filters) if request.method == 'POST': d = {'user': request.user} log.info('The user is uploading a custom deck.', extra=d) course_name = request.POST.get('course', '') if course_name == '' or 'file' not in request.FILES: if course_name == '' and 'file' not in request.FILES: upload_error = "Course name needed. No file selected." elif course_name != '' and 'file' not in request.FILES: upload_error = 'No file selected' else: upload_error = 'Course name needed.' else: try: is_teacher = lti_req.isTeacher() deck = services.handle_custom_file(request.FILES['file'], course_name, request.user, is_teacher) lti_req.associateCourse(deck.collection.id) log.info( 'Custom deck %(d)s successfully added to the new collection %(c)s.' % { 'c': str(deck.collection.id), 'd': str(deck.id) }, extra=d) return redirect(deck) except Exception, e: upload_error = str(e) msg = 'The following error occurred when the user tried uploading a deck: ' log.error(msg + upload_error, extra=d)
def test_getCourseCollections(self): card_template = CardTemplate.objects.create(title='Test', description='Test') collection = Collection.objects.create(title='Test', description='Test', card_template=card_template) collection2 = Collection.objects.create(title='Test2', description='Test2', card_template=card_template) collection3 = Collection.objects.create(title='Test3', description='Test3', card_template=card_template) canvas_course_id = 123 request = self.createMockRequest(canvas_course_id, [const.INSTRUCTOR]) lti_service = LTIService(request) course_id = lti_service.getCourseId() self.assertTrue(lti_service.associateCourse(collection.id)) self.assertTrue(lti_service.associateCourse(collection3.id)) self.assertTrue(lti_service.isCourseAssociated(course_id, collection.id)) self.assertFalse(lti_service.isCourseAssociated(course_id, collection2.id)) self.assertTrue(lti_service.isCourseAssociated(course_id, collection3.id)) course_collections = lti_service.getCourseCollections() expected_collections = [c.id for c in (collection, collection3)] self.assertEqual(len(course_collections), len(expected_collections)) self.assertEqual(course_collections, expected_collections)
def test_associateCourse(self): card_template = CardTemplate.objects.create(title='Test', description='Test') collection = Collection.objects.create(title='Test', description='Test', card_template=card_template) canvas_course_id = 123 request = self.createMockRequest(canvas_course_id, [const.INSTRUCTOR]) lti_service = LTIService(request) course_id = lti_service.getCourseId() self.assertFalse(lti_service.isCourseAssociated(course_id, collection.id), "Canvas course NOT associated with collection") result = lti_service.associateCourse(collection.id) self.assertTrue(result, msg="Canvas course associated successfully") self.assertTrue(lti_service.isCourseAssociated(course_id, collection.id), "Canvas course IS associated with collection")
def custom_create(request): """ Creates a collection with custom template. """ upload_error = '' course_name = '' role_bucket = services.get_or_update_role_bucket(request) lti_req = LTIService(request) course_collections = lti_req.getCourseCollections() collection_filters = dict(collection_ids=course_collections, can_filter=not queries.is_superuser_or_staff(request.user)) collection_list = queries.getCollectionList(role_bucket, **collection_filters) if request.method == 'POST': d = {'user': request.user} log.info('The user is uploading a custom deck.', extra=d) course_name = request.POST.get('course', '') if course_name == '' or 'file' not in request.FILES: if course_name == '' and 'file' not in request.FILES: upload_error = "Course name needed. No file selected." elif course_name != '' and 'file' not in request.FILES: upload_error = 'No file selected' else: upload_error = 'Course name needed.' else: try: is_teacher = lti_req.isTeacher() deck = services.handle_custom_file(request.FILES['file'], course_name, request.user, is_teacher) lti_req.associateCourse(deck.collection.id) log.info('Custom deck %(d)s successfully added to the new collection %(c)s.' %{'c': str(deck.collection.id), 'd':str(deck.id)}, extra=d) return redirect(deck) except Exception, e: upload_error = str(e) msg = 'The following error occurred when the user tried uploading a deck: ' log.error(msg + upload_error , extra=d)
def test_associateCourse(self): card_template = CardTemplate.objects.create(title='Test', description='Test') collection = Collection.objects.create(title='Test', description='Test', card_template=card_template) canvas_course_id = 123 request = self.createMockRequest(canvas_course_id, [const.INSTRUCTOR]) lti_service = LTIService(request) course_id = lti_service.getCourseId() self.assertFalse( lti_service.isCourseAssociated(course_id, collection.id), "Canvas course NOT associated with collection") result = lti_service.associateCourse(collection.id) self.assertTrue(result, msg="Canvas course associated successfully") self.assertTrue( lti_service.isCourseAssociated(course_id, collection.id), "Canvas course IS associated with collection")