def course_filename_prefix_generator(course_id, separator='_'): """ Generates a course-identifying unicode string for use in a file name. Args: course_id (object): A course identification object. separator (str): The character or chain of characters used for separating course details in the filename. Returns: str: A unicode string which can safely be inserted into a filename. """ filename = str(separator).join([ course_id.org, course_id.course, course_id.run ]) enable_course_filename_ccx_suffix = settings.FEATURES.get( 'ENABLE_COURSE_FILENAME_CCX_SUFFIX', False ) if enable_course_filename_ccx_suffix and getattr(course_id, 'ccx', None): filename = separator.join([filename, 'ccx', course_id.ccx]) return get_valid_filename(filename)
def course_filename_prefix_generator(course_id, separator='_'): """ Generates a course-identifying unicode string for use in a file name. Args: course_id (object): A course identification object. Returns: str: A unicode string which can safely be inserted into a filename. """ if isinstance(course_id, basestring): return get_valid_filename(course_id) else: return get_valid_filename( unicode(separator).join( [course_id.org, course_id.course, course_id.run]))
def course_filename_prefix_generator(course_id, separator='_'): """ Generates a course-identifying unicode string for use in a file name. Args: course_id (object): A course identification object. Returns: str: A unicode string which can safely be inserted into a filename. """ return get_valid_filename(unicode(separator).join([course_id.org, course_id.course, course_id.run]))
def save_archive(self, course_id, filename, timestamp, config_name='GRADES_DOWNLOAD'): report_store = ReportStore.from_config(config_name) zip_file_name = u"{filename}_{course}_{timestamp_str}.zip".format( filename=filename, course=get_valid_filename( unicode("_").join( [course_id.org, course_id.course, course_id.run])), timestamp_str=timestamp.strftime("%Y-%m-%d-%H%M")) report_store.store(course_id, zip_file_name, self.archive.read())
def save_csv(self, course_id, filename, cvs_file, timestamp, config_name='GRADES_DOWNLOAD'): report_store = ReportStore.from_config(config_name) csv_filename = u"{filename}_{course}_{timestamp_str}.csv".format( filename=filename, course=get_valid_filename( unicode("_").join( [course_id.org, course_id.course, course_id.run])), timestamp_str=timestamp.strftime("%Y-%m-%d-%H%M")) report_store.store(course_id, csv_filename, cvs_file)
def course_and_time_based_filename_generator(course_id, base_name): """ Generates a filename (without extension) based on the current time and the supplied filename. Args: course_id (object): A course identification object (must have org, course, and run). base_name (str): A name describing what type of file this is. Any characters that are not safe for filenames will be converted per django.core.files.storage.get_valid_filename (Specifically, leading and trailing spaces are removed; other spaces are converted to underscores; and anything that is not a unicode alphanumeric, dash, underscore, or dot, is removed). Returns: str: a concatenation of the org, course and run from the input course_id, the input base_name, and the current time. Note that there will be no extension. """ return u"{course_prefix}_{base_name}_{timestamp_str}".format( course_prefix=course_filename_prefix_generator(course_id), base_name=get_valid_filename(base_name), timestamp_str=datetime.now(UTC).strftime("%Y-%m-%d-%H%M%S"))
def course_and_time_based_filename_generator(course_id, base_name): """ Generates a filename (without extension) based on the current time and the supplied filename. Args: course_id (object): A course identification object (must have org, course, and run). base_name (str): A name describing what type of file this is. Any characters that are not safe for filenames will be converted per django.core.files.storage.get_valid_filename (Specifically, leading and trailing spaces are removed; other spaces are converted to underscores; and anything that is not a unicode alphanumeric, dash, underscore, or dot, is removed). Returns: str: a concatenation of the org, course and run from the input course_id, the input base_name, and the current time. Note that there will be no extension. """ return u"{course_prefix}_{base_name}_{timestamp_str}".format( course_prefix=course_filename_prefix_generator(course_id), base_name=get_valid_filename(base_name), timestamp_str=datetime.now(UTC).strftime("%Y-%m-%d-%H%M%S") )
def save(self, file): thumbnail_size = (105, 105) path = os.path.join(MEDIA_ROOT, 'image_uploads') thumbnail_path = os.path.join(path, 'thumbnails') if not os.path.exists(path): os.mkdir(path) if not os.path.exists(thumbnail_path): os.mkdir(thumbnail_path) file_name = get_valid_filename(file.name) filepath = os.path.join(path, file_name) filepath = self.get_available_name(filepath) file_name = os.path.basename(filepath) destination = open(filepath, 'wb+') for chunk in file.chunks(): destination.write(chunk) destination.close() try: im = Image.open(filepath) thumb = ImageOps.fit(im, thumbnail_size, Image.ANTIALIAS) thumb.save(os.path.join(thumbnail_path, "thumbnail_" + file_name), "PNG") except: pass
def get_filename(self): name = os.path.split(os.path.splitext(self.filename)[0])[1] name = '{display_name}_{name}'.format(name=name, display_name=self.display_name) ext = os.path.splitext(self.file.name)[1] return get_valid_filename(name + ext)
def user_upload_path(instance, filename): return "users/{0}/{1}".format(instance.id, storage.get_valid_filename(filename))
def get_valid_name(self, name): """ Return a filename, based on the provided filename, that's suitable for use in the target storage system. """ return get_valid_filename(name)