def submit_assign_mass(request, course, announce): #mod by hemant if request.user.is_authenticated: if request.method == 'POST': upload_file = request.FILES['document'] with zipfile.ZipFile(upload_file, 'r') as f: for name in f.namelist(): data = f.read(name) if len(name.split('/')) == 3 and not (name.split('/')[2] == ''): fs = FileSystemStorage() k = name.split('/') tail = k[1] + '/' + k[2] file_path = course + '/' + announce + '/' + k[1] if fs.exists(file_path) and ( fs.listdir(file_path)[1]) != 0: for fil in fs.listdir(file_path)[1]: fs.delete(file_path + '/' + fil) fs.save(course + '/' + announce + '/' + tail, io.BytesIO(data)) return render(request, 'files/upload.html', { 'course': course, 'announce': announce }) else: return redirect('account/login')
def get_assign(request, course, announce): #mod by hemant if request.user.is_authenticated: if request.method == 'POST': s = getzip(course, announce) resp = HttpResponse(s.getvalue(), content_type='application/x-zip-compressed') resp['Content-Disposition'] = 'attachment; filename=%s' % ( course + '-' + announce + '.zip') return resp fs = FileSystemStorage() sublist = [] subpath = [] if fs.exists(course + '/' + announce): dirs = fs.listdir(course + '/' + announce) dirs = dirs[0] dirs.remove('__grades') sublist = dirs for x in dirs: sub_name = fs.listdir(course + '/' + announce + '/' + x)[1][0] subpath.append('/media/' + course + '/' + announce + '/' + x + '/' + sub_name) return render( request, 'files/download.html', { 'course': course, 'announce': announce, 'sublist': list(zip(sublist, subpath)) }) else: return redirect('account/login')
def get_user_sub_name(course, assign, user): #hemant fs = FileSystemStorage() user_file_path = course + '/' + assign + '/' + user if fs.exists(user_file_path) and len(fs.listdir(user_file_path)[1]) > 0: file_name = fs.listdir(user_file_path)[1][0] return file_name.rstrip('.zip') return ''
def galleries(request): fs = FileSystemStorage() # GET request if request.method == 'GET': response_data = {} galleries = [] for g in fs.listdir('')[0]: # Check for title image if len(fs.listdir(g)[1]) > 0: i = fs.listdir(g)[1][0] gallery = { "name": urllib.parse.unquote(g), "path": g, "image": { "path": i, "fullpath": g + '/' + i, "name": i.split('.')[0], "modified": str(datetime.datetime.now()), } } else: gallery = {"name": urllib.parse.unquote(g), "path": g} galleries.append(gallery) response_data['galleries'] = galleries return Response(response_data, status=status.HTTP_200_OK) # POST request elif request.method == 'POST': serializer = GallerySerializerPostRead(data=request.data) if serializer.is_valid(): if fs.exists(urllib.parse.quote(serializer.data['name'])): return Response('Galéria so zadaným názvom už existuje.', status=status.HTTP_409_CONFLICT) else: os.mkdir("media/" + urllib.parse.quote(serializer.data['name'])) content = { "name": serializer.data['name'], "path": urllib.parse.quote(serializer.data['name']) } return Response(content, status=status.HTTP_201_CREATED) else: bad_validation_response = { "code": 400, "payload": { "paths": ["name"], "validator": "required", "example": None, }, "name": "INVALID_SCHEMA", "description": "Bad JSON object: u'name' is a required property" } return Response(bad_validation_response, status=status.HTTP_400_BAD_REQUEST)
def status_of_pending_files(request): if request.user.has_perm('molbank.is_admin'): user = 1 #permissions print("PERMISSIONS: { ") pprint.pprint(request.user.get_all_permissions()) print(" }") fs = FileSystemStorage() var = fs.listdir('') #listing of files print("FILES IN DIR : { ") pprint.pprint(var[1]) print(" }") all = status.objects.all() return render(request, 'molbank/AdminUploadCSV.html',{'flag_pending':False,'pending': var[1],'flag':True,'pill':'uploadCSV', 'allstat': all, 'user':user,'username':request.user.username}) #flag is used for checking whether there are any pending files or not elif request.user.has_perm('molbank.is_chem'): user = 2 print("PERMISSIONS: { ") pprint.pprint(request.user.get_all_permissions()) print(" }") fs = FileSystemStorage() var = fs.listdir('') # listing of files print("FILES IN DIR : { ") pprint.pprint(var[1]) print(" }") all = status.objects.all() return render(request, 'molbank/uploadCSV.html', {'flag_pending':False,'pending': var[1], 'flag': True, 'pill': 'uploadCSV', 'allstat': all, 'user':user,'username':request.user.username}) elif request.user.has_perm('molbank.is_bio'): user = 3 print("PERMISSIONS: { ") pprint.pprint(request.user.get_all_permissions()) print(" }") fs = FileSystemStorage() var = fs.listdir('') # listing of files print("FILES IN DIR : { ") pprint.pprint(var[1]) print(" }") all = status.objects.all() return render(request, 'molbank/uploadCSV.html', {'flag_pending':False,'pending': var[1], 'flag': True, 'pill': 'uploadCSV', 'allstat': all,'user':user,'username':request.user.username}) elif request.user.has_perm('molbank.is_slab'): user = 4 print("PERMISSIONS: { ") pprint.pprint(request.user.get_all_permissions()) print(" }") fs = FileSystemStorage() var = fs.listdir('') # listing of files print("FILES IN DIR : { ") pprint.pprint(var[1]) print(" }") all = status.objects.all() return render(request, 'molbank/uploadCSV.html', {'flag_pending':False,'pending': var[1], 'flag': True, 'pill': 'uploadCSV', 'allstat': all, 'user':user,'username':request.user.username}) return render(request, 'molbank/uploadCSV.html',{'flag': False,'pill':'uploadCSV','username':request.user.username})
def buscar_pdfs_pedidos(pk, field='', pedido=None, fecha_pedido=None): from FacturasNorte.custom_classes import Factura cliente = get_object_or_404(Cliente, nroUsuario=pk) storageManager = FileSystemStorage() facturas = storageManager.listdir(config.PDF_FACTURAS)[1] pedidos = storageManager.listdir(config.PDF_PEDIDOS)[1] PDFs = [] if field != '': if field == '2': query = pedido else: query = fecha_pedido for ped in pedidos: check = ped.split('_')[0] if check == 'PED': cuit = ped.split('_')[3].split('.')[0] if (cuit == cliente.cuit): nroPed = ped.split('_')[1] fechaPed = ped.split('_')[2] fechaPed = obtener_fecha_factura(fechaPed) rutaPed = config.CARPETA_PEDIDOS + ped if (field == '') or \ ((field == '2') and (query == nroPed)) or \ ((field == '4') and (query == fechaPed)): nroFac = None fechaFac = None rutaFac = None for fac in facturas: check = fac.split('-')[0] if check == 'fac': pedido_factura = fac.split('-')[3] if (nroPed == pedido_factura): nroFac = fac.split('-')[2] fechaFac = fac.split('-')[4].split('.')[0] #fechaFac = obtener_fecha_factura(fechaFac) rutaFac = config.CARPETA_FACTURAS + fac pdf = Factura() pdf.set_nroPedido(nroPed) pdf.set_nroFactura(nroFac) pdf.set_fechaPed(fechaPed) pdf.set_fechaFac(fechaFac) pdf.set_rutaFac(rutaFac) pdf.set_rutaPed(rutaPed) PDFs.append(pdf) return PDFs
class TrailDirectoryService: filesystem = None directory = '' gpx_file_name_without_path = '' def __init__(self, directory): self.directory = directory self.filesystem = FileSystemStorage( path.join(settings.MEDIA_ROOT, settings.DIARY_FILES_SUBDIRECTORY, self.directory)) def chunk(self, lst, n): return zip(*[iter(lst)] * n) def get_gpx_file_name(self): filename = self._get_gpx_file_name_without_path() return self._get_relative_path_to_document_root(filename) def _get_gpx_file_name_without_path(self): if self.gpx_file_name_without_path != '': return self.gpx_file_name_without_path for file in self.filesystem.listdir('.')[1]: if file.endswith(".gpx"): self.gpx_file_name_without_path = file return self.gpx_file_name_without_path def get_gpx_points(self): points = [] gpx_file_name = self._get_gpx_file_name_without_path() gpx_file = self.filesystem.open(gpx_file_name, 'r') gpx = gpxpy.parse(gpx_file) gpx.simplify(5) first_segment = gpx.tracks[0].segments[0] for point in first_segment.points: new_point = (point.latitude, point.longitude) points.append(new_point) return points def _get_relative_path_to_document_root(self, filename): return path.join(settings.DIARY_FILES_SUBDIRECTORY, self.directory, filename) def get_start_position(self): first_point = self.get_gpx_points()[0] return first_point def get_responsive_images(self): responsive_images_chunked = [] responsive_images_with_path = [] responsive_images = self.filesystem.listdir('./')[1] for responsive_image in responsive_images: responsive_images_with_path.append( self._get_relative_path_to_document_root(responsive_image)) responsive_images_with_path.sort() for chunk in self.chunk(responsive_images_with_path, 3): responsive_images_chunked.append(chunk) return responsive_images_chunked
def extract_sub(course, assign, user): #hemant fs = FileSystemStorage() user_file_path = course + '/' + assign + '/' + user if fs.exists(user_file_path): if len(fs.listdir(user_file_path)[1]) > 0: file_name = fs.listdir(user_file_path)[1][0] if not fs.exists(user_file_path + '/' + file_name.rstrip('.zip')): zipf = zipfile.ZipFile(settings.MEDIA_ROOT + '/' + user_file_path + '/' + file_name) zipf.extractall(settings.MEDIA_ROOT + '/' + user_file_path)
def get_images(request, image_name=""): jwtToken = request.META['HTTP_AUTHORIZATION'] images = [] image = [] error = "False" try: payload = jwt.decode(jwtToken, 'bhandari', True) if image_name: fs = FileSystemStorage() files = fs.listdir('') if not files[1]: return HttpResponse( json.dumps({ "image": image, "images": images, "error": "no images in media folder" })) # for f in files: for img in files[1]: if jwtToken in img and image_name in img: image.append("/media/" + img) if not image: error = "no image by the given name or given token" else: fs = FileSystemStorage() files = fs.listdir('') if not files[1]: return HttpResponse( json.dumps({ "image": image, "images": images, "error": "no images in media folder" })) # for f in files: for img in files[1]: if jwtToken in img: images.append("/media/" + img) if not images: error = "no images for given token" except jwt.DecodeError: error = "Decoding Error" except jwt.ExpiredSignature: error = "Signature Expired" except jwt.InvalidTokenError: error = "Invalid Token" return HttpResponse( json.dumps({ "image": image, "images": images, "error": error }))
def getzip(course, assign): fs = FileSystemStorage() dir_fil = course + '/' + assign + '/' files, subdir = fs.listdir(dir_fil) s = io.BytesIO() zipf = zipfile.ZipFile(s, "w") for file in files: if file != '__grades': file_name = fs.listdir(dir_fil + file)[1][0] zipf.write( settings.MEDIA_ROOT + '/' + dir_fil + file + '/' + file_name, file + '/' + file_name) zipf.close() return s
def getFoldersAndFiles(self, folder=''): """ list: Return a list of all folders. """ storage = FileSystemStorage(os.path.join(settings.MEDIA_ROOT, 'models')) result = list() base_path = "" if folder.startswith("/"): base_path = folder[1:] elif folder: base_path = f"{folder}/" items = storage.listdir(base_path) denied_folders = [] if not self.current_user.has_perm('pyplan.change_group_permissions'): denied_folders = self._getDeniedFolders() # folders for item in sorted(items[0], key=str.lower): full_path = f"{base_path}{item}" if not denied_folders or not item in denied_folders: result.append( FileEntry( show=not item.startswith('.'), text=item, type=eFileTypes.MY_FOLDER, data=FileEntryData( fileSize=None, fullPath=full_path, # specialFolderType=eSpecialFolder.MODELS_PATH lastUpdateTime=storage.get_modified_time( full_path), ))) # files for item in sorted(items[1], key=str.lower): full_path = f"{base_path}{item}" specialFileType = eSpecialFileType.FILE lowerItem = item.lower() if lowerItem.endswith('.ppl') | lowerItem.endswith('.cbpy') | \ lowerItem.endswith('.model') | lowerItem.endswith('.ana'): specialFileType = eSpecialFileType.MODEL elif lowerItem.endswith('.zip'): specialFileType = eSpecialFileType.ZIP result.append( FileEntry( text=item, type=eFileTypes.PUBLIC, data=FileEntryData( fileSize=storage.size(full_path), fullPath=full_path, extension=full_path[full_path.rfind('.') + 1:], specialFileType=specialFileType, lastUpdateTime=storage.get_modified_time(full_path), ))) return result
def predictFungiClassUsingOverfeatViewZIP(request): """CURL example: curl -X POST -F [email protected] 'http://193.146.250.57:8000/fungi_classification/predictFungiClass/zip/'; echo "" """ if request.method == 'POST': zip_file = request.FILES['zip'] fs = FileSystemStorage() extension = zip_file.name[zip_file.name.rfind("."):] ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d-%H-%M-%S') fs.save("zip-files/" + st + extension, zip_file) zip_ref = zipfile.ZipFile("media/zip-files/" + st + extension, 'r') zip_ref.extractall("media/zip-files/" + st) zip_ref.close() fs.delete("zip-files/" + st + extension) (_, files) = fs.listdir("zip-files/" + st) print(files) predictions = [ ("../../../media/zip-files/" + st + "/" + file, categoryOfFungiImageUsingOverfeat( _grab_image(path="media/zip-files/" + st + "/" + file), oe)) for file in files ] data = {"success": True} data.update({"predictions": predictions}) predictionJSON = JsonResponse(data) return render(request, 'FungiClassification/zip_upload.html', json.loads(predictionJSON.content)) return render(request, 'FungiClassification/zip_upload.html')
def index(request): fs = FileSystemStorage() filesList = fs.listdir(settings.MEDIA_ROOT) context = { 'files' : filesList[1], } return render(request, 'polls/index.html', context)
def search(request): category_list = Category.objects.all() file_system = FileSystemStorage() keyword = request.GET.get('keyword') if keyword: post_list = Post.objects.all().order_by('-id') post_list = post_list.filter( Q(title__icontains=keyword) | Q(content__icontains=keyword) | Q(tag__icontains=keyword)) paginator = Paginator(post_list, 19) page_info = request.GET.get('page_info') post_list = paginator.get_page(page_info) for index_number in range(len(post_list)): try: file_dir = file_system.listdir( post_list[index_number].file_dir) for file in file_dir[1]: if "title_image" in file: post_list[index_number].file_dir = post_list[ index_number].file_dir + file except FileNotFoundError: post_list[index_number].file_dir = "1" else: post_list = None return render(request, 'search.html', { 'post_list': post_list, 'keyword': keyword, 'category_list': category_list })
def index(request): category_list = Category.objects.all() file_system = FileSystemStorage() posts = Post.objects.all().order_by('-id') paginator = Paginator(posts, 19) page_info = request.GET.get('page_info') page_info = paginator.get_page(page_info) posts = list(page_info) for index in range(len(posts)): try: file_dir = file_system.listdir(posts[index].file_dir) for file in file_dir[1]: if "title_image" in file: posts[index].file_dir = posts[index].file_dir + file except FileNotFoundError: posts[index].file_dir = "1" post = 0 if posts: post = posts[0] posts = posts[1:] return render( request, 'index.html', { 'post': post, 'posts': posts, 'page_info': page_info, 'category_list': category_list })
def category(request): category_list = Category.objects.all() file_system = FileSystemStorage() post_list = Post.objects.all().order_by('-id') category = request.GET.get('category') category = Category.objects.get(category_name=category) post_list = post_list.filter(category=category) paginator = Paginator(post_list, 5) page_info = request.GET.get('page_info') post_list = paginator.get_page(page_info) for index_number in range(len(post_list)): try: file_dir = file_system.listdir(post_list[index_number].file_dir) for file in file_dir[1]: if "title_image" in file: post_list[index_number].file_dir = post_list[ index_number].file_dir + file except FileNotFoundError: post_list[index_number].file_dir = "1" return render( request, 'category.html', { 'post_list': post_list, 'category': category, 'category_list': category_list })
def delete(request, image_name): error = "False" msg = "" flag = 1 jwtToken = request.META['HTTP_AUTHORIZATION'] try: payload = jwt.decode(jwtToken, 'bhandari', True) fs = FileSystemStorage() files = fs.listdir('') if not files[1]: return HttpResponse( json.dumps({ "msg": "", "error": "no images in media folder" })) for img in files[1]: if jwtToken in img and image_name in img: fs.delete(img) msg = "image deleted" flag = 0 if flag == 1: error = "no images by the given name or given token" except jwt.ExpiredSignature: error = "Signature Expired" except jwt.DecodeError: error = "Decoding Error" except jwt.InvalidTokenError: error = "Invalid Token" return HttpResponse(json.dumps({"msg": msg, "error": error}))
class RasterOptimizerView(TemplateView): template_name = "admin/imageserver/raster_optimizer.html" form = ActionForm def __init__(self): # FIXME: support for multiple directories print('DIR', settings.GISCUBE_IMAGESERVER['DATA_ROOT'][0]) self.storage = FileSystemStorage( settings.GISCUBE_IMAGESERVER['DATA_ROOT'][0]) def get_context_data(self, **kwargs): context = super(RasterOptimizerView, self).get_context_data(**kwargs) dir = self.request.GET.get('dir', '') context['dir'] = dir if dir.startswith('/'): dir = dir[1:] dirs, files = self.storage.listdir(dir) entries = [] for f in sorted(dirs): e = OptimizerEntry() e.name = f e.type = 'folder' e.path = os.path.join(dir, f) entries.append(e) for f in sorted(files): e = OptimizerEntry() e.name = f e.type = 'file' e.path = os.path.join(dir, f) try: e.size = self.storage.size(e.path) except Exception: e.size = 0 for format in ('.tif', '.jpg'): if f.lower().endswith(format): e.overview = True entries.append(e) context['entries'] = entries return context def post(self, request, *args, **kwargs): f = self.form(request.POST) if f.is_valid(): action = f.cleaned_data['action'] path = f.cleaned_data['path'] if action == 'delete': self.storage.delete(path) elif action == 'build_overviews': gdal_build_overviews(self.storage.path(path)) return HttpResponse('Overviews completed for %s' % path) else: return HttpResponse(f.errors) return HttpResponse('OK')
def deleteMediaDir(probId, hintId): fs = FileSystemStorage() location = fs.location base_url = fs.base_url file_permissions_mode = fs.file_permissions_mode directory_permissions_mode = fs.directory_permissions_mode probLoc = os.path.join(location, QUICKAUTH_PROB_DIRNAME, Problem.getProblemDirName(probId)) hintLoc = os.path.join(location, QUICKAUTH_PROB_DIRNAME, Problem.getProblemDirName(probId), Hint.getHintDirName(hintId)) # Create a new FileSystemStorage object based on the default one. It uses the new directory for the problem. fs = FileSystemStorage( location=probLoc, file_permissions_mode=file_permissions_mode, directory_permissions_mode=directory_permissions_mode) fs2 = FileSystemStorage( location=hintLoc, file_permissions_mode=file_permissions_mode, directory_permissions_mode=directory_permissions_mode) if not fs.exists(Hint.getHintDirName(hintId)): return stuff = fs.listdir(Hint.getHintDirName( hintId)) # returns a 2-tuple of lists ([dirs], [files]) files = stuff[1] #deletes all the files in the hint dir for f in files: fs2.delete(f) os.rmdir(hintLoc)
def get_card(request): if request.method == 'POST': import json resp_data = json.loads(request.body.decode('utf-8')) cards = Str_obj.objects.all() for card in cards: if card.id == int(resp_data['obj_id']): folder = "fstr/static/file_folder/" + str(card.id) try: fs = FileSystemStorage(location=folder, base_url=folder) fs_urls = [] for f in fs.listdir("")[1]: fs_urls.append(fs.url(f)) resp = serializers.serialize('json', [ card, ]) fs_f = json.dumps({'list_files': fs_urls}) return HttpResponse( json.dumps({ 'resp': resp, 'files': fs_f })) except: resp = serializers.serialize('json', [ card, ]) return HttpResponse(json.dumps({'resp': resp}))
def get_images(request, image_name=""): jwtToken = request.META['HTTP_AUTHORIZATION'] images = [] image = "" error = "False" try: payload = jwt.decode(jwtToken, 'bhandari', True) if image_name: fs = FileSystemStorage() if fs.exists(jwtToken + image_name): image = fs.url(jwtToken + image_name) else: image = "image not found" else: fs = FileSystemStorage() files = fs.listdir('') if not files: error="NO images to get" for f in files: for img in f: if jwtToken in img: images.append("/media/" + img) except jwt.DecodeError: error = "Decoding Error" except jwt.ExpiredSignature: error = "Signature Expired" except jwt.InvalidTokenError: error = "Invalid Token" return HttpResponse(json.dumps({"image": image, "images": images, "error": error}))
def rm_mem_from_proj(request): if request.method == "PUT": try: res = QueryDict(request.body) proj = Project.objects.get(id=res.get("project_id")) for member in res.get("users_arr").split(","): rm_proj_user_ws = proj.workspace_set.filter(user=member) for ws in rm_proj_user_ws: fs = FileSystemStorage() if fs.exists(ws.file_path): for file in fs.listdir(ws.file_path)[1]: fs.delete(ws.file_path + file) ws.delete() proj.members.remove( Member.objects.get(usr=User.objects.get(username=member))) LogMessage(log=proj.log, log_type="remove_member", user=res.get("username"), timestamp=timezone.now(), body=res.get("username") + " removed " + member + " from the project").save() return HttpResponse( reverse('projectlab:view_project', args=( res.get("username"), proj.id, ))) except ObjectDoesNotExist: return render(request, 'projectlab/403.html', {'user': user}) except: return HttpResponseServerError( "Error updating project. Please try again.")
def workspace_history(request, acc, proj_id, workspace_id): if acc == request.user.username: try: user = Member.objects.get(usr=User.objects.get( username=request.user.username)) proj = user.project_set.get(id=proj_id) workspace = proj.workspace_set.get(id=workspace_id) if not workspace.current: return render(request, 'projectlab/403.html', {'user': user}) history = [] fs = FileSystemStorage() id_current = workspace.last_workplace_id while id_current != -1: ws_current = Workspace.objects.get(id=id_current) history.append( [ws_current, fs.listdir(ws_current.file_path)[1]]) id_current = Workspace.objects.get( id=id_current).last_workplace_id return render( request, 'projectlab/workspace_history.html', { 'user': user, 'project': proj, 'workspace': workspace, 'history': history, 'location': fs.base_url }) except ObjectDoesNotExist: return render(request, 'projectlab/403.html', {'user': user}) else: return HttpResponseRedirect(reverse('projectlab:login'))
def view_workspace(request, acc, proj_id, workspace_id): if acc == request.user.username: try: user = Member.objects.get(usr=User.objects.get( username=request.user.username)) proj = user.project_set.get(id=proj_id) workspace = proj.workspace_set.get(id=workspace_id) if not workspace.current: return render(request, 'projectlab/403.html', {'user': user}) fs = FileSystemStorage() files = fs.listdir(workspace.file_path) return render( request, 'projectlab/workspace.html', { 'user': user, 'project': proj, 'workspace': workspace, 'messages': workspace.message_set.all().order_by('-timestamp'), 'files': files[1], 'location': fs.base_url }) except ObjectDoesNotExist: return render(request, 'projectlab/403.html', {'user': user}) except FileNotFoundError: return render( request, 'projectlab/workspace.html', { 'user': user, 'project': proj, 'workspace': workspace, 'messages': workspace.message_set.all().order_by('-timestamp'), 'files': [] }) else: return HttpResponseRedirect(reverse('projectlab:login'))
def get_auto_len(course, assign): #jishnu fs = FileSystemStorage() return [ len( os.listdir('media/' + course + '/' + assign + '/__grades/auto_grades')), len(fs.listdir(course + '/' + assign)[0]) - 1 ]
def get_deploy_packages_name(): fs = FileSystemStorage() directories, files = fs.listdir(settings.MEDIA_ROOT) dir_creation_time = [(fs.get_created_time(directory), directory) for directory in directories] dir_creation_time.sort(key=lambda tup: tup[0], reverse=True) uploaded_package = [d[1] for d in dir_creation_time] return uploaded_package
def user_result(request): fs = FileSystemStorage(location=settings.RESULT_ROOT+'/'+request.user.username) filelist = fs.listdir(settings.RESULT_ROOT+'/'+request.user.username) result_list = [] for f in filelist[1]: if 'html' in f: result_list.append(f) return render(request, 'result.html', {"resultlist":result_list})
def deleteImage(request, image_name): fs = FileSystemStorage() fs.delete(settings.MEDIA_ROOT + '/' + image_name) filesList = fs.listdir(settings.MEDIA_ROOT) context = { 'files' : filesList[1], } return render(request, 'polls/index.html', context)
def submit_assign(request, course, announce): #mod by hemant if request.user.is_authenticated: if request.method == 'POST': upload_file = request.FILES['document'] fs = FileSystemStorage() user = request.user.get_username() user_file_path = course + '/' + announce + '/' + user if fs.exists(user_file_path) and ( fs.listdir(user_file_path)[1]) != 0: for fil in fs.listdir(user_file_path)[1]: fs.delete(user_file_path + '/' + fil) fs.save(user_file_path + '/' + upload_file.name, upload_file) return render(request, 'files/upload.html', { 'course': course, 'announce': announce }) else: return redirect('account/login')
def get_html_code(self, context): file_storage = FileSystemStorage(location=settings.STATICFILES_DIRS[0], base_url=settings.STATIC_URL) if not file_storage.exists('downloads') or not file_storage.listdir('downloads')[0]: # note: listdir returns a tuple:- (directory, files). We wish to use folders as headings and the files in # the sub folders for links, if the download folder has only files without folders, we ignore them because # they will not be used return self.empty_html_message() return self.build_html(file_storage)
def listdir(self, path): ''' Lists a directory, stripping out /djmodules/static from the directory (and allowing / and /djmodules to work) ''' if path == "" or path == "/": return ["djmodules"], [] elif path in ["djmodules", "djmodules/", "/djmodules", "/djmodules/"]: return [self.base_url.split('/')[1]], [] else: return FileSystemStorage.listdir(self, path)
def setUp(self): test_storage = FileSystemStorage( location=getattr(settings, 'FILE_UPLOAD_TEMP_DIR')) fixtures_storage = FileSystemStorage(location=CHUNKS_ROOT) for filename in fixtures_storage.listdir('.')[1]: test_storage.save(filename, fixtures_storage.open(filename)) self.seagull = ResumableFile(test_storage, seagull) self.craw = ResumableFile(test_storage, craw) self.storage = test_storage
def delete_file(request): if request.method == "POST": for obj in Str_obj.objects.all(): print(obj.id) print(str(request.POST['id'])) if str(obj.id) == str(request.POST['id']): print('OK') folder = "fstr/static/file_folder/" + str(obj.id) fs = FileSystemStorage(location=folder, base_url=folder) print(fs.listdir('')[1]) for f in fs.listdir('')[1]: print(f) if str(f) == unquote(str(request.POST['file'])): default_storage.delete( "fstr/static/file_folder/" + str(obj.id) + '/' + unquote(str(request.POST['file']))) print('OK') return HttpResponse('OK')
def setUp(self): test_storage = FileSystemStorage( location=getattr(settings, 'FILE_UPLOAD_TEMP_DIR')) fixtures_storage = FileSystemStorage(location=CHUNKS_ROOT) for filename in fixtures_storage.listdir('.')[1]: test_storage.save( filename, fixtures_storage.open(filename) ) self.seagull = ResumableFile(test_storage, seagull) self.craw = ResumableFile(test_storage, craw) self.storage = test_storage
def delete_screencaps(): """ This func cleans up any screencaps that are 3 hours or older. It cleans up the mess that monitor.tasks.test_screencap creates This is a scheduled task. Let's have celerybeat run it. """ upload_storage = FileSystemStorage(location=getattr(settings, 'MONITOR_ROOT')) files = upload_storage.listdir(getattr(settings, 'MONITOR_ROOT'))[1] # If the file is over three hours old, delete it. for f in files: age_of_file = datetime.now() - upload_storage.created_time(f) if age_of_file.seconds > 10800: upload_storage.delete(f)
class DummyExternalStorage(Storage): def __init__(self, *args, **kwargs): self.wrapped = FileSystemStorage(*args, **kwargs) def path(self, name): # Overridden to give it the behaviour of the base Storage class # This is what an external storage backend would have raise NotImplementedError("This backend doesn't support absolute paths.") def _open(self, name, mode='rb'): # Overridden to return a DummyExternalStorageFile instead of a normal # File object return DummyExternalStorageFile(open(self.wrapped.path(name), mode)) # Wrap all other functions def _save(self, name, content): return self.wrapped._save(name, content) def delete(self, name): self.wrapped.delete(name) def exists(self, name): return self.wrapped.exists(name) def listdir(self, path): return self.wrapped.listdir(path) def size(self, name): return self.wrapped.size(name) def url(self, name): return self.wrapped.url(name) def accessed_time(self, name): return self.wrapped.accessed_time(name) def created_time(self, name): return self.wrapped.created_time(name) def modified_time(self, name): return self.wrapped.modified_time(name)
def obtener_diarios(fecha=None): from FacturasNorte.custom_classes import Diario storageManager = FileSystemStorage() diarios = storageManager.listdir(config.CARPETA_DIARIOS)[1] lista_diarios = [] for d in diarios: if fecha: if fecha == obtener_fecha_diario(d): pdf = Diario() pdf.set_fecha(obtener_fecha_diario(d)) pdf.set_ruta(d) lista_diarios.append(pdf) else: pdf = Diario() pdf.set_fecha(obtener_fecha_diario(d)) pdf.set_ruta(d) lista_diarios.append(pdf) lista_diarios.sort(key=lambda x: x.fecha, reverse=True) return lista_diarios
def obtener_diarios_2(fecha=None): from FacturasNorte.custom_classes import Diario storageManager = FileSystemStorage() diarios = storageManager.listdir(config.CARPETA_DIARIOS2)[1] lista_diarios = [] for list1 in listdir(diarios): for list2 in listdir(list1): for list3 in listdir(list2): for list4 in listdir(list3): for list5 in listdir(list4): if isfile(join(list5, f)): lista_diarios.append(list5) return lista_diarios for d in diarios: if fecha: if fecha == obtener_fecha_diario(d): pdf = Diario() pdf.set_fecha(obtener_fecha_diario(d)) pdf.set_ruta(d) lista_diarios.append(pdf) else: pdf = Diario() pdf.set_fecha(obtener_fecha_diario(d)) pdf.set_ruta(d) lista_diarios.append(pdf) lista_diarios.sort(key=lambda x: x.fecha, reverse=True) return lista_diarios
class Command(NoArgsCommand): help = "Purges all files from `VERSIONED_STATIC_ROOT` that are not "\ "current versions of static files. (Files where the version "\ "*matches* the source file's current version are not removed, "\ "unless the `--all` flag is given.)" option_list = NoArgsCommand.option_list + ( make_option('-n', '--dry-run', action='store_true', dest='dry_run', default=False, help="Do everything except modify the filesystem."), make_option('-i', '--ignore', action='append', default=[], dest='ignore_patterns', metavar='PATTERN', help="Ignore files or directories matching this glob-style " "pattern. Use multiple times to ignore more."), make_option('--all', action='store_true', dest='all', default=False, help='Removes *everything* from `VERSIONED_STATIC_ROOT`, not just '\ 'old versions of static files.'), make_option('--noinput', action='store_true', dest='interactive', default=False, help='Does not prompt for confirmation when deleting.'), make_option('--no-default-ignore', action='store_false', dest='use_default_ignore_patterns', default=True, help="Don't ignore the common private glob-style patterns 'CVS', " "'.*' and '*~'."), ) def __init__(self, *args, **kwargs): super(Command, self).__init__(*args, **kwargs) self.deleted_files = [] self.unmodified_files = [] # Force storage to be a filesystem storage for VERSIONED_STATIC_ROOT. self.storage = get_storage_class(settings.STATICFILES_STORAGE)() self.versioned_storage = FileSystemStorage(location=settings.VERSIONED_STATIC_ROOT) def handle_noargs(self, **options): self.verbosity = int(options.get('verbosity', 1)) ignore_patterns = options['ignore_patterns'] if options['use_default_ignore_patterns']: ignore_patterns += ['CVS', '.*', '*~'] ignore_patterns = list(set(ignore_patterns)) # Warn before doing anything more. if options.get('interactive'): confirm = raw_input(u""" You have requested to delete versioned files at the destination location as specified in your settings file. This will remove existing files. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """) if confirm != 'yes': raise CommandError("Collecting static files cancelled.") # Get output filenames for up-to-date static files. self.valid_outpaths = set() if not options['all']: for finder in finders.get_finders(): for path, storage in finder.list(ignore_patterns): # Prefix the relative path if source storage contains it if getattr(storage, 'prefix', None): prefixed_path = os.path.join(storage.prefix, path) else: prefixed_path = path current_version = get_file_version(path, storage) current_outpath = get_versioned_path( prefixed_path, current_version) self.valid_outpaths.add(current_outpath) # Find everything currently in VERSIONED_STATIC_ROOT and remove # anything not in `self.valid_outpaths` def cleardirs(cd, subdirs, files): for d in subdirs: new_cd = os.path.join(cd, d) new_subdirs, new_files = self.versioned_storage.listdir( os.path.join(cd, d) ) cleardirs(new_cd, new_subdirs, new_files) for f in files: filepath = os.path.join(cd, f) if filepath not in self.valid_outpaths: if options['dry_run']: self.log(u"Pretending to delete '%s'" % filepath) else: self.log(u"Deleting '%s'" % filepath) self.versioned_storage.delete(filepath) self.deleted_files.append(filepath) else: self.log(u"Skipping up-to-date file '%s'" % filepath) self.unmodified_files.append(filepath) subdirs, files = self.versioned_storage.listdir("") cleardirs("", subdirs, files) actual_count = len(self.deleted_files) unmodified_count = len(self.unmodified_files) if self.verbosity >= 1: self.stdout.write(smart_str(u"\n%s versioned file%s %s%s.\n" % (actual_count, actual_count != 1 and 's' or '', 'deleted', unmodified_count and ' (%s unmodified)' % unmodified_count or ''))) def log(self, msg, level=2): """ Small log helper """ msg = smart_str(msg) if not msg.endswith("\n"): msg += "\n" if self.verbosity >= level: self.stdout.write(msg) def remove_old_versions(self, path, prefixed_path, source_storage, **kwargs): # For iglob matching to work, we actually need the full path # of the output file. outpath = os.path.join(settings.VERSIONED_STATIC_ROOT, prefixed_path) current_version = get_file_version(path, source_storage) current_outpath = get_versioned_path(outpath, current_version) # Match any possible version of the output file. for matching_path in iglob(get_versioned_path(outpath, "*")): # Remove VERSIONED_STATIC_ROOT just for output log, etc. rel_matching_path = matching_path.replace( settings.VERSIONED_STATIC_ROOT, "", 1 )[1:] if kwargs['all'] or (matching_path != current_outpath): self.deleted_files.append(matching_path) if kwargs['dry_run']: self.log(u"Pretending to delete '%s' (Current is '%s')" % ( rel_matching_path, current_version )) else: self.log(u"Deleting '%s' (Current is '%s')" % ( rel_matching_path, current_version )) os.unlink(matching_path) self.versioned_storage.delete(matching_path) else: self.unmodified_files.append(matching_path) self.log(u"Skipping '%s'. (Current version.)" % rel_matching_path)
def listdir(self, path): file_system = FileSystemStorage() return file_system.listdir(path)
def listdir(self, name): return FileSystemStorage.listdir(self, name)