I am trying to upload my media files to AWS Bucket and I am completely able to upload them there on bucket using this code.
model.py
class Evidence(models.Model):
"""
Stores an individual evidence file, related to :model:`reporting.ReportFindingLink`
and :model:`users.User`.
"""
def set_upload_destination(self, filename):
"""
Sets the `upload_to` destination to the evidence folder for the associated report ID.
"""
return os.path.join("evidence", str(self.finding.report.id), filename)
document = models.FileField(
upload_to=set_upload_destination,
validators=[validate_evidence_extension],
blank=True,
)
finding = models.ForeignKey("ReportFindingLink", on_delete=models.CASCADE)
views.py
def form_valid(self, form, **kwargs):
self.object = form.save(commit=False)
self.object.uploaded_by = self.request.user
self.object.finding = self.finding_instance
try:
self.object.save()
messages.success(self.request,"Evidence uploaded successfully", extra_tags="alert-success")
except:
messages.error(self.request, "Evidence file failed to upload", extra_tags="alert-danger")
return HttpResponseRedirect(self.get_success_url())
def get_success_url(self):
if "modal" in self.kwargs:
return reverse("reporting:upload_evidence_modal_success")
return reverse("reporting:report_detail", args=(self.object.finding.report.pk,))
All of this is working fine. But trouble occurs when I am trying to delete the file. Huh?
Here is my code for deleting the file.
I must tell you guys that there is relationship established between Evidence and ReportingLink table.
class ReportFindingLinkDelete(LoginRequiredMixin, SingleObjectMixin, View):
"""
Delete an individual :model:`reporting.ReportFindingLink`.
"""
model = ReportFindingLink
def post(self, *args, **kwargs):
finding = self.get_object()
finding.delete()
data = {
"result": "success",
"message": "Successfully deleted {finding} and cleaned up evidence".format(finding=finding),
}
logger.info(
"Deleted %s %s by request of %s",
finding.__class__.__name__,
finding.id,
self.request.user,
)
return JsonResponse(data)
Above is the code for deletion of ReportLink which will eventually delete the Evidence.
What could the problem here?