deleting the file in database if it exists in report
This commit is contained in:
parent
16dbd04ef4
commit
7b2c58cb2d
3 changed files with 32 additions and 4 deletions
|
@ -2,6 +2,7 @@ from django.db import models
|
|||
from django.conf import settings
|
||||
import datetime
|
||||
import ntpath
|
||||
import os
|
||||
|
||||
class Report(models.Model):
|
||||
user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
|
@ -81,7 +82,21 @@ class Field(models.Model):
|
|||
|
||||
# function that accommodates if
|
||||
# path has slash at end
|
||||
def path_leaf(self, path):
|
||||
head, tail = ntpath.split(path)
|
||||
return tail or ntpath.basename(head)
|
||||
def path_leaf(self, path, flag=1):
|
||||
dir_path, name = ntpath.split(path)
|
||||
if flag == 1:
|
||||
return name or ntpath.basename(dir_path)
|
||||
else:
|
||||
return dir_path, name
|
||||
|
||||
# def delete_data_file(self, file_path):
|
||||
# # delete file name in uploads
|
||||
# # os.remove(file_name)
|
||||
# os.remove(file_path)
|
||||
#
|
||||
# # check if directory is empty
|
||||
# # if yes, delete directory
|
||||
# # if [f for f in os.listdir("back/uploads") if not f.startswith('.')] == []:
|
||||
# # os.removedirs("")
|
||||
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ from rest_framework.decorators import api_view
|
|||
from django.http import JsonResponse
|
||||
from .models import *
|
||||
from .policy import pol
|
||||
import os
|
||||
|
||||
|
||||
# function that prints all the reports
|
||||
|
@ -133,7 +134,19 @@ def report_detail(request, report_pk):
|
|||
|
||||
# Delete the report
|
||||
elif request.method == 'DELETE':
|
||||
Report.objects.filter(id=report_pk).delete()
|
||||
# get corresponding sections
|
||||
section_set = Section.objects.filter(report_id=report_pk)
|
||||
for i in section_set:
|
||||
# gets the fields that have a field in them
|
||||
field_set = Field.objects.filter(section_id=i.id).exclude(data_file__exact='')
|
||||
if field_set.exists():
|
||||
for j in field_set:
|
||||
# delete the file if exists
|
||||
path_name = str(j.data_file)
|
||||
os.remove(path_name)
|
||||
# Field.delete_data_file(j, path_name)
|
||||
# delete the full report
|
||||
Report.objects.get(id=report_pk).delete()
|
||||
return JsonResponse({"message": "Deleted report {0}.".format(report_pk)})
|
||||
|
||||
|
||||
|
|
BIN
back/db.sqlite3
BIN
back/db.sqlite3
Binary file not shown.
Loading…
Reference in a new issue