Skip to content

Commit

Permalink
Suppression de la suppression logique des médias
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Aug 9, 2024
1 parent 0ef55f9 commit e67b7fb
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
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",
)
61 changes: 61 additions & 0 deletions apptax/taxonomie/commands/taxhub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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)
1 change: 0 additions & 1 deletion apptax/taxonomie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ class TMedias(db.Model):
source = db.Column(db.Unicode)
licence = db.Column(db.Unicode)
is_public = db.Column(db.BOOLEAN)
supprime = db.Column(db.BOOLEAN)
id_type = db.Column(
db.Integer,
ForeignKey(BibTypesMedia.id_type),
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- Remplacement du fichier de configuration `config.py` par une fichier toml : `taxhub_config.toml` (#517)
- Création d'une commande de récupération des médias de l'inpn et suppression des anciens scripts. Pour spécifier les taxons à traiter la commande prend comme paramètre un fichier contenant une liste de cd_nom
`flask taxref import-inpn-media list_cd_ref.csv`
- Suppression de la colonne supprime des médias qui effectuait une suppression logique et non physique des médias.
- Ajout commande de vérification de la cohérence des médias `flask taxhub check-deleted-media`

**⚠️ Notes de version**
- Les données de la table "bib_nom" on été sauvegardées dans une liste nommée "Save bib_nom". Le champs "nom_français" ainsi que "commentaire" de cette table ne sont pas conservés dans la version 2.0.0 (ils n'étaient plus utilisés dans les recherche de taxons depuis plusieurs versions).
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
],
"flask.commands": [
"taxref = apptax.taxonomie.commands.taxref:taxref",
"taxhub = apptax.taxonomie.commands.taxhub:taxhub",
],
},
extras_require={
Expand Down

0 comments on commit e67b7fb

Please sign in to comment.