-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suppression de la suppression logique des médias
- Loading branch information
1 parent
0ef55f9
commit c1f99cf
Showing
5 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
apptax/migrations/versions/44447746cacc_drop_t_medias_supprime_column.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""drop t_medias.supprime column | ||
Revision ID: 44447746cacc | ||
Revises: 0db13d65cb27 | ||
Create Date: 2023-08-04 14:04:28.235799 | ||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.sql import false | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "44447746cacc" | ||
down_revision = "0db13d65cb27" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.execute("DELETE FROM taxonomie.t_medias AS tm WHERE supprime = TRUE;") | ||
op.drop_column(table_name="t_medias", column_name="supprime", schema="taxonomie") | ||
|
||
|
||
def downgrade(): | ||
op.add_column( | ||
table_name="t_medias", | ||
column=sa.Column("supprime", sa.Boolean, nullable=False, server_default=false()), | ||
schema="taxonomie", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import click | ||
|
||
import glob | ||
import os | ||
import shutil | ||
|
||
from pathlib import Path | ||
from flask import current_app | ||
from flask.cli import with_appcontext | ||
|
||
from sqlalchemy import select | ||
|
||
from apptax.database import db | ||
from apptax.taxonomie.models import TMedias | ||
|
||
|
||
import logging | ||
logger = logging.getLogger("taxhub_commands") | ||
|
||
|
||
@click.group(help="Manager Taxhub app.") | ||
def taxhub(): | ||
pass | ||
|
||
|
||
@taxhub.command() | ||
@with_appcontext | ||
@click.option("--drop-file", is_flag=True, help="Drop file and thumb dir") | ||
def check_deleted_media(drop_file): | ||
""" | ||
Suppression des fichiers médias qui ne sont plus référencés en base | ||
""" | ||
media_dir = Path(current_app.config["MEDIA_FOLDER"], "taxhub").absolute() | ||
|
||
# Traitement des fichiers des médias | ||
logger.info(f"Process media files {media_dir}") | ||
for f in glob.glob(f"{media_dir}/*.*"): | ||
file_name = os.path.basename(f) | ||
query = select(TMedias).where(TMedias.chemin != None).where(TMedias.chemin == file_name) | ||
results = db.session.scalars(query).all() | ||
if not results: | ||
logger.warning(f"File {file_name} not related to database media") | ||
if drop_file: | ||
os.remove(f) | ||
logger.warning("\t file dropped") | ||
logger.info("...done") | ||
# Traitement des thumnails des médias | ||
logger.info(f"Process thumbnail files {media_dir}/thumb") | ||
for d in os.listdir(f"{media_dir}/thumb"): | ||
query = select(TMedias).where(TMedias.id_media == int(d)) | ||
results = db.session.scalars(query).all() | ||
if not results: | ||
logger.warning(f"Thumb dir {d} not related to database media") | ||
if drop_file: | ||
shutil.rmtree(f"{media_dir}/thumb/{d}") | ||
logger.warning("\t dir dropped") | ||
logger.info("...done") | ||
|
||
|
||
taxhub.add_command(check_deleted_media) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters