mirror of
https://github.com/umap-project/umap.git
synced 2025-04-30 20:12:37 +02:00

When deleting a datalayer, it will now be moved to a state "deleted", and it will only be deleted for real when running the command `umap empty_trash`. This is what we already do for the map itself, but until now if a user deleted a only a datalayer by mistake (not the map itself) it could not retrieve it.
46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
from datetime import datetime, timedelta
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from umap.models import DataLayer, Map
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Remove maps in trash. Eg.: umap empty_trash --days 7"
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
"--days",
|
|
help="Number of days to consider maps for removal",
|
|
default=30,
|
|
type=int,
|
|
)
|
|
parser.add_argument(
|
|
"--dry-run",
|
|
help="Pretend to delete but just report",
|
|
action="store_true",
|
|
)
|
|
|
|
def handle(self, *args, **options):
|
|
days = options["days"]
|
|
since = (datetime.utcnow() - timedelta(days=days)).date()
|
|
print(f"Deleting map in trash since {since}")
|
|
maps = Map.objects.filter(share_status=Map.DELETED, modified_at__lt=since)
|
|
for map in maps:
|
|
map_id = map.id
|
|
map_name = map.name
|
|
trashed_at = map.modified_at.date()
|
|
if not options["dry_run"]:
|
|
map.delete()
|
|
print(f"Deleted map {map_name} ({map_id}), trashed at {trashed_at}")
|
|
print(f"Deleting layers in trash since {since}")
|
|
layers = DataLayer.objects.filter(
|
|
share_status=DataLayer.DELETED, modified_at__lt=since
|
|
)
|
|
for layer in layers:
|
|
layer_id = layer.uuid
|
|
layer_name = layer.name
|
|
trashed_at = layer.modified_at.date()
|
|
if not options["dry_run"]:
|
|
layer.delete()
|
|
print(f"Deleted layer {layer_name} ({layer_id}), trashed at {trashed_at}")
|