Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete urls #149

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v1.14.6

- Fix bug where url tilesets could not be deleted

v1.14.5

- Don't require indexfile field in Tileset model
Expand Down
39 changes: 25 additions & 14 deletions tilesets/management/commands/delete_tileset.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
import os

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.db.models import ProtectedError
from django.conf import settings

import tilesets.models as tm
import os


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('--uuid', type=str, required=True)
parser.add_argument("--uuid", type=str, required=True)

def handle(self, *args, **options):
uuid = options.get('uuid')
uuid = options.get("uuid")

# search for Django object, remove associated file and record
instance = tm.Tileset.objects.get(uuid=uuid)
if not instance:
raise CommandError('Instance for specified uuid ({}) was not found'.format(uuid))
raise CommandError(
"Instance for specified uuid ({}) was not found".format(uuid)
)
else:
filename = instance.datafile.name
filepath = os.path.join(settings.MEDIA_ROOT, filename)
if not os.path.isfile(filepath):
raise CommandError('File does not exist under media root')
try:
os.remove(filepath)
except OSError:
raise CommandError('File under media root could not be removed')

if not filepath.endswith(".."):
# ignore files that are mounted using simple-httpfs
if not os.path.isfile(filepath):
raise CommandError("File does not exist under media root")
try:
os.remove(filepath)
except OSError:
raise CommandError("File under media root could not be removed")

try:
instance.delete()
except ProtectedError:
raise CommandError('Instance for specified uuid ({}) could not be deleted'.format(uuid))
raise CommandError(
"Instance for specified uuid ({}) could not be deleted".format(uuid)
)