From f59c1e034631dc25a53db6e9c0bb4f4a2772b8e6 Mon Sep 17 00:00:00 2001 From: rr- Date: Fri, 29 Apr 2016 10:33:46 +0200 Subject: [PATCH] server/tags: create tag categories automatically --- server/szurubooru/func/tags.py | 7 ++----- .../szurubooru/tests/api/test_tag_creating.py | 20 +++++++++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/server/szurubooru/func/tags.py b/server/szurubooru/func/tags.py index 14619a0..6dc01c2 100644 --- a/server/szurubooru/func/tags.py +++ b/server/szurubooru/func/tags.py @@ -10,7 +10,6 @@ class TagNotFoundError(errors.NotFoundError): pass class TagAlreadyExistsError(errors.ValidationError): pass class TagIsInUseError(errors.ValidationError): pass class InvalidTagNameError(errors.ValidationError): pass -class InvalidTagCategoryError(errors.ValidationError): pass class InvalidTagRelationError(errors.ValidationError): pass def _verify_name_validity(name): @@ -159,10 +158,8 @@ def update_category_name(tag, category_name): .filter(db.TagCategory.name == category_name) \ .first() if not category: - category_names = tag_categories.get_all_category_names() - raise InvalidTagCategoryError( - 'Category %r is invalid. Valid categories: %r.' % ( - category_name, category_names)) + category = tag_categories.create_category(category_name, 'default') + db.session.add(category) tag.category = category def update_names(tag, names): diff --git a/server/szurubooru/tests/api/test_tag_creating.py b/server/szurubooru/tests/api/test_tag_creating.py index 5f4b6fe..41b8463 100644 --- a/server/szurubooru/tests/api/test_tag_creating.py +++ b/server/szurubooru/tests/api/test_tag_creating.py @@ -2,7 +2,7 @@ import datetime import os import pytest from szurubooru import api, config, db, errors -from szurubooru.func import util, tags +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] @@ -14,6 +14,7 @@ def test_ctx( config_injector({ 'data_dir': str(tmpdir), 'tag_name_regex': '^[^!]*$', + 'tag_category_name_regex': '^[^!]*$', 'ranks': ['anonymous', 'regular_user'], 'privileges': {'tags:create': 'regular_user'}, }) @@ -63,9 +64,9 @@ def test_creating_simple_tags(test_ctx, fake_datetime): ({'names': ['']}, tags.InvalidTagNameError), ({'names': ['!bad']}, tags.InvalidTagNameError), ({'names': ['x' * 65]}, tags.InvalidTagNameError), - ({'category': None}, tags.InvalidTagCategoryError), - ({'category': ''}, tags.InvalidTagCategoryError), - ({'category': 'invalid'}, tags.InvalidTagCategoryError), + ({'category': None}, tag_categories.InvalidTagCategoryNameError), + ({'category': ''}, tag_categories.InvalidTagCategoryNameError), + ({'category': '!bad'}, tag_categories.InvalidTagCategoryNameError), ({'suggestions': ['good', '!bad']}, tags.InvalidTagNameError), ({'implications': ['good', '!bad']}, tags.InvalidTagNameError), ]) @@ -157,6 +158,17 @@ def test_trying_to_use_existing_name(test_ctx): user=test_ctx.user_factory(rank='regular_user'))) assert tags.try_get_tag_by_name('unused') is None +def test_creating_new_category(test_ctx): + test_ctx.api.post( + test_ctx.context_factory( + input={ + 'names': ['main'], + 'category': 'new', + 'suggestions': [], + 'implications': [], + }, user=test_ctx.user_factory(rank='regular_user'))) + assert tag_categories.try_get_category_by_name('new') is not None + @pytest.mark.parametrize('input,expected_suggestions,expected_implications', [ # new relations ({