Delete attachments when deleting a partition

This commit is contained in:
augustin64 2023-10-26 14:24:30 +02:00
parent f8670f4901
commit 3b08f2e08c
2 changed files with 21 additions and 1 deletions

View File

@ -1,3 +1,5 @@
import os
from flask import current_app from flask import current_app
from ..db import get_db from ..db import get_db
@ -29,5 +31,18 @@ class Attachment():
self.filetype = data["filetype"] self.filetype = data["filetype"]
self.partition_uuid = data["partition_uuid"] self.partition_uuid = data["partition_uuid"]
def delete(self):
db = get_db()
db.execute(
"""
DELETE FROM attachments
WHERE uuid = ?
""",
(self.uuid,)
)
db.commit()
os.remove(f"partitioncloud/attachments/{self.uuid}.{self.filetype}")
def __repr__(self): def __repr__(self):
return f"{self.name}.{self.filetype}" return f"{self.name}.{self.filetype}"

View File

@ -31,6 +31,8 @@ class Partition():
raise LookupError raise LookupError
def delete(self): def delete(self):
self.load_attachments()
db = get_db() db = get_db()
db.execute( db.execute(
""" """
@ -45,7 +47,7 @@ class Partition():
if os.path.exists(f"partitioncloud/static/thumbnails/{self.uuid}.jpg"): if os.path.exists(f"partitioncloud/static/thumbnails/{self.uuid}.jpg"):
os.remove(f"partitioncloud/static/thumbnails/{self.uuid}.jpg") os.remove(f"partitioncloud/static/thumbnails/{self.uuid}.jpg")
partitions = db.execute( db.execute(
""" """
DELETE FROM partition DELETE FROM partition
WHERE uuid = ? WHERE uuid = ?
@ -54,6 +56,9 @@ class Partition():
) )
db.commit() db.commit()
for attachment in self.attachments:
attachment.delete()
def update(self, name=None, author="", body=""): def update(self, name=None, author="", body=""):
if name is None: if name is None:
return Exception("name cannot be None") return Exception("name cannot be None")