partitioncloud-server/partitioncloud/modules/utils.py

96 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/python3
import io
import random
import string
import qrcode
from flask import current_app, send_file
2023-12-15 13:38:32 +01:00
from .db import get_db
def new_uuid():
return ''.join([random.choice(string.ascii_uppercase + string.digits) for _ in range(6)])
def format_uuid(uuid):
"""Format old uuid4 format"""
return uuid.upper()[:6]
def get_qrcode(location):
complete_url = current_app.config["BASE_URL"] + location
img_io = io.BytesIO()
qrcode.make(complete_url).save(img_io)
img_io.seek(0)
return send_file(img_io, mimetype="image/jpeg")
2022-12-19 15:19:58 +01:00
2023-10-10 11:14:16 +02:00
from .classes.user import User
from .classes.album import Album
2023-10-11 17:15:49 +02:00
from .classes.groupe import Groupe
2023-10-10 11:14:16 +02:00
from .classes.partition import Partition
from .classes.attachment import Attachment
2024-01-15 18:53:57 +01:00
from .classes.album import create as create_album
2022-12-19 15:19:58 +01:00
2022-08-16 18:13:00 +02:00
def get_all_partitions():
db = get_db()
partitions = db.execute(
"""
SELECT p.uuid, p.name, p.author, p.body, p.user_id,
CASE WHEN MAX(a.uuid) IS NOT NULL THEN 1 ELSE 0 END AS has_attachment
FROM partition AS p
JOIN contient_partition ON contient_partition.partition_uuid = p.uuid
JOIN album ON album.id = album_id
LEFT JOIN attachments AS a ON p.uuid = a.partition_uuid
GROUP BY p.uuid, p.name, p.author, p.user_id
2022-08-16 18:13:00 +02:00
"""
)
2022-12-19 15:19:58 +01:00
# Transform sql object to dictionary usable in any thread
return [
{
"uuid": p["uuid"],
"name": p["name"],
"author": p["author"],
"body": p["body"],
"user_id": p["user_id"],
"has_attachment": p["has_attachment"]
2022-12-19 15:19:58 +01:00
} for p in partitions
]
def get_all_albums():
db = get_db()
albums = db.execute(
"""
SELECT * FROM album
"""
)
# Transform sql object to dictionary usable in any thread
return [
{
"id": a["id"],
"name": a["name"],
"uuid": a["uuid"]
} for a in albums
2023-12-15 11:36:34 +01:00
]
2024-01-29 18:37:30 +01:00
def user_count():
db = get_db()
count = db.execute(
"""
SELECT COUNT(*) as count FROM user
"""
).fetchone()
return count[0]
def partition_count():
db = get_db()
count = db.execute(
"""
SELECT COUNT(*) FROM partition
"""
).fetchone()
2024-02-25 15:54:48 +01:00
2024-01-29 18:37:30 +01:00
return count[0]