diff --git a/back/backend/models.py b/back/backend/models.py index 032ed0c..176c080 100644 --- a/back/backend/models.py +++ b/back/backend/models.py @@ -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("") + diff --git a/back/backend/views.py b/back/backend/views.py index b3efb87..fac08a4 100644 --- a/back/backend/views.py +++ b/back/backend/views.py @@ -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)}) diff --git a/back/db.sqlite3 b/back/db.sqlite3 index 5915594..894a6e7 100644 Binary files a/back/db.sqlite3 and b/back/db.sqlite3 differ