Ethan Dalool
4c65ccaf68
I found that the strict heirarchy was not satisfying the situation where one tag is the intersection of two others, but we can only pick one as the parent For example, does red_jacket belong under clothes.red_clothes or clothes.jackets? A search for "red_clothes AND jackets" might give us someone wearing red pants and a black jacket, so this definitely needs to be a separate tag, but picking only one parent for it is not sufficient. Now, a search for red_clothes and a search for jackets will both find our red_jacket photo. The change also applies to Albums because why not, and I'm sure a similar case can be made. Unfortunately this means tags no longer have one true qualname. The concept of qualnames has not been completely phased out but it's in progress. This commit is very big because I was not sure for a long time whether to go through with it, and so much stuff had to change that I don't want to go back and figure out what could be grouped together.
91 lines
2.3 KiB
Python
91 lines
2.3 KiB
Python
'''
|
|
This file provides functions that convert the Etiquette objects into
|
|
dictionaries suitable for JSON serializing.
|
|
'''
|
|
|
|
def album(a, minimal=False):
|
|
j = {
|
|
'id': a.id,
|
|
'description': a.description,
|
|
'title': a.title,
|
|
'author': user_or_none(a.get_author()),
|
|
}
|
|
if not minimal:
|
|
j['photos'] = [photo(p) for p in a.get_photos()]
|
|
j['parents'] = [album(p, minimal=True) for p in a.get_parents()]
|
|
j['sub_albums'] = [album(c, minimal=True) for c in a.get_children()]
|
|
|
|
return j
|
|
|
|
def bookmark(b):
|
|
j = {
|
|
'id': b.id,
|
|
'author': user_or_none(b.get_author()),
|
|
'url': b.url,
|
|
'title': b.title,
|
|
}
|
|
return j
|
|
|
|
def exception(e):
|
|
j = {
|
|
'error_type': e.error_type,
|
|
'error_message': e.error_message,
|
|
}
|
|
return j
|
|
|
|
def photo(p, include_albums=True, include_tags=True):
|
|
tags = p.get_tags()
|
|
tags.sort(key=lambda x: x.name)
|
|
j = {
|
|
'id': p.id,
|
|
'author': user_or_none(p.get_author()),
|
|
'extension': p.extension,
|
|
'width': p.width,
|
|
'height': p.height,
|
|
'ratio': p.ratio,
|
|
'area': p.area,
|
|
'bytes': p.bytes,
|
|
'duration_str': p.duration_string,
|
|
'duration': p.duration,
|
|
'bytes_str': p.bytestring,
|
|
'has_thumbnail': bool(p.thumbnail),
|
|
'created': p.created,
|
|
'filename': p.basename,
|
|
'mimetype': p.mimetype,
|
|
'searchhidden': bool(p.searchhidden),
|
|
}
|
|
if include_albums:
|
|
j['albums'] = [album(a, minimal=True) for a in p.get_containing_albums()]
|
|
|
|
if include_tags:
|
|
j['tags'] = [tag(t, minimal=True) for t in tags]
|
|
|
|
return j
|
|
|
|
def tag(t, include_synonyms=False, minimal=False):
|
|
j = {
|
|
'id': t.id,
|
|
'name': t.name,
|
|
}
|
|
if not minimal:
|
|
j['author'] = user_or_none(t.get_author())
|
|
j['description'] = t.description
|
|
j['children'] = [tag(c, minimal=True) for c in t.get_children()]
|
|
|
|
if include_synonyms:
|
|
j['synonyms'] = list(t.get_synonyms())
|
|
return j
|
|
|
|
def user(u):
|
|
j = {
|
|
'id': u.id,
|
|
'username': u.username,
|
|
'created': u.created,
|
|
'display_name': u.display_name,
|
|
}
|
|
return j
|
|
|
|
def user_or_none(u):
|
|
if u is None:
|
|
return None
|
|
return user(u)
|