Commit c62287ca by Kálmán Viktor

dashboard: fix vm list filtering

- tags uses __in, so it needs to be handle somehow else
- filtering by 2 or more tags can return one instances more than once so distinct is needed
parent 3bcecbcd
...@@ -128,7 +128,11 @@ class FilterMixin(object): ...@@ -128,7 +128,11 @@ class FilterMixin(object):
filters = {} filters = {}
for item in self.allowed_filters: for item in self.allowed_filters:
if item in self.request.GET: if item in self.request.GET:
filters[self.allowed_filters[item]] = self.request.GET[item] filters[self.allowed_filters[item]] = (
self.request.GET[item].split(",")
if self.allowed_filters[item].endswith("__in") else
self.request.GET[item])
return filters return filters
def get_queryset(self): def get_queryset(self):
...@@ -1152,7 +1156,8 @@ class VmList(LoginRequiredMixin, FilterMixin, ListView): ...@@ -1152,7 +1156,8 @@ class VmList(LoginRequiredMixin, FilterMixin, ListView):
'name': "name__icontains", 'name': "name__icontains",
'node': "node__name__icontains", 'node': "node__name__icontains",
'status': "status__iexact", 'status': "status__iexact",
'tags': "tags__name__in", # note: use it as ?tags[]=a,b 'tags[]': "tags__name__in",
'tags': "tags__name__in", # for search string
'owner': "owner__username", 'owner': "owner__username",
} }
...@@ -1191,8 +1196,10 @@ class VmList(LoginRequiredMixin, FilterMixin, ListView): ...@@ -1191,8 +1196,10 @@ class VmList(LoginRequiredMixin, FilterMixin, ListView):
(sort[1:] if sort[0] == "-" else sort) (sort[1:] if sort[0] == "-" else sort)
in [i.name for i in Instance._meta.fields] + ["pk"]): in [i.name for i in Instance._meta.fields] + ["pk"]):
queryset = queryset.order_by(sort) queryset = queryset.order_by(sort)
return queryset.filter(**self.get_queryset_filters()
).select_related('owner', 'node') return queryset.filter(
**self.get_queryset_filters()).select_related('owner', 'node'
).distinct()
def create_fake_get(self): def create_fake_get(self):
""" """
...@@ -1224,8 +1231,7 @@ class VmList(LoginRequiredMixin, FilterMixin, ListView): ...@@ -1224,8 +1231,7 @@ class VmList(LoginRequiredMixin, FilterMixin, ListView):
# generate a new GET request, that is kinda fake # generate a new GET request, that is kinda fake
fake = self.request.GET.copy() fake = self.request.GET.copy()
for k, v in got.iteritems(): for k, v in got.iteritems():
fake["%s%s" % ( fake[k] = v
k, "[]" if len(v.split(",")) > 1 else "")] = v
self.request.GET = fake self.request.GET = fake
......
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