Commit 749d0686 by Czémán Arnold

dashboard: move VmDescFilterMixin into FilterMixin and rewrite query for 'shared' stype

parent 4c5d09d7
Pipeline #176 passed with stage
in 0 seconds
......@@ -29,7 +29,7 @@ from django.core.exceptions import PermissionDenied, SuspiciousOperation
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.contrib.auth.views import redirect_to_login
from django.db.models import Q
from django.db.models import Q, Count, Sum
from django.http import (
HttpResponse, Http404, HttpResponseRedirect, JsonResponse
)
......@@ -159,31 +159,28 @@ class FilterMixin(object):
def create_acl_queryset(self, model):
cleaned_data = self.search_form.cleaned_data
stype = cleaned_data.get('stype', "all")
superuser = stype == "all"
shared = stype == "shared" or stype == "all"
level = "owner" if stype == "owned" else "user"
queryset = model.get_objects_with_level(
level, self.request.user,
group_also=shared, disregard_superuser=not superuser,
)
return queryset
class VmDescFilterMixin(FilterMixin):
def create_acl_queryset(self, model):
cleaned_data = self.search_form.cleaned_data
stype = cleaned_data.get('stype', 'all')
queryset = super(VmDescFilterMixin, self).create_acl_queryset(model)
superuser = stype == 'all'
shared = stype == 'shared' or stype == 'all'
level = 'owner' if stype == 'owned' else 'user'
user = self.request.user
queryset = model.get_objects_with_level(
level, user, group_also=shared, disregard_superuser=not superuser)
if stype == 'owned':
queryset = queryset.filter(owner=user)
elif stype == 'shared':
queryset = model.get_objects_with_level(
'owner', self.request.user)
pk_list = [record.pk for record in queryset
if record.object_level_set.count() > 1]
queryset = queryset.filter(owner=user)
pk_list = []
for record in queryset:
sum = record.object_level_set.annotate(
Count('users'), Count('groups')).aggregate(
Sum('users__count'), Sum('groups__count'))
if (sum['users__count__sum'] > 1 or
sum['groups__count__sum'] > 0):
pk_list.append(record.pk)
queryset = queryset.filter(pk__in=pk_list)
elif stype == 'shared_with_me':
queryset = queryset.exclude(owner=user)
......
......@@ -56,7 +56,7 @@ from vm.models import (
)
from .util import (
CheckedDetailView, AjaxOperationMixin, OperationView, AclUpdateView,
FormOperationMixin, VmDescFilterMixin, GraphMixin,
FormOperationMixin, FilterMixin, GraphMixin,
TransferOwnershipConfirmView, TransferOwnershipView,
)
from ..forms import (
......@@ -921,7 +921,7 @@ vm_mass_ops = OrderedDict([
])
class VmList(LoginRequiredMixin, VmDescFilterMixin, ListView):
class VmList(LoginRequiredMixin, FilterMixin, ListView):
template_name = "dashboard/vm-list.html"
allowed_filters = {
'name': "name__icontains",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment