diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py index ef21383..b39ee11 100644 --- a/server/szurubooru/func/posts.py +++ b/server/szurubooru/func/posts.py @@ -63,8 +63,6 @@ def serialize_note(note): } 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( post, { @@ -83,13 +81,7 @@ def serialize_post(post, authenticated_user, options=None): 'thumbnailUrl': lambda: get_post_thumbnail_url(post), 'flags': lambda: post.flags, 'tags': lambda: [ - tag.names[0].name for tag in sorted( - post.tags, - key=lambda tag: ( - default_category_name == tag.category.name, - tag.category.name, - tag.names[0].name) - )], + tag.names[0].name for tag in tags.sort_tags(post.tags)], 'relations': lambda: sorted( { post['id']: diff --git a/server/szurubooru/func/tags.py b/server/szurubooru/func/tags.py index 0d988ed..b29eb60 100644 --- a/server/szurubooru/func/tags.py +++ b/server/szurubooru/func/tags.py @@ -36,6 +36,17 @@ def _get_default_category_name(): else: 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): return util.serialize_entity( tag, @@ -47,9 +58,11 @@ def serialize_tag(tag, options=None): 'lastEditTime': lambda: tag.last_edit_time, 'usages': lambda: tag.post_count, 'suggestions': lambda: [ - relation.names[0].name for relation in tag.suggestions], + relation.names[0].name + for relation in sort_tags(tag.suggestions)], '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), }, options) diff --git a/server/szurubooru/tests/api/test_tag_creating.py b/server/szurubooru/tests/api/test_tag_creating.py index 666b940..24d7682 100644 --- a/server/szurubooru/tests/api/test_tag_creating.py +++ b/server/szurubooru/tests/api/test_tag_creating.py @@ -5,8 +5,8 @@ from szurubooru import api, config, db, errors from szurubooru.func import util, tags, tag_categories def assert_relations(relations, expected_tag_names): - actual_names = [rel.names[0].name for rel in relations] - assert actual_names == expected_tag_names + actual_names = sorted([rel.names[0].name for rel in relations]) + assert actual_names == sorted(expected_tag_names) @pytest.fixture def test_ctx( @@ -186,7 +186,7 @@ def test_creating_new_category(test_ctx): 'category': 'meta', 'suggestions': ['sug', 'shared'], 'implications': ['shared', 'imp'], - }, ['sug', 'shared'], ['shared', 'imp']), + }, ['shared', 'sug'], ['imp', 'shared']), # duplicate relations ({ 'names': ['main'], diff --git a/server/szurubooru/tests/api/test_tag_updating.py b/server/szurubooru/tests/api/test_tag_updating.py index 778dcc5..a0b275d 100644 --- a/server/szurubooru/tests/api/test_tag_updating.py +++ b/server/szurubooru/tests/api/test_tag_updating.py @@ -5,8 +5,8 @@ from szurubooru import api, config, db, errors from szurubooru.func import util, tags, tag_categories def assert_relations(relations, expected_tag_names): - actual_names = [rel.names[0].name for rel in relations] - assert actual_names == expected_tag_names + actual_names = sorted([rel.names[0].name for rel in relations]) + assert actual_names == sorted(expected_tag_names) @pytest.fixture def test_ctx( @@ -176,7 +176,7 @@ def test_trying_to_use_existing_name(test_ctx, dup_name): ({ 'suggestions': ['sug', 'shared'], 'implications': ['shared', 'imp'], - }, ['sug', 'shared'], ['shared', 'imp']), + }, ['shared', 'sug'], ['imp', 'shared']), # duplicate relations ({ 'suggestions': ['sug', 'SUG'],