Revision 56b9989a snf-astakos-app/astakos/im/management/commands/_common.py
b/snf-astakos-app/astakos/im/management/commands/_common.py | ||
---|---|---|
36 | 36 |
from django.utils.timesince import timesince, timeuntil |
37 | 37 |
from django.contrib.auth.models import Permission |
38 | 38 |
from django.contrib.contenttypes.models import ContentType |
39 |
from django.core.exceptions import FieldError |
|
40 |
from django.core.management import CommandError |
|
39 | 41 |
|
40 | 42 |
from synnefo.lib.ordereddict import OrderedDict |
41 | 43 |
from astakos.im.models import AstakosUser |
... | ... | |
163 | 165 |
return '..' + s[-display:] |
164 | 166 |
else: |
165 | 167 |
return s[:display] + '..' |
168 |
|
|
169 |
|
|
170 |
# Copied from snf-cyclades-app/synnefo/management/common.py |
|
171 |
# It could be moved to snf-common |
|
172 |
def filter_results(objects, filter_by): |
|
173 |
filter_list = filter_by.split(",") |
|
174 |
filter_dict = {} |
|
175 |
exclude_dict = {} |
|
176 |
|
|
177 |
def map_field_type(query): |
|
178 |
def fix_bool(val): |
|
179 |
if val.lower() in ("yes", "true", "t"): |
|
180 |
return True |
|
181 |
if val.lower() in ("no", "false", "f"): |
|
182 |
return False |
|
183 |
return val |
|
184 |
|
|
185 |
if "!=" in query: |
|
186 |
key, val = query.split("!=") |
|
187 |
exclude_dict[key] = fix_bool(val) |
|
188 |
return |
|
189 |
OP_MAP = { |
|
190 |
">=": "__gte", |
|
191 |
"=>": "__gte", |
|
192 |
">": "__gt", |
|
193 |
"<=": "__lte", |
|
194 |
"=<": "__lte", |
|
195 |
"<": "__lt", |
|
196 |
"=": "", |
|
197 |
} |
|
198 |
for op, new_op in OP_MAP.items(): |
|
199 |
if op in query: |
|
200 |
key, val = query.split(op) |
|
201 |
filter_dict[key + new_op] = fix_bool(val) |
|
202 |
return |
|
203 |
|
|
204 |
map(lambda x: map_field_type(x), filter_list) |
|
205 |
|
|
206 |
try: |
|
207 |
objects = objects.filter(**filter_dict) |
|
208 |
return objects.exclude(**exclude_dict) |
|
209 |
except FieldError as e: |
|
210 |
raise CommandError(e) |
|
211 |
except Exception as e: |
|
212 |
raise CommandError("Can not filter results: %s" % e) |
Also available in: Unified diff