filters.py 2.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
18
import django_filters
19
20
21

from synnefo.db.models import IPAddress

Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
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
from synnefo_admin.admin.queries_common import (query, model_filter,
                                                get_model_field)


@model_filter
def filter_ip(queryset, queries):
    q = query("ip", 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_vm(queryset, queries):
    q = query("vm", queries)
    ids = get_model_field("vm", q, 'id')
    return queryset.filter(nic__machine__id__in=ids)


@model_filter
def filter_network(queryset, queries):
    q = query("network", queries)
    ids = get_model_field("network", q, 'id')
    return queryset.filter(network__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)
58
59
60
61


class IPFilterSet(django_filters.FilterSet):

Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
62
    """A collection of filters for ips.
63
64
65
66

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

Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
67
68
69
70
71
    ip = django_filters.CharFilter(label='IP', action=filter_ip)
    user = django_filters.CharFilter(label='OF User', action=filter_user)
    vm = django_filters.CharFilter(label='OF VM', action=filter_vm)
    net = django_filters.CharFilter(label='OF Network', action=filter_network)
    proj = django_filters.CharFilter(label='OF Project', action=filter_project)
72
73
74

    class Meta:
        model = IPAddress
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
75
        fields = ('ip', 'floating_ip', 'user', 'vm', 'net', 'proj')