Skip to content
Snippets Groups Projects
Commit d2715bd3 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

Merge branch 'delete_cached_files' into '1.4.x'

Delete cached files

See merge request !239
parents e8e5f44b 8ec96619
No related branches found
No related tags found
2 merge requests!2551.4.x,!239Delete cached files
Pipeline #
......@@ -520,6 +520,39 @@ admin.site.register(ResultModel, Result)
#----------------------------------------------------------
def delete_file_on_fs(modeladmin, request, queryset):
'''
Delete the files contained in the cache
'''
for obj in queryset:
obj.delete_files()
delete_file_on_fs.short_description = 'Delete files from the cache'
def cascading_delete_file_on_fs(modeladmin, request, queryset):
'''
Delete the files contained in the cache
'''
for obj in queryset:
for block in obj.blocks.all():
experiment = block.experiment
for exp_block in experiment.blocks.all():
for result in exp_block.results.all():
if result.cache is not None:
result.cache.delete_files()
for input_ in exp_block.inputs.all():
if input_.cache is not None:
input_.cache.delete_files()
cascading_delete_file_on_fs.short_description = 'Delete files from the ' \
'selected and related caches'
class CachedFile(admin.ModelAdmin):
search_fields = [
......@@ -543,10 +576,18 @@ class CachedFile(admin.ModelAdmin):
# to avoid very slow loading of cached files
raw_id_fields = ('blocks',)
actions = [delete_file_on_fs, cascading_delete_file_on_fs]
def get_queryset(self, request):
qs = super(CachedFile, self).get_queryset(request)
return qs.annotate(date=Max('blocks__start_date'))
def get_actions(self, request):
actions = super(CachedFile, self).get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions
def date(self, obj):
return obj.date
......
......@@ -177,3 +177,14 @@ class CachedFile(models.Model):
return False
return True
def delete_files(self, cache=settings.CACHE_ROOT):
'''
Delete the files contained in this cache
'''
files = self.files()
for file in files:
os.remove(file)
self.status = CachedFile.NOT_CACHED
self.save()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment