/
home
/
report
/
report
/
utility
/
file_manager
/
File Upload :
llllll
Current File: /home/report/report/utility/file_manager/interfaces.py
from .Drivers import Drivers from decouple import config from utility.file_manager.Drivers import get_extension class FileManager: accepted_extensions = ['.jpeg', '.png', '.jpg', '.pdf'] allowed_size = 5120000 driver = getattr(Drivers, config('FILE_DRIVER')) @staticmethod def save(files, user=None, from_url=False): """ handle save file in selected driver Args : - files : List of django InMemoryUploadedFile object (get from request) - user : User object who upload this file Return : saved Files Objects | error """ response = [] for file in files: if files[file].size > FileManager.allowed_size: raise Exception('سایز فایل از حداکثر بیشتر است.') if not get_extension(files[file].name).lower() in FileManager.accepted_extensions: raise Exception('فرمت فایل مجاز نمی باشد.') result = FileManager.driver.save(files[file], user, from_url=from_url) if not from_url else FileManager.driver.save( file, user, from_url=from_url) response.append(result) return response @staticmethod def delete(file): """ handle delete file in selected driver Args : - file : File object Return : True | error """ try: result = FileManager.driver.delete() return result except Exception as e: return e # @staticmethod # def relation(file_id, r_type, type_id): # """ handle file relation with other types # # Args : # # - file_id : file id of relation # # - r_type : The type of module to which the file relates # # - type_id : The id of module to which the file relates # # Return : # FileRelation object | error # """ # try: # file = File.objects.get(pk=file_id) # relation = FileRelation.objects.create(file=file, type=r_type, type_id=type_id) # relation.save() # return relation # except Exception as e: # return e
Copyright ©2k19 -
Hexid
|
Tex7ure