server/tags: create tag categories automatically

This commit is contained in:
rr- 2016-04-29 10:33:46 +02:00
parent 0f90196ad5
commit f59c1e0346
2 changed files with 18 additions and 9 deletions

View file

@ -10,7 +10,6 @@ class TagNotFoundError(errors.NotFoundError): pass
class TagAlreadyExistsError(errors.ValidationError): pass class TagAlreadyExistsError(errors.ValidationError): pass
class TagIsInUseError(errors.ValidationError): pass class TagIsInUseError(errors.ValidationError): pass
class InvalidTagNameError(errors.ValidationError): pass class InvalidTagNameError(errors.ValidationError): pass
class InvalidTagCategoryError(errors.ValidationError): pass
class InvalidTagRelationError(errors.ValidationError): pass class InvalidTagRelationError(errors.ValidationError): pass
def _verify_name_validity(name): def _verify_name_validity(name):
@ -159,10 +158,8 @@ def update_category_name(tag, category_name):
.filter(db.TagCategory.name == category_name) \ .filter(db.TagCategory.name == category_name) \
.first() .first()
if not category: if not category:
category_names = tag_categories.get_all_category_names() category = tag_categories.create_category(category_name, 'default')
raise InvalidTagCategoryError( db.session.add(category)
'Category %r is invalid. Valid categories: %r.' % (
category_name, category_names))
tag.category = category tag.category = category
def update_names(tag, names): def update_names(tag, names):

View file

@ -2,7 +2,7 @@ import datetime
import os import os
import pytest import pytest
from szurubooru import api, config, db, errors 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): def assert_relations(relations, expected_tag_names):
actual_names = [rel.names[0].name for rel in relations] actual_names = [rel.names[0].name for rel in relations]
@ -14,6 +14,7 @@ def test_ctx(
config_injector({ config_injector({
'data_dir': str(tmpdir), 'data_dir': str(tmpdir),
'tag_name_regex': '^[^!]*$', 'tag_name_regex': '^[^!]*$',
'tag_category_name_regex': '^[^!]*$',
'ranks': ['anonymous', 'regular_user'], 'ranks': ['anonymous', 'regular_user'],
'privileges': {'tags:create': 'regular_user'}, 'privileges': {'tags:create': 'regular_user'},
}) })
@ -63,9 +64,9 @@ def test_creating_simple_tags(test_ctx, fake_datetime):
({'names': ['']}, tags.InvalidTagNameError), ({'names': ['']}, tags.InvalidTagNameError),
({'names': ['!bad']}, tags.InvalidTagNameError), ({'names': ['!bad']}, tags.InvalidTagNameError),
({'names': ['x' * 65]}, tags.InvalidTagNameError), ({'names': ['x' * 65]}, tags.InvalidTagNameError),
({'category': None}, tags.InvalidTagCategoryError), ({'category': None}, tag_categories.InvalidTagCategoryNameError),
({'category': ''}, tags.InvalidTagCategoryError), ({'category': ''}, tag_categories.InvalidTagCategoryNameError),
({'category': 'invalid'}, tags.InvalidTagCategoryError), ({'category': '!bad'}, tag_categories.InvalidTagCategoryNameError),
({'suggestions': ['good', '!bad']}, tags.InvalidTagNameError), ({'suggestions': ['good', '!bad']}, tags.InvalidTagNameError),
({'implications': ['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'))) user=test_ctx.user_factory(rank='regular_user')))
assert tags.try_get_tag_by_name('unused') is None 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', [ @pytest.mark.parametrize('input,expected_suggestions,expected_implications', [
# new relations # new relations
({ ({