def download_submissions(self, request, suffix=''): # pylint: disable=unused-argument """ Api for downloading zip file which consist of all students submissions. """ require(self.is_course_staff()) user = self.get_real_user() require(user) try: zip_file_path = get_zip_file_path( user.username, self.block_course_id, self.block_id, self.location ) zip_file_name = get_zip_file_name( user.username, self.block_course_id, self.block_id ) return Response( app_iter=file_contents_iter(zip_file_path), content_type='application/zip', content_disposition="attachment; filename=" + zip_file_name.encode('utf-8') ) except IOError: return Response( "Sorry, submissions cannot be found. Press Collect ALL Submissions button or" " contact {} if you issue is consistent".format(settings.TECH_SUPPORT_EMAIL), status_code=404 )
def is_zip_file_available(self, user): """ returns True if zip file exists. """ # pylint: disable=no-member zip_file_path = get_zip_file_path(user.username, self.block_course_id, self.block_id, self.location) return default_storage.exists(zip_file_path)
def count_archive_files(self, user): """ returns number of files archive in zip. """ zip_file_path = get_zip_file_path(user.username, self.block_course_id, self.block_id, self.location) with default_storage.open(zip_file_path, 'rb') as zip_file: with closing(ZipFile(zip_file)) as archive: return len(archive.infolist())
def is_zip_file_available(self, user): """ returns True if zip file exists. """ zip_file_path = get_zip_file_path( user.username, self.block_course_id, self.block_id, self.location ) return True if default_storage.exists(zip_file_path) else False
def count_archive_files(self, user): """ returns number of files archive in zip. """ zip_file_path = get_zip_file_path( user.username, self.block_course_id, self.block_id, self.location ) with default_storage.open(zip_file_path, 'rb') as zip_file: with closing(ZipFile(zip_file)) as archive: return len(archive.infolist())
def prepare_download_submissions(self, request, suffix=''): # pylint: disable=unused-argument """ Runs a async task that collects submissions in background and zip them. """ # pylint: disable=no-member require(self.is_course_staff()) user = self.get_real_user() require(user) zip_file_ready = False location = six.text_type(self.location) if self.is_zip_file_available(user): log.info("Zip file already available for block: %s for instructor: %s", location, user.username) assignments = self.get_sorted_submissions() if assignments: last_assignment_date = assignments[0]['timestamp'].astimezone(pytz.utc) zip_file_path = get_zip_file_path( user.username, self.block_course_id, self.block_id, self.location ) zip_file_time = get_file_modified_time_utc(zip_file_path) log.info( "Zip file modified time: %s, last zip file time: %s for block: %s for instructor: %s", last_assignment_date, zip_file_time, location, user.username ) # if last zip file is older the last submission then recreate task if zip_file_time >= last_assignment_date: zip_file_ready = True # check if some one reset submission. If yes the recreate zip file assignment_count = len(assignments) if self.count_archive_files(user) != assignment_count: zip_file_ready = False if not zip_file_ready: log.info("Creating new zip file for block: %s for instructor: %s", location, user.username) zip_student_submissions.delay( self.block_course_id, self.block_id, location, user.username ) return Response(json_body={ "downloadable": zip_file_ready })
def prepare_download_submissions(self, request, suffix=''): # pylint: disable=unused-argument """ Runs a async task that collects submissions in background and zip them. """ require(self.is_course_staff()) user = self.get_real_user() require(user) zip_file_ready = False location = unicode(self.location) if self.is_zip_file_available(user): log.info("Zip file already available for block: %s for instructor: %s", location, user.username) assignments = self.get_sorted_submissions() if assignments: last_assignment_date = assignments[0]['timestamp'].astimezone(pytz.utc) zip_file_path = get_zip_file_path( user.username, self.block_course_id, self.block_id, self.location ) zip_file_time = get_file_modified_time_utc(zip_file_path) log.info( "Zip file modified time: %s, last zip file time: %s for block: %s for instructor: %s", last_assignment_date, zip_file_time, location, user.username ) # if last zip file is older the last submission then recreate task if zip_file_time >= last_assignment_date: zip_file_ready = True # check if some one reset submission. If yes the recreate zip file assignment_count = len(assignments) if self.count_archive_files(user) != assignment_count: zip_file_ready = False if not zip_file_ready: log.info("Creating new zip file for block: %s for instructor: %s", location, user.username) zip_student_submissions.delay( self.block_course_id, self.block_id, location, user.username ) return Response(json_body={ "downloadable": zip_file_ready })
def download_submissions(self, request, suffix=''): # pylint: disable=unused-argument """ Api for downloading zip file which consist of all students submissions. """ require(self.is_course_staff()) user = self.get_real_user() require(user) try: zip_file_path = get_zip_file_path(user.username, self.block_course_id, self.block_id, self.location) zip_file_name = get_zip_file_name(user.username, self.block_course_id, self.block_id) return Response(app_iter=file_contents_iter(zip_file_path), content_type='application/zip', content_disposition="attachment; filename=" + zip_file_name.encode('utf-8')) except IOError: return Response( "Sorry, submissions cannot be found. Press Collect ALL Submissions button or" " contact {} if you issue is consistent".format( settings.TECH_SUPPORT_EMAIL), status_code=404)