From 7b2c58cb2daabd3b39bb076c81f2928045fcd697 Mon Sep 17 00:00:00 2001 From: Rupika Date: Sun, 10 Feb 2019 19:23:12 -0800 Subject: [PATCH 1/3] deleting the file in database if it exists in report --- back/backend/models.py | 21 ++++++++++++++++++--- back/backend/views.py | 15 ++++++++++++++- back/db.sqlite3 | Bin 103424 -> 105472 bytes 3 files changed, 32 insertions(+), 4 deletions(-) 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 59155946e1dad1007050485e31d0852d6b644fa3..894a6e76b6ba52528ec81376750c7f6dad32f04f 100644 GIT binary patch delta 3147 zcmb`J32+nF8OOixJO|wY|S0UH|r;sdzI@5uf(oClb(@v5mGmuG>X(mIOmZnW;8K#i7-|i?$f?~$M^l8*N-Hx|7r5jBz+J78osRBop}xnzPvgq4ZTu0Esn>PkJJ7%Gea~A z7Pnw?+XR!vX|q{HMp%#lhY;GoXVRPOELGdZIU)tmC9aV``EKg zV%wC}u^DWth9l~{W9k{x{4t$3rf%hEf0c&bJu7L~ccroq*t_f<_69qvh&n&ZN#ZH! zRmG!9zAE!naVsAU8gYCQzev-7dVZB(=AZG4%7hK)xQ3q8V>E`h%3+jnwj7QUUM+_sgi~ekBH?5ij1Z2M!Z2Z^ z6b=&(l)?*y=StxaLGr^v!d5^0n6S|g2MFu@u%8h1!#+a05B3rwC9nqu0BTF%dAeOu z0=o$n#qb;z&65)1+bM+F&};iPzr(h@Fd;(yzm5Jz8AI-oL<;W5WTR8Fxvwg z2{SyffiT$vj}x*yFi0?XAWqOKC~=K}^G|{Qga3_x%KyOMQ!<}hxi>u$(XF;kEkJ+TTZXqbIVAD+)~onxqi|IbA6;! zb4p0FbBakbbBajQa|%h5a|+aVvlq~HcJ_SI%xo`ddbWo&SvoYlnuc7=_!+*57xPp$ zq-2i#jHyHRC~qDq&@g^c5|hk|Z}c!1=xME<#GXcC(~*2XDV6PjRMrHniMQ|>{5@XE z&hrF2!T!m2DMsBQw&+0~Ky!Ofb3}~w21ET#O`T$&EhMxwv^6yLI$RyL?r6V(`kE=m z8biHSbAL4K=!n^RdqR&o>KZNeEg@THV^dG0Rj>9WzrG2L%DI!XG*j4F`tm675BVYf z3trFjxSyx6Kk|)il#SBDZD-Qi(|W=7A4jl?cF|!~KQ_%Qh^{$;d5)QyZ~X1sT@pzF)*<+G=HOFdizP13h99jQwpNpq2|PyP3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E}Oc(}~^MRJp>?Z_t>$*(Wn667Tbv5W3)wN#Nh^}>-&Z^b* zHeG9UU81RSe5|>^j3Y=VX*WeF4^OMk@t>nLRZ%2PiygS+D?F~p< zI=r39fYhC+?r!VYYS#uSuf6G3&;O(%r&OQybw-!yIK51*R8BF<#Xr%5___$i^QcTs z+a3NdZ98AEx+)uZl~Fq&Qjw34h!#C0c@sfKT#<7D~Q_UVU>*)nQ+y$6FN)h*kb|@hvL< zs`I>f*)(7^(?#KkrLwk-aDf~};=EDT0=TJ5$cEmwItz&rNqiUXLJ{O z;5sz2&v9JjW*z&ka8$;tJ7G2i`LzdN0s{QWebB@=b-_mtr`2i=$aa90-HCXoJGex7 z?P!2zm3)g!X(-N)rC|fyELO9#OAMZdb@WRbrn|@_GIkWx=!Z|hDK70}G5+-)DCe;) zZ~#4Bye9)i{MbEE%QXLV2vtQ6cB>-fd8nb#8-?vBV5B7~Y_x;wAJUyZ|%s zV|<(Si}4$I|9x;jpKOOYCYM}Re{cspPOe}?cjhXo)18593?2Z~tl1!!(GV>b1-um{ zIw9U@ufnMCor|a0X{NBXB7zcMI|!Tjk4C}fs88IRN^~r2OE)FseW{RK%#UpVU!GU0 z&xrPFc_!cA54Da+Te>fmNyq!zy3_H_fb8TGd*D`Eq%*#)yFZoLTq!&FX$8Jf7|Epi zwq%Ui%H?sK6^VB)5nli->yU+5dG|Y>gVj zT)bu{EVf1eGiZ@Ki@&!QzG}MGYVyvu>7@xFZUGkU4yXbt7Q33 zxs-1og4LxBTF$lLTG&-lkGMvBB!QaX33gS~;=9?a4?`o6T+zTM(=bl1=BTmz8i(Q8 vGCP}QbcmX%5PypY#fNqt{Tj~07_5OjHZATylARubIAqt1!t=oAje_sr=ou<- From 01e1d1ffee444c52da8a3e56d4f93198c6a007a6 Mon Sep 17 00:00:00 2001 From: Rupika Date: Sun, 10 Feb 2019 19:59:28 -0800 Subject: [PATCH 2/3] Fixed deleted report message --- back/backend/views.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/back/backend/views.py b/back/backend/views.py index fac08a4..457f438 100644 --- a/back/backend/views.py +++ b/back/backend/views.py @@ -137,7 +137,7 @@ def report_detail(request, report_pk): # 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 + # gets the fields that only have a data file in them field_set = Field.objects.filter(section_id=i.id).exclude(data_file__exact='') if field_set.exists(): for j in field_set: @@ -145,9 +145,11 @@ def report_detail(request, report_pk): 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)}) + # delete the full report and catch the title + r = Report.objects.get(id=report_pk) + title = r.title + r.delete() + return JsonResponse({"message": "Deleted report: {0}.".format(title)}) # update a section with new data From e7161c7ff91e06c179791acaf05f12074d490a17 Mon Sep 17 00:00:00 2001 From: Rupika Date: Mon, 11 Feb 2019 12:36:24 -0800 Subject: [PATCH 3/3] clean up code --- back/backend/models.py | 20 ++------------------ back/backend/views.py | 1 - 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/back/backend/models.py b/back/backend/models.py index 176c080..557020f 100644 --- a/back/backend/models.py +++ b/back/backend/models.py @@ -2,7 +2,6 @@ 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) @@ -82,21 +81,6 @@ class Field(models.Model): # function that accommodates if # path has slash at end - def path_leaf(self, path, flag=1): + def path_leaf(self, path): 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("") - - + return name or ntpath.basename(dir_path) diff --git a/back/backend/views.py b/back/backend/views.py index 457f438..fa17a1b 100644 --- a/back/backend/views.py +++ b/back/backend/views.py @@ -144,7 +144,6 @@ def report_detail(request, report_pk): # 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 and catch the title r = Report.objects.get(id=report_pk) title = r.title