forked from NoahGWood/OpenEFT
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
views.py
109 lines (93 loc) · 3.41 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import cv2
import time
import json
import threading
from django.http import JsonResponse, HttpResponse
from urllib.parse import quote
from django.shortcuts import render, redirect
from django.conf import settings
from django.views.decorators.http import require_http_methods
from conversion.core.core import generate_eft, section_fp, manual_section
CWD = os.getcwd()
TMP_DIR = settings.TMP_DIR
FILES = [] # List to hold generated files
# Create your views here.
def index(request):
return render(request, "conversion/index.html")
def new(request):
return render(request, "conversion/new.html")
def process_fp():
os.chdir(TMP_DIR)
for each in RESULTS:
each.convert()
def resection(request):
if request.method == "POST":
data = request.POST.dict()
fname = os.path.join(TMP_DIR, 'input.png')
out = manual_section(fname=fname, data=data)
return JsonResponse({'values': out}, safe=False)
return JsonResponse({'message': 'Invalid request method'}, status=405)
def step1(request):
global RESULTS
if request.method == "POST":
file = request.FILES.get("formFileLg")
print(file)
time.sleep(1)
fname = os.path.join(TMP_DIR, 'input.png')
with open(fname, 'wb+') as dest:
for chunk in file.chunks():
dest.write(chunk)
try:
out = section_fp(fname=fname)
except Exception as e:
print(e)
out = False
return JsonResponse({'values': out}, safe=False)
return JsonResponse({'message': 'Invalid request method'}, status=405)
def step2(request):
global FILES
if request.method == "POST":
print(dir(request))
print(request.body)
data = request.POST.dict()
print(data)
fname = generate_eft(data)
FILES.append(fname)
# Get the file sizes and add them to the context
file_info = []
for file in FILES:
file_path = os.path.join(TMP_DIR, file)
if os.path.exists(file_path):
file_size = os.path.getsize(file_path)
# Convert the size to MB and round it to 1 decimal place
file_size_mb = round(file_size / (1024 * 1024), 1)
file_info.append({'name': file, 'size': file_size_mb})
return render(request, "conversion/download.html", context={'files': file_info})
def download(request, filename):
"""
Handle the download of files.
"""
file_path = os.path.join(TMP_DIR, filename)
# Check if the file exists
if os.path.exists(file_path):
with open(file_path, 'rb') as fh:
response = HttpResponse(fh.read(), content_type="application/octet-stream")
response['Content-Disposition'] = f'attachment; filename={quote(filename)}'
return response
else:
return JsonResponse({"message": "File not found"}, status=404)
@require_http_methods(["DELETE"])
def delete_file(request, filename):
"""
Handles the deletion of files from the server
"""
global FILES
file_path = os.path.join(TMP_DIR, filename)
# Check if the file exists and remove it
if os.path.exists(file_path):
os.remove(file_path)
FILES = [f for f in FILES if f != filename] # Remove the file from the FILES list
return JsonResponse({"message": "File deleted successfully"}, status=200)
else:
return JsonResponse({"message": "File not found"}, status=404)