def avatars tag_ids page for_tags False if for_tags query Avatar objec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def avatars(tag_ids, page, for_tags=False):
if for_tags:
query = Avatar.objects.query(Avatar.id)
else:
query = Avatar.objects.query
if tag_ids:
# Get avatar id's that match criteria
related_table = Avatar.tags.property.secondary
related_avatar_id = related_table.get_related_id(Avatar)
related_tag_id = related_table.get_related_id(Tag)
selects = [select([related_avatar_id]).where(related_tag_id==t)
for t in tag_ids]
query = query.filter(Avatar.id.in_(intersect(*selects)))
query = query.filter(Avatar.pending==False)
count = 0
if not for_tags:
count = query.count()
if page:
query = query.offset(page * PAGE_SIZE)
query = query.limit(PAGE_SIZE)
return count, query