I am uploading a CSV file then adding the rows in the database and I want to able to pause, resume or terminate the function that handles the upload
so the user can pause or terminate while the file is still uploading the file to the server or when the server is adding the content of the CSV file into the database
I tried to use threading but I couldn't do it that way and then I tried to time.sleep()
but it just runs and it does not pause the show function and when I try to stop the pause function it does not stop
views.py
is_paused = False
def show(request):
content = {}
if not is_paused:
if request.method =='POST':
uploaded_file = request.FILES['file']
print(uploaded_file.name)
print(uploaded_file.size)
fs = FileSystemStorage()
fs.save(uploaded_file.name,uploaded_file)
upload = Upload()
upload.title = uploaded_file.name
upload.file = uploaded_file
upload.save()
filecontent = file_content()
path = '/home/ahmed/Desktop/atlan/atlan/media/'+uploaded_file.name
with open(path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader)
for row in csv_reader:
filecontent.seq = row[0]
filecontent.First_name =row[1]
filecontent.Last_name = row[2]
filecontent.age = row[3]
filecontent.street = row[4]
filecontent.city = row[5]
filecontent.state = row[6]
filecontent.zipcode = row[7]
filecontent.dollar = row[8]
filecontent.color = row[9]
filecontent.date = row[10]
file_content.CSV_ID = upload.ID
filecontent.save()
else:
time.sleep(2)
return render(request,'upload/upload.html', content)
def pause_upload(request):
global is_paused
if is_paused == True:
is_paused = False
else:
is_paused = True
return HttpResponse("PAUSED")
def resume(request):
stop_threads = 0
t1 = Thread(target = pause_upload, args =(lambda : stop_threads, ))
t1.start()
time.sleep(1)
stop_threads = 1
t1.join()
print('thread continued')
return render(request,'upload/upload.html')
def stop(request):
pause_upload(request,stop=True)
sys.exit()
return HttpResponse(request,'stopped')
upload.html
<body>
<div class="container">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="custom-file">
<label class="text" for="title">title</label>
<input type="text" class="text" name="title">
</div>
<div class="custom-file">
<label class="file-label" for="file">Browse</label>
<input type="file" class="file-input" name="file">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<div class="container">
<a href="{% url 'pause_upload' %}" class="btn btn-danger">Pause</a>
<a href="{% url 'stop' %}" class="btn btn-danger pull-right">Stop</a>
<a href="{% url 'resume' %}" class="btn btn-success pull-right">Resume</a>
</div>
</div>
</body>