server/tags: sort relations like post tags

This commit is contained in:
rr- 2016-07-30 12:31:04 +02:00
parent 7e6e59417e
commit 298aedbc75
4 changed files with 22 additions and 17 deletions

View File

@ -63,8 +63,6 @@ def serialize_note(note):
} }
def serialize_post(post, authenticated_user, options=None): def serialize_post(post, authenticated_user, options=None):
default_category = tag_categories.try_get_default_category()
default_category_name = default_category.name if default_category else None
return util.serialize_entity( return util.serialize_entity(
post, post,
{ {
@ -83,13 +81,7 @@ def serialize_post(post, authenticated_user, options=None):
'thumbnailUrl': lambda: get_post_thumbnail_url(post), 'thumbnailUrl': lambda: get_post_thumbnail_url(post),
'flags': lambda: post.flags, 'flags': lambda: post.flags,
'tags': lambda: [ 'tags': lambda: [
tag.names[0].name for tag in sorted( tag.names[0].name for tag in tags.sort_tags(post.tags)],
post.tags,
key=lambda tag: (
default_category_name == tag.category.name,
tag.category.name,
tag.names[0].name)
)],
'relations': lambda: sorted( 'relations': lambda: sorted(
{ {
post['id']: post['id']:

View File

@ -36,6 +36,17 @@ def _get_default_category_name():
else: else:
return DEFAULT_CATEGORY_NAME return DEFAULT_CATEGORY_NAME
def sort_tags(tags):
default_category = tag_categories.try_get_default_category()
default_category_name = default_category.name if default_category else None
return sorted(
tags,
key=lambda tag: (
default_category_name == tag.category.name,
tag.category.name,
tag.names[0].name)
)
def serialize_tag(tag, options=None): def serialize_tag(tag, options=None):
return util.serialize_entity( return util.serialize_entity(
tag, tag,
@ -47,9 +58,11 @@ def serialize_tag(tag, options=None):
'lastEditTime': lambda: tag.last_edit_time, 'lastEditTime': lambda: tag.last_edit_time,
'usages': lambda: tag.post_count, 'usages': lambda: tag.post_count,
'suggestions': lambda: [ 'suggestions': lambda: [
relation.names[0].name for relation in tag.suggestions], relation.names[0].name
for relation in sort_tags(tag.suggestions)],
'implications': lambda: [ 'implications': lambda: [
relation.names[0].name for relation in tag.implications], relation.names[0].name
for relation in sort_tags(tag.implications)],
'snapshots': lambda: snapshots.get_serialized_history(tag), 'snapshots': lambda: snapshots.get_serialized_history(tag),
}, },
options) options)

View File

@ -5,8 +5,8 @@ from szurubooru import api, config, db, errors
from szurubooru.func import util, tags, tag_categories from szurubooru.func import util, tags, tag_categories
def assert_relations(relations, expected_tag_names): def assert_relations(relations, expected_tag_names):
actual_names = [rel.names[0].name for rel in relations] actual_names = sorted([rel.names[0].name for rel in relations])
assert actual_names == expected_tag_names assert actual_names == sorted(expected_tag_names)
@pytest.fixture @pytest.fixture
def test_ctx( def test_ctx(
@ -186,7 +186,7 @@ def test_creating_new_category(test_ctx):
'category': 'meta', 'category': 'meta',
'suggestions': ['sug', 'shared'], 'suggestions': ['sug', 'shared'],
'implications': ['shared', 'imp'], 'implications': ['shared', 'imp'],
}, ['sug', 'shared'], ['shared', 'imp']), }, ['shared', 'sug'], ['imp', 'shared']),
# duplicate relations # duplicate relations
({ ({
'names': ['main'], 'names': ['main'],

View File

@ -5,8 +5,8 @@ from szurubooru import api, config, db, errors
from szurubooru.func import util, tags, tag_categories from szurubooru.func import util, tags, tag_categories
def assert_relations(relations, expected_tag_names): def assert_relations(relations, expected_tag_names):
actual_names = [rel.names[0].name for rel in relations] actual_names = sorted([rel.names[0].name for rel in relations])
assert actual_names == expected_tag_names assert actual_names == sorted(expected_tag_names)
@pytest.fixture @pytest.fixture
def test_ctx( def test_ctx(
@ -176,7 +176,7 @@ def test_trying_to_use_existing_name(test_ctx, dup_name):
({ ({
'suggestions': ['sug', 'shared'], 'suggestions': ['sug', 'shared'],
'implications': ['shared', 'imp'], 'implications': ['shared', 'imp'],
}, ['sug', 'shared'], ['shared', 'imp']), }, ['shared', 'sug'], ['imp', 'shared']),
# duplicate relations # duplicate relations
({ ({
'suggestions': ['sug', 'SUG'], 'suggestions': ['sug', 'SUG'],