def search(current, query=None):
if not query:
raise ValueError("query must be provided.")
query = query.strip()
condition = current.db.clients.id > 0
orderby = None
audit.log(current, "ClientSearch", query=query)
# Search for a client ID directly.
if query.startswith("C."):
condition = current.db.clients.client_id == query
elif query.startswith("label:"):
label = query.split(":", 1)[1]
condition = (current.db.clients.labels == label) | (
current.db.clients.custom_labels == label)
orderby = current.db.clients.id
else:
# AppEngine uses Bigtable which does not support `like` operation. We
# only support a prefix match.
condition = ((current.db.clients.hostname >= query) &
(current.db.clients.hostname < query + u"\ufffd"))
result = []
for row in current.db(condition).select(
orderby_on_limitby=False, limitby=(0, 1000),
orderby=orderby):
labels = set([x for x in row.labels if x])
result.append(dict(
last=row.last,
last_humanized=humanize.naturaltime(
datetime.datetime.now()-row.last),
client_id=row.client_id,
summary=json.loads(row.summary),
labels=sorted(labels),
# These are the only labels which may be modified.
custom_labels=row.custom_labels))
return dict(data=result)
评论列表
文章目录