Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement sorting on entities and relation indexes.
- Loading branch information
Showing
3 changed files
with
36 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from flask import request | ||
from sqlalchemy import desc, asc | ||
|
||
|
||
def parse_sorts(): | ||
for key in request.args.getlist('sort'): | ||
direction = asc | ||
if key.startswith('-'): | ||
direction = desc | ||
key = key[1:] | ||
yield key, direction | ||
|
||
|
||
def for_relations(q, Rel): | ||
for key, direction in parse_sorts(): | ||
for cand in ['id', 'created_at', 'updated_at']: | ||
if key == cand: | ||
q = q.order_by(direction(getattr(Rel, key))) | ||
if key == 'source': | ||
q = q.order_by(direction(Rel.source_id, key)) | ||
if key == 'target': | ||
q = q.order_by(direction(Rel.target_id, key)) | ||
return q | ||
|
||
|
||
def for_entities(q, Ent): | ||
for key, direction in parse_sorts(): | ||
for cand in ['id', 'created_at', 'updated_at', 'degree', 'degree_in', 'degree_out']: | ||
if key == cand: | ||
q = q.order_by(direction(getattr(Ent, key))) | ||
return q | ||
|