partitioncloud-server/partitioncloud/modules/classes/partition.py

117 lines
2.9 KiB
Python
Raw Normal View History

2023-10-10 11:14:16 +02:00
import os
from flask import current_app
from ..db import get_db
from .user import User
from .attachment import Attachment
2023-10-10 11:14:16 +02:00
class Partition():
def __init__(self, uuid=None):
db = get_db()
if uuid is not None:
self.uuid = uuid
data = db.execute(
"""
SELECT * FROM partition
WHERE uuid = ?
""",
(self.uuid,)
).fetchone()
if data is None:
raise LookupError
self.name = data["name"]
self.author = data["author"]
self.body = data["body"]
self.user_id = data["user_id"]
self.source = data["source"]
self.attachments = None
2023-10-10 11:14:16 +02:00
else:
raise LookupError
def delete(self):
self.load_attachments()
2023-10-10 11:14:16 +02:00
db = get_db()
db.execute(
"""
DELETE FROM contient_partition
WHERE partition_uuid = ?
""",
(self.uuid,)
)
db.commit()
os.remove(f"partitioncloud/partitions/{self.uuid}.pdf")
if os.path.exists(f"partitioncloud/static/thumbnails/{self.uuid}.jpg"):
os.remove(f"partitioncloud/static/thumbnails/{self.uuid}.jpg")
db.execute(
2023-10-10 11:14:16 +02:00
"""
DELETE FROM partition
WHERE uuid = ?
""",
(self.uuid,)
)
db.commit()
for attachment in self.attachments:
attachment.delete()
2023-10-10 11:14:16 +02:00
def update(self, name=None, author="", body=""):
if name is None:
return Exception("name cannot be None")
db = get_db()
db.execute(
"""
UPDATE partition
SET name = ?,
author = ?,
body = ?
WHERE uuid = ?
""",
(name, author, body, self.uuid)
)
db.commit()
def get_user(self):
db = get_db()
user = db.execute(
"""
SELECT * FROM user
JOIN partition ON user_id = user.id
WHERE partition.uuid = ?
""",
(self.uuid,),
).fetchone()
if user is None:
raise LookupError
return User(user_id=user["id"])
def get_albums(self):
db = get_db()
return db.execute(
"""
SELECT * FROM album
JOIN contient_partition ON album.id = album_id
WHERE partition_uuid = ?
""",
(self.uuid,),
).fetchall()
def load_attachments(self):
db = get_db()
if self.attachments is None:
data = db.execute(
"""
SELECT * FROM attachments
WHERE partition_uuid = ?
""",
(self.uuid,)
)
self.attachments = [Attachment(data=i) for i in data]