def test_get_mappings_from_zip_file(self): filename = 'folder_with_xls_and_audio_folder.zip' uploaded_file = self.make_uploaded_zip_file(filename) self.assertTrue(uploaded_file) self.assertEqual(uploaded_file.name, filename) self.assertEqual(uploaded_file.content_type, 'application/zip') deck = services.handle_custom_file(uploaded_file, 'test course', self.user) custom = True [file_contents, zfile, file_names, path_to_excel] = services.extract_from_zip(uploaded_file) [file_contents, mappings] = services.get_mappings_from_zip(deck, file_contents, file_names, zfile, path_to_excel, custom=custom) self.assertTrue(len(file_contents) > 0) self.assertTrue(mappings) self.assertTrue('Image' in mappings) self.assertTrue(len(mappings['Image'].keys()) == 0) self.assertTrue('Audio' in mappings) self.assertTrue(len(mappings['Audio'].keys()) == 6) zip_paths = [ "audio/aurevoir.mp3", "audio/bonappetit.mp3", "audio/bonjour.mp3", "audio/double.mp3", "audio/horsdoeuvre.mp3", "audio/jenecomprendspas.mp3", ] for zip_path in zip_paths: zip_file_name = os.path.split(zip_path)[1] self.assertNotEqual(mappings['Audio'][zip_path], zip_file_name) self.assertRegexpMatches(mappings['Audio'][zip_path], '[0-9a-fA-F]{16}\.\w+')
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 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)