def view_directory(request, path): parent_path = fs.parent_path(path) context_files = [] context_dirs = [] dirs = [] files = [] path_regex = '^' + path + '/[^/]+$' try: d = Directory.objects.get(path=path) except: return HttpResponse('Not Found') try: dirs = Directory.objects.filter(path__regex=path_regex) except: pass for single in dirs: context_dirs.append({'path': single.path, 'name': fs.file_name(single.path)}) files = File.objects.filter(path=d) for single in files: file_path = single.path.path + '/' + single.name context_files.append({'path': file_path, 'name': single.name}) response = render(request, 'directory.html', {'remote': REMOTE_PREFIX, 'dirs': context_dirs, 'files': context_files}) return response
def view_directory(request, path): parent_path = fs.parent_path(path) context_files = [] context_dirs = [] dirs = [] files = [] path_regex = '^' + path + '/[^/]+$' try: d = Directory.objects.get(path=path) except: return HttpResponse('Not Found') try: dirs = Directory.objects.filter(path__regex=path_regex) except: pass for single in dirs: context_dirs.append({'path': single.path, 'name': fs.file_name(single.path)}) files = File.objects.filter(path=d) for single in files: file_path = single.path.path + '/' + single.name context_files.append({'path': file_path, 'name': single.name}) response = render(request, 'directory.html', {'dir_path': path, 'remote': REMOTE_PREFIX, 'dirs': context_dirs, 'files': context_files}) return response
def bulk_delete(request): if request.method == 'POST': report = [] file_data= request.POST['files'] files = json.load(file_data) for entry in files: remote_path = request.POST['path'] name = fs.file_name(remote_path) remote_parent = fs.parent_path(remote_path) try: remote_dir = Directory.objects.get(path=remote_parent) tmp_file = File.objects.get(name=name, path=remote_dir) tmp_file.delete() report.append({'status': '1', 'name': name, 'message': 'File delete succesfully.'}) except: report.append({'status': '0', 'name': name, 'message': 'Path does not exist.'}) else: report.append({'status': '0', 'message': 'Use POST for deleting a file.'}) response = render(request, 'response.xml', {'report': report}, content_type='text/xml') return response
def search(request, text): keywords = text.split(' ') regex = '' for keyword in keywords: regex = regex + keyword + '|' regex = regex[:-1] dirs = Directory.objects.filter(path__iregex=regex) files = File.objects.filter(name__iregex=regex) res_dirs = [] res_files = [] for dir in dirs: folder_name = fs.file_name(dir.path) if re.search(regex, folder_name, re.I): res_dirs.append({'path': dir.path, 'name': fs.file_name(dir.path)}) for file in files: res_files.append({'path': file.path.path, 'name': file.name}) json_obj = {'directories': res_dirs, 'files': res_files} json_str = json.dumps(json_obj) return HttpResponse(json_str, content_type='application/json')
def index(request): path_regex = '^' + '[^/]+$' context_dirs = [] try: dirs = Directory.objects.filter(path__regex=path_regex) except: pass for single in dirs: context_dirs.append({'path': single.path, 'name': fs.file_name('/' + single.path)}) response = render(request, 'index.html', {'dirs': context_dirs}) return response