Skip to content

Commit

Permalink
command for deleting files
Browse files Browse the repository at this point in the history
  • Loading branch information
thusser committed Apr 28, 2022
1 parent 5763360 commit ce96360
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pyobs_archive/api/management/commands/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os

from django.core.management.base import BaseCommand

from pyobs_archive.api.models import Frame
from pyobs_archive import settings


class Command(BaseCommand):
help = 'Ingest images'

def add_arguments(self, parser):
parser.add_argument('files', type=str, nargs='+', help='Names of files to delete')

def handle(self, *args, files: list = None, **options):
to_delete = []
for filename in files:
frames = Frame.objects.filter(basename=filename)
if len(frames) > 0:
to_delete.append(frames)

if not to_delete:
return

print("Images to delete:")
for d in to_delete:
print(" - " + d.basename)
reply = None
while reply not in 'yYnN':
reply = input("Delete files? [yn]")

if reply not in 'yY':
return

for d in to_delete:
# get filename
root = settings.ARCHIVE_ROOT
filename = os.path.join(root, frame.path, frame.basename + '.fits.fz')

# delete file
os.remove(filename)

# delete db entry
d.delete()

0 comments on commit ce96360

Please sign in to comment.