Skip to content

Commit

Permalink
Add filters and bulk actions for K8S namespaces. Closes #24 and #18 and
Browse files Browse the repository at this point in the history
  • Loading branch information
troeger committed Feb 28, 2020
1 parent 47eeaa1 commit 5acb237
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions kubeportal/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,21 @@ def sync_view(self, request):

class KubernetesServiceAccountAdmin(admin.ModelAdmin):
list_display = ['name', 'namespace']
list_display_links = None

def has_delete_permission(self, request, obj=None):
return False

def has_change_permission(self, request, obj=None):
'''
The name and namespace of the service account can only be configured on
creation, but is fixed after the first sync.
'''
if obj and obj.is_synced():
return False
else:
return True
return False

def has_add_permission(self, request, obj=None):
return False

def save_model(self, request, obj, form, change):
super().save_model(request, obj, form, change)
kubernetes.sync(request)

def has_delete_permission(self, request, obj=None):
'''
Disable deletion, even for superusers.
'''
return False

def get_queryset(self, request):
'''
Show service accounts in namespaces being marked as non-visible
Expand All @@ -62,10 +56,23 @@ def get_queryset(self, request):
return qs


def make_visible(modeladmin, request, queryset):
queryset.update(visible=True)
make_visible.short_description = "Mark as visible"


def make_invisible(modeladmin, request, queryset):
queryset.update(visible=False)
make_invisible.short_description = "Mark as non-visible"


class KubernetesNamespaceAdmin(admin.ModelAdmin):
list_display = ['name', 'visible', 'portal_users', 'created', 'number_of_pods']
list_display_links = None
list_filter = ['visible']
ns_list = kubernetes.get_namespaces()
pod_list = kubernetes.get_pods()
actions = [make_visible, make_invisible]

def portal_users(self, instance):
return ','.join(User.objects.filter(service_account__namespace=instance).values_list('username', flat=True))
Expand All @@ -86,13 +93,11 @@ def number_of_pods(self, instance):
number_of_pods.short_description = "Number of pods"

def has_change_permission(self, request, obj=None):
'''
When everything is read-only, the view is no longer a change view
'''
if obj and obj.is_synced() and not request.user.is_superuser:
return False
else:
return True
return False

def has_add_permission(self, request, obj=None):
return False


def get_readonly_fields(self, request, obj=None):
'''
Expand Down Expand Up @@ -133,6 +138,7 @@ def get_queryset(self, request):
qs = qs.filter(visible=True)
return qs


def reject(modeladmin, request, queryset):
for user in queryset:
if user.reject(request):
Expand All @@ -150,8 +156,6 @@ class PortalUserAdmin(UserAdmin):
)
actions = [reject]



def has_add_permission(self, request, obj=None):
return False

Expand Down

0 comments on commit 5acb237

Please sign in to comment.