filters.py 2.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Copyright (C) 2010-2014 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import logging
import django_filters

Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
19
from synnefo.db.models import VirtualMachine
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from synnefo_admin.admin.queries_common import (query, model_filter,
                                                get_model_field)


@model_filter
def filter_vm(queryset, queries):
    q = query("vm", queries)
    return queryset.filter(q)


@model_filter
def filter_user(queryset, queries):
    q = query("user", queries)
    ids = get_model_field("user", q, 'uuid')
    return queryset.filter(userid__in=ids)


@model_filter
def filter_volume(queryset, queries):
    q = query("volume", queries)
    ids = get_model_field("volume", q, 'machine__id')
    return queryset.filter(id__in=ids)


@model_filter
def filter_network(queryset, queries):
    q = query("network", queries)
    ids = get_model_field("network", q, 'machines__id')
    return queryset.filter(id__in=ids)


@model_filter
def filter_ip(queryset, queries):
    q = query("ip", queries)
    ids = get_model_field("ip", q, 'machines__id')
    return queryset.filter(id__in=ids)


@model_filter
def filter_project(queryset, queries):
    q = query("project", queries)
    ids = get_model_field("project", q, 'uuid')
    return queryset.filter(project__in=ids)


def filter_id(field):
    def _filter_id(qs, query):
        if not query:
            return qs
        return qs.filter(**{"%s__icontains" % field: int(query)})

    return _filter_id
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
72
73


74
75
76
77
78
79
80
class VMFilterSet(django_filters.FilterSet):

    """A collection of filters for VMs.

    This filter collection is based on django-filter's FilterSet.
    """

Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
81
82
83
84
85
    vm = django_filters.CharFilter(label='VM', action=filter_vm)
    user = django_filters.CharFilter(label='OF User', action=filter_user)
    vol = django_filters.CharFilter(label='HAS Volume', action=filter_volume)
    net = django_filters.CharFilter(label='IN Network', action=filter_network)
    proj = django_filters.CharFilter(label='OF Project', action=filter_project)
86
87
88
89
90
    operstate = django_filters.MultipleChoiceFilter(
        label='Status', name='operstate', choices=VirtualMachine.OPER_STATES)

    class Meta:
        model = VirtualMachine
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
91
        fields = ('vm', 'operstate', 'suspended', 'user', 'vol', 'net', 'proj')