Fix: Clean up map_iframe_single.html, remove debug overlay, ensure clean map rendering.
This commit is contained in:
38
app/utils/clean_orphan_media.py
Normal file
38
app/utils/clean_orphan_media.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import os
|
||||
from app import db
|
||||
from app.models import Post, GPXFile
|
||||
|
||||
def clean_orphan_post_media(media_root='app/static/media/posts'):
|
||||
"""
|
||||
Remove folders in app/static/media/posts that do not have a corresponding Post in the database.
|
||||
"""
|
||||
# Get all valid media_folder names from the database
|
||||
valid_folders = set(post.media_folder for post in Post.query.all() if post.media_folder)
|
||||
|
||||
# List all folders in the media root
|
||||
for folder in os.listdir(media_root):
|
||||
folder_path = os.path.join(media_root, folder)
|
||||
if os.path.isdir(folder_path) and folder not in valid_folders:
|
||||
print(f"Deleting orphaned media folder: {folder_path}")
|
||||
# Recursively delete the folder and its contents
|
||||
for root, dirs, files in os.walk(folder_path, topdown=False):
|
||||
for name in files:
|
||||
os.remove(os.path.join(root, name))
|
||||
for name in dirs:
|
||||
os.rmdir(os.path.join(root, name))
|
||||
os.rmdir(folder_path)
|
||||
|
||||
# --- Remove orphaned GPXFile records ---
|
||||
# Get all valid post IDs
|
||||
valid_post_ids = set(post.id for post in Post.query.all())
|
||||
# Find GPXFile records whose post_id is not in valid_post_ids
|
||||
orphaned_gpx_files = GPXFile.query.filter(~GPXFile.post_id.in_(valid_post_ids)).all()
|
||||
if orphaned_gpx_files:
|
||||
print(f"Deleting {len(orphaned_gpx_files)} orphaned GPXFile records from the database.")
|
||||
for gpx in orphaned_gpx_files:
|
||||
db.session.delete(gpx)
|
||||
db.session.commit()
|
||||
else:
|
||||
print("No orphaned GPXFile records found.")
|
||||
|
||||
print("Orphaned media and GPXFile cleanup complete.")
|
||||
Reference in New Issue
Block a user