1- from django .shortcuts import render
2- from django .http import HttpResponse
1+ import os
2+ from django .shortcuts import render , redirect
3+ from django .http import HttpResponse , FileResponse , Http404
4+ from django .conf import settings
35
46# Create your views here.
57
@@ -12,3 +14,78 @@ def index(request):
1214
1315 # Page from the theme
1416 return render (request , 'pages/dashboard.html' , context = context )
17+
18+ def get_files_from_directory (directory_path ):
19+ files = []
20+ for root , _ , filenames in os .walk (directory_path ):
21+ for filename in filenames :
22+ file_path = os .path .join (root , filename )
23+ files .append ({
24+ 'file' : file_path .split ('/media/' )[1 ],
25+ 'filename' : filename ,
26+ 'file_path' : file_path
27+ })
28+ return files
29+
30+
31+ def file_manager (request ):
32+ media_path = os .path .join (settings .MEDIA_ROOT )
33+ directories = generate_nested_directory (media_path , media_path )
34+ selected_directory = request .GET .get ('directory' , '' )
35+
36+ files = []
37+ if selected_directory :
38+ selected_directory_path = os .path .join (media_path , selected_directory )
39+ if os .path .isdir (selected_directory_path ):
40+ files = get_files_from_directory (selected_directory_path )
41+
42+ context = {
43+ 'directories' : directories ,
44+ 'files' : files ,
45+ 'selected_directory' : selected_directory ,
46+ 'segment' : 'file_manager' ,
47+ 'MEDIA_ROOT' : settings .MEDIA_ROOT
48+ }
49+ return render (request , 'pages/file-manager.html' , context )
50+
51+
52+ def generate_nested_directory (root_path , current_path ):
53+ directories = []
54+ for name in os .listdir (current_path ):
55+ if os .path .isdir (os .path .join (current_path , name )):
56+ nested_path = os .path .join (current_path , name )
57+ nested_directories = generate_nested_directory (root_path , nested_path )
58+ directories .append ({'name' : name , 'path' : os .path .relpath (nested_path , root_path ), 'directories' : nested_directories })
59+ return directories
60+
61+
62+ def delete_file (request , file_path ):
63+ path = file_path .replace ('%slash%' , '/' )
64+ absolute_file_path = os .path .join (settings .MEDIA_ROOT , path )
65+ os .remove (absolute_file_path )
66+ print ("File deleted" , absolute_file_path )
67+ return redirect (request .META .get ('HTTP_REFERER' ))
68+
69+
70+ def download_file (request , file_path ):
71+ path = file_path .replace ('%slash%' , '/' )
72+ absolute_file_path = os .path .join (settings .MEDIA_ROOT , path )
73+ if os .path .exists (absolute_file_path ):
74+ with open (absolute_file_path , 'rb' ) as fh :
75+ response = HttpResponse (fh .read (), content_type = "application/vnd.ms-excel" )
76+ response ['Content-Disposition' ] = 'inline; filename=' + os .path .basename (absolute_file_path )
77+ return response
78+ raise Http404
79+
80+ def upload_file (request ):
81+ media_path = os .path .join (settings .MEDIA_ROOT )
82+ selected_directory = request .POST .get ('directory' , '' )
83+ selected_directory_path = os .path .join (media_path , selected_directory )
84+ if request .method == 'POST' :
85+ file = request .FILES .get ('file' )
86+ file_path = os .path .join (selected_directory_path , file .name )
87+ with open (file_path , 'wb' ) as destination :
88+ for chunk in file .chunks ():
89+ destination .write (chunk )
90+
91+ return redirect (request .META .get ('HTTP_REFERER' ))
0 commit comments