from django.contrib import messages
from django.contrib.auth.decorators import user_passes_test
from django.db.models import Q
from django.shortcuts import render, redirect
from ipynbsrv.core.auth.checks import login_allowed
from ipynbsrv.core.models import Container, ContainerImage


@user_passes_test(login_allowed)
def delete(request):
    if request.method != "POST":
        messages.error(request, "Invalid request method.")
        return redirect('images')
    if 'id' not in request.POST:
        messages.error(request, "Invalid POST request.")
        return redirect('images')

    img_id = int(request.POST.get('id'))

    img = ContainerImage.objects.filter(pk=img_id)
    if img.exists():
        img = img.first()
        if img.owner == request.user:
            img.delete()
            messages.success(request, "Image deleted successfully.")
        else:
            messages.error(request, "You don't have enough permissions for the requested operation.")
    else:
        messages.error(request, "Image does not exist.")

    return redirect('images')


@user_passes_test(login_allowed)
def index(request):
    return render(request, 'web/images/index.html', {
        'title': "Images",
        'containers': Container.objects.filter(owner=request.user.backend_user),
        'images': ContainerImage.objects.filter((Q(owner=request.user) | Q(is_public=True))),
        # meta information for the create modal
        'selected': Container.objects.filter(pk=int(request.GET.get('ct', -1))).first(),
        'share': 'share' in request.GET
    })

@@ -93,39 +93,8 @@
 est%0A
-from django.db.models import Q%0A
 from
@@ -205,52 +205,108 @@
 srv.
-core.models import Container, ContainerImage
+web.api_client_proxy import get_httpclient_instance%0Afrom slumber.exceptions import HttpNotFoundError
 %0A%0A%0A@
@@ -659,155 +659,299 @@
     
-img = ContainerImage.objects.filter(pk=img_id)%0A    if img.exists():%0A        img = img.first()%0A        if img.owner == request.user:%0A            img
+client = get_httpclient_instance(request)%0A%0A    try:%0A        image = client.images(img_id).get()%0A    except HttpNotFoundError:%0A        messages.error(request, %22Image does not exist or you don't have the permissions to delete it.%22)%0A%0A    if image:%0A        try:%0A            client.images(img_id)
 .del
@@ -1030,27 +1030,39 @@
 %22)%0A        e
-lse
+xcept Exception
 :%0A          
@@ -1092,69 +1092,39 @@
 t, %22
-You don't have enough permissions for the requested operation
+Whuups, something went wrong :(
 .%22)%0A
@@ -1186,16 +1186,63 @@
 ot exist
+ or you don't have the permissions to delete it
 .%22)%0A%0A   
@@ -1319,24 +1319,281 @@
 x(request):%0A
+    client = get_httpclient_instance(request)%0A%0A    containers = client.containers.get()%0A    images = client.images.get()%0A    if request.GET.get('ct'):%0A        selected = client.containers(int(request.GET.get('ct'))).get()%0A    else:%0A        selected = None%0A%0A
     return r
@@ -1683,25 +1683,25 @@
 iners': 
-C
+c
 ontainer
 .objects
@@ -1696,150 +1696,35 @@
 iner
-.objects.filter(owner=request.user.backend_user),%0A        'images': ContainerImage.objects.filter((Q(owner=request.user) %7C Q(is_public=True)))
+s,%0A        'images': images
 ,%0A  
@@ -1793,75 +1793,16 @@
 d': 
-Container.objects.filter(pk=int(request.GET.get('ct', -1))).first()
+selected
 ,%0A  

rewrite Image view to use API client