Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / _filtering.py @ 35cbac33

History | View | Annotate | Download (2.6 kB)

1 5a0f9d6c Giorgos Korfiatis
# Copyright 2013 GRNET S.A. All rights reserved.
2 5a0f9d6c Giorgos Korfiatis
#
3 5a0f9d6c Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 5a0f9d6c Giorgos Korfiatis
# without modification, are permitted provided that the following
5 5a0f9d6c Giorgos Korfiatis
# conditions are met:
6 5a0f9d6c Giorgos Korfiatis
#
7 5a0f9d6c Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 5a0f9d6c Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 5a0f9d6c Giorgos Korfiatis
#      disclaimer.
10 5a0f9d6c Giorgos Korfiatis
#
11 5a0f9d6c Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 5a0f9d6c Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 5a0f9d6c Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 5a0f9d6c Giorgos Korfiatis
#      provided with the distribution.
15 5a0f9d6c Giorgos Korfiatis
#
16 5a0f9d6c Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 5a0f9d6c Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 5a0f9d6c Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 5a0f9d6c Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 5a0f9d6c Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 5a0f9d6c Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 5a0f9d6c Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 5a0f9d6c Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 5a0f9d6c Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 5a0f9d6c Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 5a0f9d6c Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 5a0f9d6c Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 5a0f9d6c Giorgos Korfiatis
#
29 5a0f9d6c Giorgos Korfiatis
# The views and conclusions contained in the software and
30 5a0f9d6c Giorgos Korfiatis
# documentation are those of the authors and should not be
31 5a0f9d6c Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 5a0f9d6c Giorgos Korfiatis
# or implied, of GRNET S.A.
33 5a0f9d6c Giorgos Korfiatis
34 5a0f9d6c Giorgos Korfiatis
from synnefo.util import units
35 35cbac33 Giorgos Korfiatis
from snf_django.management.commands import CommandError
36 5a0f9d6c Giorgos Korfiatis
from django.db.models import Q
37 5a0f9d6c Giorgos Korfiatis
38 5a0f9d6c Giorgos Korfiatis
39 5a0f9d6c Giorgos Korfiatis
OP_MAP = [
40 5a0f9d6c Giorgos Korfiatis
    ("!=", lambda x: ~x, ""),
41 5a0f9d6c Giorgos Korfiatis
    (">=", lambda x: x, "__gte"),
42 5a0f9d6c Giorgos Korfiatis
    ("=>", lambda x: x, "__gte"),
43 5a0f9d6c Giorgos Korfiatis
    (">", lambda x: x, "__gt"),
44 5a0f9d6c Giorgos Korfiatis
    ("<=", lambda x: x, "__lte"),
45 5a0f9d6c Giorgos Korfiatis
    ("=<", lambda x: x, "__lte"),
46 5a0f9d6c Giorgos Korfiatis
    ("<", lambda x: x, "__lt"),
47 5a0f9d6c Giorgos Korfiatis
    ("=", lambda x: x, ""),
48 5a0f9d6c Giorgos Korfiatis
    ]
49 5a0f9d6c Giorgos Korfiatis
50 5a0f9d6c Giorgos Korfiatis
51 5a0f9d6c Giorgos Korfiatis
def parse_filter(exp):
52 5a0f9d6c Giorgos Korfiatis
    for s, prepend, op in OP_MAP:
53 5a0f9d6c Giorgos Korfiatis
        key, sep, value = exp.partition(s)
54 5a0f9d6c Giorgos Korfiatis
        if s == sep:
55 5a0f9d6c Giorgos Korfiatis
            return key, prepend, op, value
56 5a0f9d6c Giorgos Korfiatis
    raise CommandError("Could not parse filter.")
57 5a0f9d6c Giorgos Korfiatis
58 5a0f9d6c Giorgos Korfiatis
59 5a0f9d6c Giorgos Korfiatis
def make_query(flt, handlers):
60 5a0f9d6c Giorgos Korfiatis
    key, prepend, opstr, value = parse_filter(flt)
61 5a0f9d6c Giorgos Korfiatis
    try:
62 5a0f9d6c Giorgos Korfiatis
        (dbkey, parse) = handlers[key]
63 5a0f9d6c Giorgos Korfiatis
        return prepend(Q(**{dbkey+opstr: parse(value)}))
64 5a0f9d6c Giorgos Korfiatis
    except KeyError:
65 5a0f9d6c Giorgos Korfiatis
        return None
66 5a0f9d6c Giorgos Korfiatis
67 5a0f9d6c Giorgos Korfiatis
68 5a0f9d6c Giorgos Korfiatis
def parse_with_unit(value):
69 5a0f9d6c Giorgos Korfiatis
    try:
70 5a0f9d6c Giorgos Korfiatis
        return units.parse(value)
71 5a0f9d6c Giorgos Korfiatis
    except units.ParseError:
72 5a0f9d6c Giorgos Korfiatis
        raise CommandError("Failed to parse value, should be an integer, "
73 5a0f9d6c Giorgos Korfiatis
                           "possibly followed by a unit, or 'inf'.")