server/tags: create snapshots for automatic tags
This commit is contained in:
parent
6a48020426
commit
fa14bf714c
|
@ -1,8 +1,17 @@
|
||||||
import datetime
|
import datetime
|
||||||
from szurubooru import search
|
from szurubooru import db, search
|
||||||
from szurubooru.api.base_api import BaseApi
|
from szurubooru.api.base_api import BaseApi
|
||||||
from szurubooru.func import auth, tags, snapshots
|
from szurubooru.func import auth, tags, snapshots
|
||||||
|
|
||||||
|
def _create_if_needed(tag_names, user):
|
||||||
|
if not tag_names:
|
||||||
|
return
|
||||||
|
auth.verify_privilege(user, 'tags:create')
|
||||||
|
_existing_tags, new_tags = tags.get_or_create_tags_by_names(tag_names)
|
||||||
|
db.session.flush()
|
||||||
|
for tag in new_tags:
|
||||||
|
snapshots.save_entity_creation(tag, user)
|
||||||
|
|
||||||
class TagListApi(BaseApi):
|
class TagListApi(BaseApi):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
@ -23,6 +32,9 @@ class TagListApi(BaseApi):
|
||||||
implications = ctx.get_param_as_list(
|
implications = ctx.get_param_as_list(
|
||||||
'implications', required=False, default=[])
|
'implications', required=False, default=[])
|
||||||
|
|
||||||
|
_create_if_needed(suggestions, ctx.user)
|
||||||
|
_create_if_needed(implications, ctx.user)
|
||||||
|
|
||||||
tag = tags.create_tag(names, category, suggestions, implications)
|
tag = tags.create_tag(names, category, suggestions, implications)
|
||||||
ctx.session.add(tag)
|
ctx.session.add(tag)
|
||||||
ctx.session.flush()
|
ctx.session.flush()
|
||||||
|
@ -48,12 +60,14 @@ class TagDetailApi(BaseApi):
|
||||||
tag, ctx.get_param_as_string('category'))
|
tag, ctx.get_param_as_string('category'))
|
||||||
if ctx.has_param('suggestions'):
|
if ctx.has_param('suggestions'):
|
||||||
auth.verify_privilege(ctx.user, 'tags:edit:suggestions')
|
auth.verify_privilege(ctx.user, 'tags:edit:suggestions')
|
||||||
tags.update_tag_suggestions(
|
suggestions = ctx.get_param_as_list('suggestions')
|
||||||
tag, ctx.get_param_as_list('suggestions'))
|
_create_if_needed(suggestions, ctx.user)
|
||||||
|
tags.update_tag_suggestions(tag, suggestions)
|
||||||
if ctx.has_param('implications'):
|
if ctx.has_param('implications'):
|
||||||
auth.verify_privilege(ctx.user, 'tags:edit:implications')
|
auth.verify_privilege(ctx.user, 'tags:edit:implications')
|
||||||
tags.update_tag_implications(
|
implications = ctx.get_param_as_list('implications')
|
||||||
tag, ctx.get_param_as_list('implications'))
|
_create_if_needed(implications, ctx.user)
|
||||||
|
tags.update_tag_implications(tag, implications)
|
||||||
tag.last_edit_time = datetime.datetime.now()
|
tag.last_edit_time = datetime.datetime.now()
|
||||||
ctx.session.flush()
|
ctx.session.flush()
|
||||||
snapshots.save_entity_modification(tag, ctx.user)
|
snapshots.save_entity_modification(tag, ctx.user)
|
||||||
|
|
|
@ -214,13 +214,9 @@ def update_tag_names(tag, names):
|
||||||
def update_tag_implications(tag, relations):
|
def update_tag_implications(tag, relations):
|
||||||
if _check_name_intersection(_get_plain_names(tag), relations):
|
if _check_name_intersection(_get_plain_names(tag), relations):
|
||||||
raise InvalidTagRelationError('Tag cannot imply itself.')
|
raise InvalidTagRelationError('Tag cannot imply itself.')
|
||||||
related_tags, new_tags = get_or_create_tags_by_names(relations)
|
tag.implications = get_tags_by_names(relations)
|
||||||
db.session.flush()
|
|
||||||
tag.implications = related_tags + new_tags
|
|
||||||
|
|
||||||
def update_tag_suggestions(tag, relations):
|
def update_tag_suggestions(tag, relations):
|
||||||
if _check_name_intersection(_get_plain_names(tag), relations):
|
if _check_name_intersection(_get_plain_names(tag), relations):
|
||||||
raise InvalidTagRelationError('Tag cannot suggest itself.')
|
raise InvalidTagRelationError('Tag cannot suggest itself.')
|
||||||
related_tags, new_tags = get_or_create_tags_by_names(relations)
|
tag.suggestions = get_tags_by_names(relations)
|
||||||
db.session.flush()
|
|
||||||
tag.suggestions = related_tags + new_tags
|
|
||||||
|
|
|
@ -249,11 +249,12 @@ def test_reusing_suggestions_and_implications(test_ctx):
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
def test_tag_trying_to_relate_to_itself(test_ctx, input):
|
def test_tag_trying_to_relate_to_itself(test_ctx, input):
|
||||||
with pytest.raises(tags.InvalidTagRelationError):
|
with pytest.raises(tags.TagAlreadyExistsError):
|
||||||
test_ctx.api.post(
|
test_ctx.api.post(
|
||||||
test_ctx.context_factory(
|
test_ctx.context_factory(
|
||||||
input=input,
|
input=input,
|
||||||
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
|
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
|
||||||
|
db.session.rollback()
|
||||||
assert tags.try_get_tag_by_name('tag') is None
|
assert tags.try_get_tag_by_name('tag') is None
|
||||||
|
|
||||||
def test_trying_to_create_tag_without_privileges(test_ctx):
|
def test_trying_to_create_tag_without_privileges(test_ctx):
|
||||||
|
|
|
@ -16,6 +16,7 @@ def test_ctx(
|
||||||
'tag_name_regex': '^[^!]*$',
|
'tag_name_regex': '^[^!]*$',
|
||||||
'tag_category_name_regex': '^[^!]*$',
|
'tag_category_name_regex': '^[^!]*$',
|
||||||
'privileges': {
|
'privileges': {
|
||||||
|
'tags:create': db.User.RANK_REGULAR,
|
||||||
'tags:edit:names': db.User.RANK_REGULAR,
|
'tags:edit:names': db.User.RANK_REGULAR,
|
||||||
'tags:edit:category': db.User.RANK_REGULAR,
|
'tags:edit:category': db.User.RANK_REGULAR,
|
||||||
'tags:edit:suggestions': db.User.RANK_REGULAR,
|
'tags:edit:suggestions': db.User.RANK_REGULAR,
|
||||||
|
|
Loading…
Reference in New Issue