Skip to content

Commit c44ae12

Browse files
author
mominur-helios
committed
convert csv to text
1 parent bde0e53 commit c44ae12

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

home/views.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import uuid
3+
import csv
34
from django.shortcuts import render, redirect
45
from django.http import HttpResponse, FileResponse, Http404
56
from django.conf import settings
@@ -16,23 +17,40 @@ def index(request):
1617
# Page from the theme
1718
return render(request, 'pages/dashboard.html', context=context)
1819

20+
def convert_csv_to_text(csv_file_path):
21+
with open(csv_file_path, 'r') as file:
22+
reader = csv.reader(file)
23+
rows = list(reader)
24+
25+
text = ''
26+
for row in rows:
27+
text += ','.join(row) + '\n'
28+
29+
return text
30+
1931
def get_files_from_directory(directory_path):
2032
files = []
2133
for filename in os.listdir(directory_path):
2234
file_path = os.path.join(directory_path, filename)
2335
if os.path.isfile(file_path):
2436
try:
2537
print( ' > file_path ' + file_path)
38+
_, extension = os.path.splitext(filename)
39+
if extension.lower() == '.csv':
40+
csv_text = convert_csv_to_text(file_path)
41+
else:
42+
csv_text = ''
43+
2644
files.append({
2745
'file': file_path.split(os.sep + 'media' + os.sep)[1],
2846
'filename': filename,
29-
'file_path': file_path
47+
'file_path': file_path,
48+
'csv_text': csv_text
3049
})
3150
except Exception as e:
3251
print( ' > ' + str( e ) )
3352
return files
3453

35-
3654
def get_breadcrumbs(request):
3755
path_components = [component for component in request.path.split("/") if component]
3856
breadcrumbs = []

templates/pages/file-manager.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
.actions span {
1616
cursor: pointer;
1717
}
18+
.modal {
19+
z-index: 99999 !important;
20+
}
1821
</style>
1922
{% endblock extrastyle %}
2023

@@ -124,6 +127,8 @@ <h1 class="modal-title fs-5" id="staticBackdropLabel">{{ file.filename }}</h1>
124127
</video>
125128
{% elif file.filename|file_extension in ".pdf, .txt" %}
126129
<iframe src="/media/{{ file.file }}" width="100%" height="700px"></iframe>
130+
{% elif file.filename|file_extension in ".csv" %}
131+
<pre class="bg-dark text-light p-3">{{ file.csv_text }}</pre>
127132
{% endif %}
128133
</div>
129134
</div>

0 commit comments

Comments
 (0)