Skip to content

Commit

Permalink
Add removal plan to containers for #737.
Browse files Browse the repository at this point in the history
  • Loading branch information
donkirkby committed Aug 3, 2018
1 parent ee5c3aa commit 7b6041b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
4 changes: 3 additions & 1 deletion kive/container/ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from wsgiref.util import FileWrapper

from django.db.models import Q
from django.db.models.aggregates import Count
from django.http import HttpResponse
from rest_framework import permissions
from rest_framework.decorators import action
Expand Down Expand Up @@ -39,7 +40,8 @@ class ContainerFamilyViewSet(CleanCreateModelMixin,
* filters[n][key]=user&filters[n][val]=match - username of creator contains
the value (case insensitive)
"""
queryset = ContainerFamily.objects.all()
queryset = ContainerFamily.objects.annotate(
num_containers=Count('containers'))
serializer_class = ContainerFamilySerializer
permission_classes = (permissions.IsAuthenticated, IsDeveloperOrGrantedReadOnly)
pagination_class = StandardPagination
Expand Down
49 changes: 47 additions & 2 deletions kive/container/models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os
import re

import errno
from django.core.validators import RegexValidator
from django.db import models
from django.db import models, transaction
from django.dispatch import receiver
from django.urls import reverse

from constants import maxlengths
from metadata.models import AccessControl
from metadata.models import AccessControl, empty_removal_plan, remove_helper


class ContainerFamily(AccessControl):
Expand All @@ -20,6 +23,7 @@ class ContainerFamily(AccessControl):
help_text='URL of Git repository that containers were built from',
max_length=2000,
blank=True)
containers = None # Filled in later from child table.

class Meta(object):
ordering = ['name']
Expand All @@ -30,6 +34,23 @@ def __str__(self):
def get_absolute_url(self):
return reverse('container_family_update', kwargs=dict(pk=self.pk))

@transaction.atomic
def build_removal_plan(self, removal_accumulator=None):
""" Make a manifest of objects to remove when removing this. """
removal_plan = removal_accumulator or empty_removal_plan()
assert self not in removal_plan["ContainerFamilies"]
removal_plan["ContainerFamilies"].add(self)

for container in self.containers.all():
container.build_removal_plan(removal_plan)

return removal_plan

@transaction.atomic
def remove(self):
removal_plan = self.build_removal_plan()
remove_helper(removal_plan)


class Container(AccessControl):
UPLOAD_DIR = "CodeResources"
Expand Down Expand Up @@ -61,3 +82,27 @@ class Meta:

def get_absolute_url(self):
return reverse('container_update', kwargs=dict(pk=self.pk))

@transaction.atomic
def build_removal_plan(self, removal_accumulator=None):
""" Make a manifest of objects to remove when removing this. """
removal_plan = removal_accumulator or empty_removal_plan()
assert self not in removal_plan["Containers"]
removal_plan["Containers"].add(self)

return removal_plan

@transaction.atomic
def remove(self):
removal_plan = self.build_removal_plan()
remove_helper(removal_plan)


@receiver(models.signals.post_delete, sender=Container)
def delete_container_file(instance, **_kwargs):
if instance.file:
try:
os.remove(instance.file.path)
except OSError as ex:
if ex.errno != errno.ENOENT:
raise
19 changes: 10 additions & 9 deletions kive/container/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
class ContainerFamilySerializer(AccessControlSerializer,
serializers.ModelSerializer):
absolute_url = URLField(source='get_absolute_url', read_only=True)
# TODO: removal plan here and in fields
# removal_plan = serializers.HyperlinkedIdentityField(
# view_name='dockerimage-removal-plan')
num_containers = serializers.IntegerField()
removal_plan = serializers.HyperlinkedIdentityField(
view_name='containerfamily-removal-plan')

class Meta:
model = ContainerFamily
Expand All @@ -23,15 +23,13 @@ class Meta:
"git",
"user",
"users_allowed",
"groups_allowed")
"groups_allowed",
"num_containers",
"removal_plan")


class ContainerSerializer(AccessControlSerializer,
serializers.ModelSerializer):
# TODO: removal plan here and in fields
# removal_plan = serializers.HyperlinkedIdentityField(
# view_name='dockerimage-removal-plan')

absolute_url = URLField(source='get_absolute_url', read_only=True)
family = serializers.SlugRelatedField(
slug_field='name',
Expand All @@ -43,6 +41,8 @@ class ContainerSerializer(AccessControlSerializer,
read_only=True)
download_url = serializers.HyperlinkedIdentityField(
view_name='container-download')
removal_plan = serializers.HyperlinkedIdentityField(
view_name='container-removal-plan')

class Meta:
model = Container
Expand All @@ -59,4 +59,5 @@ class Meta:
'created',
'user',
'users_allowed',
'groups_allowed')
'groups_allowed',
'removal_plan')
1 change: 1 addition & 0 deletions kive/container/static/container/ContainerFamilyTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
this.registerLinkColumn("Name", "", "name", "absolute_url");
this.registerColumn("Git", "git");
this.registerColumn("Description", "description");
this.registerColumn("Containers", "num_containers");
this.registerStandardColumn("user");
this.registerStandardColumn("users_allowed");
this.registerStandardColumn("groups_allowed");
Expand Down
3 changes: 2 additions & 1 deletion kive/metadata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
deletion_order = [
"ExecRecords", "Datasets", "Runs", "Pipelines", "PipelineFamilies", "Methods",
"MethodFamilies", "CompoundDatatypes", "Datatypes",
"CodeResourceRevisions", "CodeResources", "DockerImages"
"CodeResourceRevisions", "CodeResources", "DockerImages", "Containers",
"ContainerFamilies"
]


Expand Down
2 changes: 1 addition & 1 deletion kive/metadata/templates/metadata/permissions_widget.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- Django template -->
<!-- Display contents of Datatypes as a HTML table -->
<!-- Display permissions as an HTML table -->
{% if old_users or old_groups %}
<div>
<h4>Users allowed</h4>
Expand Down

0 comments on commit 7b6041b

Please sign in to comment.