Made bulk delete also delete files
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Fixed bug on env config evaluation Fixed oversight that always displayed browsable api Fixed file naming bug in file deletion
This commit is contained in:
parent
e3a021c53f
commit
6adba29ba1
|
|
@ -26,7 +26,7 @@ def cast_to_native_type(key, value, native_type):
|
||||||
value = value.split(",")
|
value = value.split(",")
|
||||||
|
|
||||||
if native_type == bool:
|
if native_type == bool:
|
||||||
if value == "false":
|
if value.lower() == "false":
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -126,8 +126,13 @@ validate_paths(MEDIA_ROOT)
|
||||||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||||
AUTH_USER_MODEL = "user.LockboxUser"
|
AUTH_USER_MODEL = "user.LockboxUser"
|
||||||
|
|
||||||
|
REST_FRAMEWORK_RENDER_CLASSES = ["rest_framework.renderers.JSONRenderer",]
|
||||||
|
|
||||||
|
if get_config("ENABLE_BROWSABLE_API"):
|
||||||
|
REST_FRAMEWORK_RENDER_CLASSES.append("rest_framework.renderers.BrowsableAPIRenderer")
|
||||||
|
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
|
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
|
||||||
"PAGE_SIZE": 50,
|
"PAGE_SIZE": 25,
|
||||||
|
"DEFAULT_RENDERER_CLASSES": tuple(REST_FRAMEWORK_RENDER_CLASSES),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,6 @@ urlpatterns = [
|
||||||
|
|
||||||
|
|
||||||
if get_config("ENABLE_BROWSABLE_API"):
|
if get_config("ENABLE_BROWSABLE_API"):
|
||||||
urlpatterns.extend(path("api-auth/", include("rest_framework.urls")))
|
urlpatterns.append(path("api-auth/", include("rest_framework.urls")))
|
||||||
|
|
||||||
urlpatterns.extend(static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT))
|
urlpatterns.extend(static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT))
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,11 @@ from django.db import models, transaction
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
BROKEN_STATUSES = [
|
||||||
|
UPLOAD_STATUS_TYPES.ABANDONED,
|
||||||
|
UPLOAD_STATUS_TYPES.ERROR,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class UploadError(Exception):
|
class UploadError(Exception):
|
||||||
|
|
||||||
|
|
@ -24,7 +29,21 @@ def _upload_to_fielpath(instance, filename):
|
||||||
return Path(str(instance.lid)).joinpath(f"{instance.filename}{settings.INCOMPLETE_EXT}")
|
return Path(str(instance.lid)).joinpath(f"{instance.filename}{settings.INCOMPLETE_EXT}")
|
||||||
|
|
||||||
|
|
||||||
|
class FileQuerySet(models.QuerySet):
|
||||||
|
'''Regular bulkd delete method but it invokes obj.delete to actually clear the file.
|
||||||
|
'''
|
||||||
|
def delete(self, *args, **kwargs):
|
||||||
|
keep_file = kwargs.pop("keep_file", False)
|
||||||
|
for obj in self:
|
||||||
|
obj.delete(keep_file=keep_file)
|
||||||
|
|
||||||
|
return super().delete(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class File(LockboxBase):
|
class File(LockboxBase):
|
||||||
|
|
||||||
|
objects = FileQuerySet.as_manager()
|
||||||
|
|
||||||
mime_type = models.CharField(
|
mime_type = models.CharField(
|
||||||
max_length=128,
|
max_length=128,
|
||||||
blank=True,
|
blank=True,
|
||||||
|
|
@ -183,6 +202,27 @@ class File(LockboxBase):
|
||||||
return False
|
return False
|
||||||
return Path(self.file.path).is_file()
|
return Path(self.file.path).is_file()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def cleanup(self, dry_run=True, skip=None):
|
||||||
|
# Probably skip some actual files (or record files) first make dry run report
|
||||||
|
# Then skip=[file_1, file_2]
|
||||||
|
# Should cleanup be automatic? probably not
|
||||||
|
# Find whats broken status
|
||||||
|
# Find what has a record but does not exist
|
||||||
|
# Find what does have a record but doesnt exist.
|
||||||
|
# Cleanup any directories that are not from a record (UUID and no uuid.)
|
||||||
|
# Cleanup any other files?
|
||||||
|
pass
|
||||||
|
# broken = File.objects.filter(status__in=BROKEN_STATUSES)
|
||||||
|
# strays = Path()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def reconcile(self, dry_run=True, delete_strays=True):
|
||||||
|
# finds stuff that has no record and creates one, moves it to the right place.
|
||||||
|
# Probably good to call another OS location to copy files to.
|
||||||
|
# would be cool if it hard linked
|
||||||
|
pass
|
||||||
|
|
||||||
def append_chunk(self, chunk_file, chunk_data):
|
def append_chunk(self, chunk_file, chunk_data):
|
||||||
"""Append chunks to a file
|
"""Append chunks to a file
|
||||||
|
|
||||||
|
|
@ -203,6 +243,7 @@ class File(LockboxBase):
|
||||||
# Oh oh, we are uploading a n + 1 chunk but theres no file
|
# Oh oh, we are uploading a n + 1 chunk but theres no file
|
||||||
if chunk_data["start_bytes"] != 0:
|
if chunk_data["start_bytes"] != 0:
|
||||||
self.status = UPLOAD_STATUS_TYPES.ERROR
|
self.status = UPLOAD_STATUS_TYPES.ERROR
|
||||||
|
self.file.storage.delete()
|
||||||
self.save()
|
self.save()
|
||||||
raise UploadError(
|
raise UploadError(
|
||||||
"File for uploaded chunk no longer exists",
|
"File for uploaded chunk no longer exists",
|
||||||
|
|
@ -247,6 +288,7 @@ class File(LockboxBase):
|
||||||
result = self.verify()
|
result = self.verify()
|
||||||
if not result:
|
if not result:
|
||||||
self.status = UPLOAD_STATUS_TYPES.ERROR
|
self.status = UPLOAD_STATUS_TYPES.ERROR
|
||||||
|
self.file.storage.delete() # tentative
|
||||||
raise UploadError(
|
raise UploadError(
|
||||||
"File verification failed",
|
"File verification failed",
|
||||||
code=UPLOAD_ERROR_CODES.VERIFICATION_FAILED
|
code=UPLOAD_ERROR_CODES.VERIFICATION_FAILED
|
||||||
|
|
@ -256,7 +298,7 @@ class File(LockboxBase):
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
Path(self.file.path).rename(final_path)
|
Path(self.file.path).rename(final_path)
|
||||||
self.file.name = self.filename
|
self.file.name = str(final_path)
|
||||||
self.status = UPLOAD_STATUS_TYPES.COMPLETED
|
self.status = UPLOAD_STATUS_TYPES.COMPLETED
|
||||||
self.datetime_completed = timezone.now()
|
self.datetime_completed = timezone.now()
|
||||||
self.save()
|
self.save()
|
||||||
|
|
@ -285,11 +327,11 @@ class File(LockboxBase):
|
||||||
return super().save(*args, **kwargs)
|
return super().save(*args, **kwargs)
|
||||||
|
|
||||||
def delete(self, *args, **kwargs):
|
def delete(self, *args, **kwargs):
|
||||||
|
keep_file = kwargs.pop("keep_file", False)
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
if self.file:
|
if not keep_file:
|
||||||
if Path(self.file.path).is_file():
|
if self.file and self.exists:
|
||||||
self.file.storage.delete(self.file.path)
|
self.file.storage.delete(self.file.path)
|
||||||
# Delete containing directory (UUID)
|
|
||||||
self.file.storage.delete(Path(self.file.path).parent)
|
self.file.storage.delete(Path(self.file.path).parent)
|
||||||
result = super().delete(*args, **kwargs)
|
result = super().delete(*args, **kwargs)
|
||||||
return result
|
return result
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
printf "\n\n|| Starting lint check ||\n\n"
|
printf "\n\n|| Starting lint check ||\n\n"
|
||||||
flake8
|
flake8
|
||||||
|
printf "\n\n|| Finished lint check ||\n\n"
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
cd lockbox
|
cd lockbox
|
||||||
printf "\n\n|| Starting setup ||\n\n"
|
printf "\n\n|| Starting setup ||\n\n"
|
||||||
python manage.py migrate
|
python manage.py migrate
|
||||||
|
printf "\n\n|| Finished setup ||\n\n"
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
cd lockbox
|
cd lockbox
|
||||||
printf "\n\n|| Starting pytest run ||\n\n"
|
printf "\n\n|| Starting pytest run ||\n\n"
|
||||||
pytest --cov=. --cov-report term-missing --reuse-db
|
pytest --cov=. --cov-report term-missing --reuse-db
|
||||||
|
printf "\n\n|| Finished pytest run ||\n\n"
|
||||||
Loading…
Reference in New Issue