def export_olx(self, user_id, course_key_string, language): """ Export a course or library to an OLX .tar.gz archive and prepare it for download. """ courselike_key = CourseKey.from_string(course_key_string) try: user = User.objects.get(pk=user_id) except User.DoesNotExist: with respect_language(language): self.status.fail(_(u'Unknown User ID: {0}').format(user_id)) return if not has_course_author_access(user, courselike_key): with respect_language(language): self.status.fail(_(u'Permission denied')) return if isinstance(courselike_key, LibraryLocator): courselike_module = modulestore().get_library(courselike_key) else: courselike_module = modulestore().get_course(courselike_key) try: self.status.set_state(u'Exporting') tarball = create_export_tarball(courselike_module, courselike_key, {}, self.status) artifact = UserTaskArtifact(status=self.status, name=u'Output') artifact.file.save(name=os.path.basename(tarball.name), content=File(tarball)) # pylint: disable=no-member artifact.save() # catch all exceptions so we can record useful error messages except Exception as exception: # pylint: disable=broad-except LOGGER.exception(u'Error exporting course %s', courselike_key, exc_info=True) if self.status.state != UserTaskStatus.FAILED: self.status.fail({'raw_error_msg': text_type(exception)}) return
def export_olx(self, user_id, course_key_string, language): """ Export a course or library to an OLX .tar.gz archive and prepare it for download. """ courselike_key = CourseKey.from_string(course_key_string) try: user = User.objects.get(pk=user_id) except User.DoesNotExist: with respect_language(language): self.status.fail(_(u'Unknown User ID: {0}').format(user_id)) return if not has_course_author_access(user, courselike_key): with respect_language(language): self.status.fail(_(u'Permission denied')) return if isinstance(courselike_key, LibraryLocator): courselike_module = modulestore().get_library(courselike_key) else: courselike_module = modulestore().get_course(courselike_key) try: self.status.set_state(u'Exporting') tarball = create_export_tarball(courselike_module, courselike_key, {}, self.status) artifact = UserTaskArtifact(status=self.status, name=u'Output') artifact.file.save(name=tarball.name, content=File(tarball)) # pylint: disable=no-member artifact.save() # catch all exceptions so we can record useful error messages except Exception as exception: # pylint: disable=broad-except LOGGER.exception(u'Error exporting course %s', courselike_key) if self.status.state != UserTaskStatus.FAILED: self.status.fail({'raw_error_msg': text_type(exception)}) return
def export_olx(self, user_id, course_key_string, language): """ Export a course or library to an OLX .tar.gz archive and prepare it for download. """ set_code_owner_attribute_from_module(__name__) courselike_key = CourseKey.from_string(course_key_string) try: user = User.objects.get(pk=user_id) except User.DoesNotExist: with translation_language(language): self.status.fail(UserErrors.UNKNOWN_USER_ID.format(user_id)) return if not has_course_author_access(user, courselike_key): with translation_language(language): self.status.fail(UserErrors.PERMISSION_DENIED) return if isinstance(courselike_key, LibraryLocator): courselike_module = modulestore().get_library(courselike_key) else: courselike_module = modulestore().get_course(courselike_key) try: self.status.set_state('Exporting') tarball = create_export_tarball(courselike_module, courselike_key, {}, self.status) artifact = UserTaskArtifact(status=self.status, name='Output') artifact.file.save(name=os.path.basename(tarball.name), content=File(tarball)) artifact.save() # catch all exceptions so we can record useful error messages except Exception as exception: # pylint: disable=broad-except LOGGER.exception('Error exporting course %s', courselike_key, exc_info=True) if self.status.state != UserTaskStatus.FAILED: self.status.fail({'raw_error_msg': str(exception)}) return
def test_long_text_string(self): """Long text artifacts should be cropped for the string representation.""" status = self._status() text = 'blah ' * 50 artifact = UserTaskArtifact(status=status, name='Cropped', text=text) assert str(artifact) == '<UserTaskArtifact: (Cropped) {}>'.format( text[:47] + '...')
def test_text_string(self): """Text-based UserTaskArtifacts should have a reasonable string representation.""" status = self._status() artifact = UserTaskArtifact(status=status, text='This was the output text.') assert str( artifact ) == '<UserTaskArtifact: (Output) This was the output text.>'
def test_url_string(self): """URL-based UserTaskArtifacts should have a reasonable string representation.""" status = self._status() artifact = UserTaskArtifact(status=status, url='http://www.edx.org/') assert str( artifact) == '<UserTaskArtifact: (Output) http://www.edx.org/>'
def test_file_string(self): """File-based UserTaskArtifacts should have a reasonable string representation""" status = self._status() uploaded_file = SimpleUploadedFile('file.txt', b'File content') artifact = UserTaskArtifact(status=status, file=uploaded_file) assert str(artifact) == '<UserTaskArtifact: (Output) file.txt>'