Beispiel #1
0
def test_get_tag_by_name(name_to_search, expected_to_find, tag_factory):
    tag = tag_factory(names=['name', 'ALIAS'])
    db.session.add(tag)
    db.session.flush()
    if expected_to_find:
        assert tags.get_tag_by_name(name_to_search) == tag
    else:
        with pytest.raises(tags.TagNotFoundError):
            tags.get_tag_by_name(name_to_search)
Beispiel #2
0
def test_get_tag_by_name(name_to_search, expected_to_find, tag_factory):
    tag = tag_factory(names=['name', 'ALIAS'])
    db.session.add(tag)
    db.session.flush()
    if expected_to_find:
        assert tags.get_tag_by_name(name_to_search) == tag
    else:
        with pytest.raises(tags.TagNotFoundError):
            tags.get_tag_by_name(name_to_search)
Beispiel #3
0
 def post(self, ctx):
     source_tag_name = ctx.get_param_as_string('remove', required=True) or ''
     target_tag_name = ctx.get_param_as_string('mergeTo', required=True) or ''
     source_tag = tags.get_tag_by_name(source_tag_name)
     target_tag = tags.get_tag_by_name(target_tag_name)
     if source_tag.tag_id == target_tag.tag_id:
         raise tags.InvalidTagRelationError('Cannot merge tag with itself.')
     auth.verify_privilege(ctx.user, 'tags:merge')
     snapshots.save_entity_deletion(source_tag, ctx.user)
     tags.merge_tags(source_tag, target_tag)
     ctx.session.commit()
     tags.export_to_json()
     return tags.serialize_tag_with_details(target_tag)
Beispiel #4
0
def merge_tags(ctx: rest.Context,
               _params: Dict[str, str] = {}) -> rest.Response:
    source_tag_name = ctx.get_param_as_string("remove")
    target_tag_name = ctx.get_param_as_string("mergeTo")
    source_tag = tags.get_tag_by_name(source_tag_name)
    target_tag = tags.get_tag_by_name(target_tag_name)
    versions.verify_version(source_tag, ctx, "removeVersion")
    versions.verify_version(target_tag, ctx, "mergeToVersion")
    versions.bump_version(target_tag)
    auth.verify_privilege(ctx.user, "tags:merge")
    tags.merge_tags(source_tag, target_tag)
    snapshots.merge(source_tag, target_tag, ctx.user)
    ctx.session.commit()
    return _serialize(ctx, target_tag)
Beispiel #5
0
def merge_tags(
        ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response:
    source_tag_name = ctx.get_param_as_string('remove')
    target_tag_name = ctx.get_param_as_string('mergeTo')
    source_tag = tags.get_tag_by_name(source_tag_name)
    target_tag = tags.get_tag_by_name(target_tag_name)
    versions.verify_version(source_tag, ctx, 'removeVersion')
    versions.verify_version(target_tag, ctx, 'mergeToVersion')
    versions.bump_version(target_tag)
    auth.verify_privilege(ctx.user, 'tags:merge')
    tags.merge_tags(source_tag, target_tag)
    snapshots.merge(source_tag, target_tag, ctx.user)
    ctx.session.commit()
    return _serialize(ctx, target_tag)
Beispiel #6
0
def merge_tags(ctx, _params=None):
    source_tag_name = ctx.get_param_as_string('remove', required=True) or ''
    target_tag_name = ctx.get_param_as_string('mergeTo', required=True) or ''
    source_tag = tags.get_tag_by_name(source_tag_name)
    target_tag = tags.get_tag_by_name(target_tag_name)
    versions.verify_version(source_tag, ctx, 'removeVersion')
    versions.verify_version(target_tag, ctx, 'mergeToVersion')
    versions.bump_version(target_tag)
    auth.verify_privilege(ctx.user, 'tags:merge')
    tags.merge_tags(source_tag, target_tag)
    snapshots.merge(source_tag, target_tag, ctx.user)
    ctx.session.commit()
    tags.export_to_json()
    return _serialize(ctx, target_tag)
def test_reusing_own_name(test_ctx, dup_name):
    db.session.add(
        test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta'))
    db.session.commit()
    result = test_ctx.api.put(
        test_ctx.context_factory(
            input={'names': [dup_name, 'tag3']},
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'tag1')
    assert result['tag']['names'] == ['tag1', 'tag3']
    assert tags.try_get_tag_by_name('tag2') is None
    tag1 = tags.get_tag_by_name('tag1')
    tag2 = tags.get_tag_by_name('tag3')
    assert tag1.tag_id == tag2.tag_id
    assert [name.name for name in tag1.names] == ['tag1', 'tag3']
def test_reusing_own_name(test_ctx, dup_name):
    db.session.add(
        test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta'))
    db.session.commit()
    result = test_ctx.api.put(
        test_ctx.context_factory(
            input={'names': [dup_name, 'tag3']},
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
        'tag1')
    assert result['tag']['names'] == ['tag1', 'tag3']
    assert tags.try_get_tag_by_name('tag2') is None
    tag1 = tags.get_tag_by_name('tag1')
    tag2 = tags.get_tag_by_name('tag3')
    assert tag1.tag_id == tag2.tag_id
    assert [name.name for name in tag1.names] == ['tag1', 'tag3']
Beispiel #9
0
 def post(self, ctx):
     source_tag_name = ctx.get_param_as_string('remove',
                                               required=True) or ''
     target_tag_name = ctx.get_param_as_string('mergeTo',
                                               required=True) or ''
     source_tag = tags.get_tag_by_name(source_tag_name)
     target_tag = tags.get_tag_by_name(target_tag_name)
     if source_tag.tag_id == target_tag.tag_id:
         raise tags.InvalidTagRelationError('Cannot merge tag with itself.')
     auth.verify_privilege(ctx.user, 'tags:merge')
     snapshots.save_entity_deletion(source_tag, ctx.user)
     tags.merge_tags(source_tag, target_tag)
     ctx.session.commit()
     tags.export_to_json()
     return tags.serialize_tag_with_details(target_tag)
Beispiel #10
0
def test_merging_without_usages(test_ctx, fake_datetime):
    source_tag = test_ctx.tag_factory(names=['source'], category_name='meta')
    target_tag = test_ctx.tag_factory(names=['target'], category_name='meta')
    db.session.add_all([source_tag, target_tag])
    db.session.commit()
    with fake_datetime('1997-12-01'):
        result = test_ctx.api.post(
            test_ctx.context_factory(
                input={
                    'remove': 'source',
                    'mergeTo': 'target',
                },
                user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    assert result['tag'] == {
        'names': ['target'],
        'category': 'meta',
        'suggestions': [],
        'implications': [],
        'creationTime': datetime.datetime(1996, 1, 1),
        'lastEditTime': None,
        'usages': 0,
    }
    assert 'snapshots' in result
    assert tags.try_get_tag_by_name('source') is None
    tag = tags.get_tag_by_name('target')
    assert tag is not None
    assert os.path.exists(os.path.join(config.config['data_dir'], 'tags.json'))
def test_creating_simple_tags(test_ctx, fake_datetime):
    with fake_datetime('1997-12-01'):
        result = test_ctx.api.post(
            test_ctx.context_factory(
                input={
                    'names': ['tag1', 'tag2'],
                    'category': 'meta',
                    'suggestions': [],
                    'implications': [],
                },
                user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    assert result['tag'] == {
        'names': ['tag1', 'tag2'],
        'category': 'meta',
        'suggestions': [],
        'implications': [],
        'creationTime': datetime.datetime(1997, 12, 1),
        'lastEditTime': None,
        'usages': 0,
    }
    assert len(result['snapshots']) == 1
    tag = tags.get_tag_by_name('tag1')
    assert [tag_name.name for tag_name in tag.names] == ['tag1', 'tag2']
    assert tag.category.name == 'meta'
    assert tag.last_edit_time is None
    assert tag.post_count == 0
    assert_relations(tag.suggestions, [])
    assert_relations(tag.implications, [])
    assert os.path.exists(os.path.join(config.config['data_dir'], 'tags.json'))
Beispiel #12
0
def test_merge_tags_doesnt_create_relation_loop_for_children(tag_factory):
    source_tag = tag_factory(names=["source"])
    target_tag = tag_factory(names=["target"])
    source_tag.suggestions = [target_tag]
    source_tag.implications = [target_tag]
    db.session.add_all([source_tag, target_tag])
    db.session.commit()
    assert source_tag.suggestion_count == 1
    assert source_tag.implication_count == 1
    assert target_tag.suggestion_count == 0
    assert target_tag.implication_count == 0
    tags.merge_tags(source_tag, target_tag)
    db.session.commit()
    assert tags.try_get_tag_by_name("source") is None
    assert tags.get_tag_by_name("target").suggestion_count == 0
    assert tags.get_tag_by_name("target").implication_count == 0
def test_creating_simple_tags(test_ctx, fake_datetime):
    with fake_datetime('1997-12-01'):
        result = test_ctx.api.post(
            test_ctx.context_factory(
                input={
                    'names': ['tag1', 'tag2'],
                    'category': 'meta',
                    'suggestions': [],
                    'implications': [],
                },
                user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    assert result['tag'] == {
        'names': ['tag1', 'tag2'],
        'category': 'meta',
        'suggestions': [],
        'implications': [],
        'creationTime': datetime.datetime(1997, 12, 1),
        'lastEditTime': None,
        'usages': 0,
    }
    assert len(result['snapshots']) == 1
    tag = tags.get_tag_by_name('tag1')
    assert [tag_name.name for tag_name in tag.names] == ['tag1', 'tag2']
    assert tag.category.name == 'meta'
    assert tag.last_edit_time is None
    assert tag.post_count == 0
    assert_relations(tag.suggestions, [])
    assert_relations(tag.implications, [])
    assert os.path.exists(os.path.join(config.config['data_dir'], 'tags.json'))
Beispiel #14
0
def update_tag(ctx, params):
    tag = tags.get_tag_by_name(params['tag_name'])
    versions.verify_version(tag, ctx)
    versions.bump_version(tag)
    if ctx.has_param('names'):
        auth.verify_privilege(ctx.user, 'tags:edit:names')
        tags.update_tag_names(tag, ctx.get_param_as_list('names'))
    if ctx.has_param('category'):
        auth.verify_privilege(ctx.user, 'tags:edit:category')
        tags.update_tag_category_name(
            tag, ctx.get_param_as_string('category'))
    if ctx.has_param('description'):
        auth.verify_privilege(ctx.user, 'tags:edit:description')
        tags.update_tag_description(
            tag, ctx.get_param_as_string('description', default=None))
    if ctx.has_param('suggestions'):
        auth.verify_privilege(ctx.user, 'tags:edit:suggestions')
        suggestions = ctx.get_param_as_list('suggestions')
        _create_if_needed(suggestions, ctx.user)
        tags.update_tag_suggestions(tag, suggestions)
    if ctx.has_param('implications'):
        auth.verify_privilege(ctx.user, 'tags:edit:implications')
        implications = ctx.get_param_as_list('implications')
        _create_if_needed(implications, ctx.user)
        tags.update_tag_implications(tag, implications)
    tag.last_edit_time = datetime.datetime.utcnow()
    ctx.session.flush()
    snapshots.modify(tag, ctx.user)
    ctx.session.commit()
    tags.export_to_json()
    return _serialize(ctx, tag)
Beispiel #15
0
def test_merge_tags_doesnt_create_relation_loop_for_parents(tag_factory):
    source_tag = tag_factory(names=['source'])
    target_tag = tag_factory(names=['target'])
    target_tag.suggestions = [source_tag]
    target_tag.implications = [source_tag]
    db.session.add_all([source_tag, target_tag])
    db.session.commit()
    assert source_tag.suggestion_count == 0
    assert source_tag.implication_count == 0
    assert target_tag.suggestion_count == 1
    assert target_tag.implication_count == 1
    tags.merge_tags(source_tag, target_tag)
    db.session.commit()
    assert tags.try_get_tag_by_name('source') is None
    assert tags.get_tag_by_name('target').suggestion_count == 0
    assert tags.get_tag_by_name('target').implication_count == 0
def test_simple_updating(test_ctx, fake_datetime):
    tag = test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta')
    db.session.add(tag)
    db.session.commit()
    with fake_datetime('1997-12-01'):
        result = test_ctx.api.put(
            test_ctx.context_factory(
                input={
                    'names': ['tag3'],
                    'category': 'character',
                },
                user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
            'tag1')
    assert result['tag'] == {
        'names': ['tag3'],
        'category': 'character',
        'suggestions': [],
        'implications': [],
        'creationTime': datetime.datetime(1996, 1, 1),
        'lastEditTime': datetime.datetime(1997, 12, 1),
        'usages': 0,
    }
    assert len(result['snapshots']) == 1
    assert tags.try_get_tag_by_name('tag1') is None
    assert tags.try_get_tag_by_name('tag2') is None
    tag = tags.get_tag_by_name('tag3')
    assert tag is not None
    assert [tag_name.name for tag_name in tag.names] == ['tag3']
    assert tag.category.name == 'character'
    assert tag.suggestions == []
    assert tag.implications == []
    assert os.path.exists(os.path.join(config.config['data_dir'], 'tags.json'))
def test_simple_updating(test_ctx, fake_datetime):
    tag = test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta')
    db.session.add(tag)
    db.session.commit()
    with fake_datetime('1997-12-01'):
        result = test_ctx.api.put(
            test_ctx.context_factory(
                input={
                    'names': ['tag3'],
                    'category': 'character',
                },
                user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'tag1')
    assert result['tag'] == {
        'names': ['tag3'],
        'category': 'character',
        'suggestions': [],
        'implications': [],
        'creationTime': datetime.datetime(1996, 1, 1),
        'lastEditTime': datetime.datetime(1997, 12, 1),
        'usages': 0,
    }
    assert len(result['snapshots']) == 1
    assert tags.try_get_tag_by_name('tag1') is None
    assert tags.try_get_tag_by_name('tag2') is None
    tag = tags.get_tag_by_name('tag3')
    assert tag is not None
    assert [tag_name.name for tag_name in tag.names] == ['tag3']
    assert tag.category.name == 'character'
    assert tag.suggestions == []
    assert tag.implications == []
    assert os.path.exists(os.path.join(config.config['data_dir'], 'tags.json'))
Beispiel #18
0
    def put(self, ctx, tag_name):
        tag = tags.get_tag_by_name(tag_name)
        if not tag:
            raise tags.TagNotFoundError('Tag %r not found.' % tag_name)

        if ctx.has_param('names'):
            auth.verify_privilege(ctx.user, 'tags:edit:names')
            tags.update_names(tag, ctx.get_param_as_list('names'))

        if ctx.has_param('category'):
            auth.verify_privilege(ctx.user, 'tags:edit:category')
            tags.update_category_name(tag, ctx.get_param_as_string('category'))

        if ctx.has_param('suggestions'):
            auth.verify_privilege(ctx.user, 'tags:edit:suggestions')
            tags.update_suggestions(tag, ctx.get_param_as_list('suggestions'))

        if ctx.has_param('implications'):
            auth.verify_privilege(ctx.user, 'tags:edit:implications')
            tags.update_implications(tag, ctx.get_param_as_list('implications'))

        tag.last_edit_time = datetime.datetime.now()
        snapshots.modify(tag, ctx.user)
        ctx.session.commit()
        tags.export_to_json()
        return _serialize_tag_with_details(tag)
Beispiel #19
0
def test_merge_tags_doesnt_create_relation_loop_for_parents(tag_factory):
    source_tag = tag_factory(names=['source'])
    target_tag = tag_factory(names=['target'])
    target_tag.suggestions = [source_tag]
    target_tag.implications = [source_tag]
    db.session.add_all([source_tag, target_tag])
    db.session.commit()
    assert source_tag.suggestion_count == 0
    assert source_tag.implication_count == 0
    assert target_tag.suggestion_count == 1
    assert target_tag.implication_count == 1
    tags.merge_tags(source_tag, target_tag)
    db.session.commit()
    assert tags.try_get_tag_by_name('source') is None
    assert tags.get_tag_by_name('target').suggestion_count == 0
    assert tags.get_tag_by_name('target').implication_count == 0
Beispiel #20
0
def test_merge_tags_moves_child_relations(tag_factory):
    source_tag = tag_factory(names=['source'])
    target_tag = tag_factory(names=['target'])
    related_tag = tag_factory()
    source_tag.suggestions = [related_tag]
    source_tag.implications = [related_tag]
    db.session.add_all([source_tag, target_tag, related_tag])
    db.session.commit()
    assert source_tag.suggestion_count == 1
    assert source_tag.implication_count == 1
    assert target_tag.suggestion_count == 0
    assert target_tag.implication_count == 0
    tags.merge_tags(source_tag, target_tag)
    db.session.commit()
    assert tags.try_get_tag_by_name('source') is None
    assert tags.get_tag_by_name('target').suggestion_count == 1
    assert tags.get_tag_by_name('target').implication_count == 1
Beispiel #21
0
def test_merge_tags_moves_child_relations(tag_factory):
    source_tag = tag_factory(names=['source'])
    target_tag = tag_factory(names=['target'])
    related_tag = tag_factory()
    source_tag.suggestions = [related_tag]
    source_tag.implications = [related_tag]
    db.session.add_all([source_tag, target_tag, related_tag])
    db.session.commit()
    assert source_tag.suggestion_count == 1
    assert source_tag.implication_count == 1
    assert target_tag.suggestion_count == 0
    assert target_tag.implication_count == 0
    tags.merge_tags(source_tag, target_tag)
    db.session.commit()
    assert tags.try_get_tag_by_name('source') is None
    assert tags.get_tag_by_name('target').suggestion_count == 1
    assert tags.get_tag_by_name('target').implication_count == 1
Beispiel #22
0
def delete_tag(ctx, params):
    tag = tags.get_tag_by_name(params['tag_name'])
    versions.verify_version(tag, ctx)
    auth.verify_privilege(ctx.user, 'tags:delete')
    snapshots.delete(tag, ctx.user)
    tags.delete(tag)
    ctx.session.commit()
    tags.export_to_json()
    return {}
Beispiel #23
0
def test_merge_tags_deletes_source_tag(tag_factory):
    source_tag = tag_factory(names=['source'])
    target_tag = tag_factory(names=['target'])
    db.session.add_all([source_tag, target_tag])
    db.session.flush()
    tags.merge_tags(source_tag, target_tag)
    db.session.flush()
    assert tags.try_get_tag_by_name('source') is None
    tag = tags.get_tag_by_name('target')
    assert tag is not None
Beispiel #24
0
def test_merge_tags_doesnt_duplicate_child_relations(tag_factory):
    source_tag = tag_factory(names=["source"])
    target_tag = tag_factory(names=["target"])
    related_tag = tag_factory()
    source_tag.suggestions = [related_tag]
    source_tag.implications = [related_tag]
    target_tag.suggestions = [related_tag]
    target_tag.implications = [related_tag]
    db.session.add_all([source_tag, target_tag, related_tag])
    db.session.commit()
    assert source_tag.suggestion_count == 1
    assert source_tag.implication_count == 1
    assert target_tag.suggestion_count == 1
    assert target_tag.implication_count == 1
    tags.merge_tags(source_tag, target_tag)
    db.session.commit()
    assert tags.try_get_tag_by_name("source") is None
    assert tags.get_tag_by_name("target").suggestion_count == 1
    assert tags.get_tag_by_name("target").implication_count == 1
Beispiel #25
0
def test_merge_tags_moves_parent_relations(tag_factory):
    source_tag = tag_factory(names=["source"])
    target_tag = tag_factory(names=["target"])
    related_tag = tag_factory(names=["related"])
    related_tag.suggestions = [related_tag]
    related_tag.implications = [related_tag]
    db.session.add_all([source_tag, target_tag, related_tag])
    db.session.commit()
    assert source_tag.suggestion_count == 0
    assert source_tag.implication_count == 0
    assert target_tag.suggestion_count == 0
    assert target_tag.implication_count == 0
    tags.merge_tags(source_tag, target_tag)
    db.session.commit()
    assert tags.try_get_tag_by_name("source") is None
    assert tags.get_tag_by_name("related").suggestion_count == 1
    assert tags.get_tag_by_name("related").suggestion_count == 1
    assert tags.get_tag_by_name("target").suggestion_count == 0
    assert tags.get_tag_by_name("target").implication_count == 0
Beispiel #26
0
def test_merge_tags_deletes_source_tag(tag_factory):
    source_tag = tag_factory(names=['source'])
    target_tag = tag_factory(names=['target'])
    db.session.add_all([source_tag, target_tag])
    db.session.flush()
    tags.merge_tags(source_tag, target_tag)
    db.session.flush()
    assert tags.try_get_tag_by_name('source') is None
    tag = tags.get_tag_by_name('target')
    assert tag is not None
Beispiel #27
0
 def get(self, ctx, tag_name):
     auth.verify_privilege(ctx.user, 'tags:view')
     tag = tags.get_tag_by_name(tag_name)
     result = tags.get_tag_siblings(tag)
     serialized_siblings = []
     for sibling, occurrences in result:
         serialized_siblings.append({
             'tag': tags.serialize_tag(sibling),
             'occurrences': occurrences
         })
     return {'siblings': serialized_siblings}
Beispiel #28
0
 def post(self, ctx):
     source_tag_name = ctx.get_param_as_string('remove', required=True) or ''
     target_tag_name = ctx.get_param_as_string('merge-to', required=True) or ''
     source_tag = tags.get_tag_by_name(source_tag_name)
     target_tag = tags.get_tag_by_name(target_tag_name)
     if not source_tag:
         raise tags.TagNotFoundError(
             'Source tag %r not found.' % source_tag_name)
     if not target_tag:
         raise tags.TagNotFoundError(
             'Source tag %r not found.' % target_tag_name)
     if source_tag.tag_id == target_tag.tag_id:
         raise tags.InvalidTagRelationError(
             'Cannot merge tag with itself.')
     auth.verify_privilege(ctx.user, 'tags:merge')
     tags.merge_tags(source_tag, target_tag)
     snapshots.delete(source_tag, ctx.user)
     ctx.session.commit()
     tags.export_to_json()
     return _serialize_tag_with_details(target_tag)
Beispiel #29
0
def get_tag_siblings(ctx, params):
    auth.verify_privilege(ctx.user, 'tags:view')
    tag = tags.get_tag_by_name(params['tag_name'])
    result = tags.get_tag_siblings(tag)
    serialized_siblings = []
    for sibling, occurrences in result:
        serialized_siblings.append({
            'tag': _serialize(ctx, sibling),
            'occurrences': occurrences
        })
    return {'results': serialized_siblings}
Beispiel #30
0
 def get(self, ctx, tag_name):
     auth.verify_privilege(ctx.user, 'tags:view')
     tag = tags.get_tag_by_name(tag_name)
     result = tags.get_tag_siblings(tag)
     serialized_siblings = []
     for sibling, occurrences in result:
         serialized_siblings.append({
             'tag': tags.serialize_tag(sibling),
             'occurrences': occurrences
         })
     return {'siblings': serialized_siblings}
def test_creating_new_suggestions_and_implications(
        test_ctx, input, expected_suggestions, expected_implications):
    result = test_ctx.api.post(
        test_ctx.context_factory(
            input=input, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    assert result['tag']['suggestions'] == expected_suggestions
    assert result['tag']['implications'] == expected_implications
    tag = tags.get_tag_by_name('main')
    assert_relations(tag.suggestions, expected_suggestions)
    assert_relations(tag.implications, expected_implications)
    for name in ['main'] + expected_suggestions + expected_implications:
        assert tags.try_get_tag_by_name(name) is not None
Beispiel #32
0
 def delete(self, ctx, tag_name):
     tag = tags.get_tag_by_name(tag_name)
     if tag.post_count > 0:
         raise tags.TagIsInUseError(
             'Tag has some usages and cannot be deleted. ' +
             'Please untag relevant posts first.')
     auth.verify_privilege(ctx.user, 'tags:delete')
     snapshots.save_entity_deletion(tag, ctx.user)
     tags.delete(tag)
     ctx.session.commit()
     tags.export_to_json()
     return {}
Beispiel #33
0
 def delete(self, ctx, tag_name):
     tag = tags.get_tag_by_name(tag_name)
     if tag.post_count > 0:
         raise tags.TagIsInUseError(
             'Tag has some usages and cannot be deleted. ' +
             'Please untag relevant posts first.')
     auth.verify_privilege(ctx.user, 'tags:delete')
     snapshots.save_entity_deletion(tag, ctx.user)
     tags.delete(tag)
     ctx.session.commit()
     tags.export_to_json()
     return {}
def test_duplicating_names(test_ctx):
    db.session.add(
        test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta'))
    result = test_ctx.api.put(
        test_ctx.context_factory(
            input={'names': ['tag3', 'TAG3']},
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'tag1')
    assert result['tag']['names'] == ['tag3']
    assert tags.try_get_tag_by_name('tag1') is None
    assert tags.try_get_tag_by_name('tag2') is None
    tag = tags.get_tag_by_name('tag3')
    assert tag is not None
    assert [tag_name.name for tag_name in tag.names] == ['tag3']
Beispiel #35
0
 def get(self, ctx, tag_name):
     auth.verify_privilege(ctx.user, 'tags:view')
     tag = tags.get_tag_by_name(tag_name)
     if not tag:
         raise tags.TagNotFoundError('Tag %r not found.' % tag_name)
     result = tags.get_siblings(tag)
     serialized_siblings = []
     for sibling, occurrences in result:
         serialized_siblings.append({
             'tag': _serialize_tag(sibling),
             'occurrences': occurrences
         })
     return {'siblings': serialized_siblings}
Beispiel #36
0
def test_merge_tags_doesnt_duplicate_usages(tag_factory, post_factory):
    source_tag = tag_factory(names=['source'])
    target_tag = tag_factory(names=['target'])
    post = post_factory()
    post.tags = [source_tag, target_tag]
    db.session.add_all([source_tag, target_tag, post])
    db.session.flush()
    assert source_tag.post_count == 1
    assert target_tag.post_count == 1
    tags.merge_tags(source_tag, target_tag)
    db.session.flush()
    assert tags.try_get_tag_by_name('source') is None
    assert tags.get_tag_by_name('target').post_count == 1
Beispiel #37
0
def test_merge_tags_moves_usages(tag_factory, post_factory):
    source_tag = tag_factory(names=['source'])
    target_tag = tag_factory(names=['target'])
    post = post_factory()
    post.tags = [source_tag]
    db.session.add_all([source_tag, target_tag, post])
    db.session.commit()
    assert source_tag.post_count == 1
    assert target_tag.post_count == 0
    tags.merge_tags(source_tag, target_tag)
    db.session.commit()
    assert tags.try_get_tag_by_name('source') is None
    assert tags.get_tag_by_name('target').post_count == 1
Beispiel #38
0
def test_merge_tags_doesnt_duplicate_usages(tag_factory, post_factory):
    source_tag = tag_factory(names=['source'])
    target_tag = tag_factory(names=['target'])
    post = post_factory()
    post.tags = [source_tag, target_tag]
    db.session.add_all([source_tag, target_tag, post])
    db.session.flush()
    assert source_tag.post_count == 1
    assert target_tag.post_count == 1
    tags.merge_tags(source_tag, target_tag)
    db.session.flush()
    assert tags.try_get_tag_by_name('source') is None
    assert tags.get_tag_by_name('target').post_count == 1
Beispiel #39
0
def test_merge_tags_moves_usages(tag_factory, post_factory):
    source_tag = tag_factory(names=['source'])
    target_tag = tag_factory(names=['target'])
    post = post_factory()
    post.tags = [source_tag]
    db.session.add_all([source_tag, target_tag, post])
    db.session.commit()
    assert source_tag.post_count == 1
    assert target_tag.post_count == 0
    tags.merge_tags(source_tag, target_tag)
    db.session.commit()
    assert tags.try_get_tag_by_name('source') is None
    assert tags.get_tag_by_name('target').post_count == 1
def test_duplicating_names(test_ctx):
    result = test_ctx.api.post(
        test_ctx.context_factory(
            input={
                'names': ['tag1', 'TAG1'],
                'category': 'meta',
                'suggestions': [],
                'implications': [],
            },
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    assert result['tag']['names'] == ['tag1']
    assert result['tag']['category'] == 'meta'
    tag = tags.get_tag_by_name('tag1')
    assert [tag_name.name for tag_name in tag.names] == ['tag1']
def test_duplicating_names(test_ctx):
    result = test_ctx.api.post(
        test_ctx.context_factory(
            input={
                'names': ['tag1', 'TAG1'],
                'category': 'meta',
                'suggestions': [],
                'implications': [],
            },
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    assert result['tag']['names'] == ['tag1']
    assert result['tag']['category'] == 'meta'
    tag = tags.get_tag_by_name('tag1')
    assert [tag_name.name for tag_name in tag.names] == ['tag1']
def test_creating_new_suggestions_and_implications(test_ctx, input,
                                                   expected_suggestions,
                                                   expected_implications):
    result = test_ctx.api.post(
        test_ctx.context_factory(
            input=input,
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    assert result['tag']['suggestions'] == expected_suggestions
    assert result['tag']['implications'] == expected_implications
    tag = tags.get_tag_by_name('main')
    assert_relations(tag.suggestions, expected_suggestions)
    assert_relations(tag.implications, expected_implications)
    for name in ['main'] + expected_suggestions + expected_implications:
        assert tags.try_get_tag_by_name(name) is not None
def test_duplicating_names(test_ctx):
    db.session.add(
        test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta'))
    result = test_ctx.api.put(
        test_ctx.context_factory(
            input={'names': ['tag3', 'TAG3']},
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
        'tag1')
    assert result['tag']['names'] == ['tag3']
    assert tags.try_get_tag_by_name('tag1') is None
    assert tags.try_get_tag_by_name('tag2') is None
    tag = tags.get_tag_by_name('tag3')
    assert tag is not None
    assert [tag_name.name for tag_name in tag.names] == ['tag3']
def test_updating_new_suggestions_and_implications(test_ctx, input,
                                                   expected_suggestions,
                                                   expected_implications):
    db.session.add(test_ctx.tag_factory(names=['main'], category_name='meta'))
    db.session.commit()
    result = test_ctx.api.put(
        test_ctx.context_factory(
            input=input,
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'main')
    assert result['tag']['suggestions'] == expected_suggestions
    assert result['tag']['implications'] == expected_implications
    tag = tags.get_tag_by_name('main')
    assert_relations(tag.suggestions, expected_suggestions)
    assert_relations(tag.implications, expected_implications)
    for name in ['main'] + expected_suggestions + expected_implications:
        assert tags.try_get_tag_by_name(name) is not None
def test_updating_new_suggestions_and_implications(
        test_ctx, input, expected_suggestions, expected_implications):
    db.session.add(
        test_ctx.tag_factory(names=['main'], category_name='meta'))
    db.session.commit()
    result = test_ctx.api.put(
        test_ctx.context_factory(
            input=input, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
        'main')
    assert result['tag']['suggestions'] == expected_suggestions
    assert result['tag']['implications'] == expected_implications
    tag = tags.get_tag_by_name('main')
    assert_relations(tag.suggestions, expected_suggestions)
    assert_relations(tag.implications, expected_implications)
    for name in ['main'] + expected_suggestions + expected_implications:
        assert tags.try_get_tag_by_name(name) is not None
def test_reusing_suggestions_and_implications(test_ctx):
    db.session.add_all([
        test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta'),
        test_ctx.tag_factory(names=['tag3'], category_name='meta'),
    ])
    db.session.commit()
    result = test_ctx.api.post(
        test_ctx.context_factory(
            input={
                'names': ['new'],
                'category': 'meta',
                'suggestions': ['TAG2'],
                'implications': ['tag1'],
            },
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    # NOTE: it should export only the first name
    assert result['tag']['suggestions'] == ['tag1']
    assert result['tag']['implications'] == ['tag1']
    tag = tags.get_tag_by_name('new')
    assert_relations(tag.suggestions, ['tag1'])
    assert_relations(tag.implications, ['tag1'])
def test_reusing_suggestions_and_implications(test_ctx):
    db.session.add_all([
        test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta'),
        test_ctx.tag_factory(names=['tag3'], category_name='meta'),
    ])
    db.session.commit()
    result = test_ctx.api.post(
        test_ctx.context_factory(
            input={
                'names': ['new'],
                'category': 'meta',
                'suggestions': ['TAG2'],
                'implications': ['tag1'],
            },
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    # NOTE: it should export only the first name
    assert result['tag']['suggestions'] == ['tag1']
    assert result['tag']['implications'] == ['tag1']
    tag = tags.get_tag_by_name('new')
    assert_relations(tag.suggestions, ['tag1'])
    assert_relations(tag.implications, ['tag1'])
Beispiel #48
0
 def put(self, ctx, tag_name):
     tag = tags.get_tag_by_name(tag_name)
     if ctx.has_param('names'):
         auth.verify_privilege(ctx.user, 'tags:edit:names')
         tags.update_tag_names(tag, ctx.get_param_as_list('names'))
     if ctx.has_param('category'):
         auth.verify_privilege(ctx.user, 'tags:edit:category')
         tags.update_tag_category_name(tag,
                                       ctx.get_param_as_string('category'))
     if ctx.has_param('suggestions'):
         auth.verify_privilege(ctx.user, 'tags:edit:suggestions')
         tags.update_tag_suggestions(tag,
                                     ctx.get_param_as_list('suggestions'))
     if ctx.has_param('implications'):
         auth.verify_privilege(ctx.user, 'tags:edit:implications')
         tags.update_tag_implications(tag,
                                      ctx.get_param_as_list('implications'))
     tag.last_edit_time = datetime.datetime.now()
     ctx.session.flush()
     snapshots.save_entity_modification(tag, ctx.user)
     ctx.session.commit()
     tags.export_to_json()
     return tags.serialize_tag_with_details(tag)
Beispiel #49
0
 def put(self, ctx, tag_name):
     tag = tags.get_tag_by_name(tag_name)
     if ctx.has_param('names'):
         auth.verify_privilege(ctx.user, 'tags:edit:names')
         tags.update_tag_names(tag, ctx.get_param_as_list('names'))
     if ctx.has_param('category'):
         auth.verify_privilege(ctx.user, 'tags:edit:category')
         tags.update_tag_category_name(
             tag, ctx.get_param_as_string('category'))
     if ctx.has_param('suggestions'):
         auth.verify_privilege(ctx.user, 'tags:edit:suggestions')
         tags.update_tag_suggestions(
             tag, ctx.get_param_as_list('suggestions'))
     if ctx.has_param('implications'):
         auth.verify_privilege(ctx.user, 'tags:edit:implications')
         tags.update_tag_implications(
             tag, ctx.get_param_as_list('implications'))
     tag.last_edit_time = datetime.datetime.now()
     ctx.session.flush()
     snapshots.save_entity_modification(tag, ctx.user)
     ctx.session.commit()
     tags.export_to_json()
     return tags.serialize_tag_with_details(tag)
Beispiel #50
0
def test_merging_with_usages(test_ctx, fake_datetime, post_factory):
    source_tag = test_ctx.tag_factory(names=['source'], category_name='meta')
    target_tag = test_ctx.tag_factory(names=['target'], category_name='meta')
    db.session.add_all([source_tag, target_tag])
    db.session.flush()
    assert source_tag.post_count == 0
    assert target_tag.post_count == 0
    post = post_factory()
    post.tags = [source_tag]
    db.session.add(post)
    db.session.commit()
    assert source_tag.post_count == 1
    assert target_tag.post_count == 0
    with fake_datetime('1997-12-01'):
        result = test_ctx.api.post(
            test_ctx.context_factory(
                input={
                    'remove': 'source',
                    'mergeTo': 'target',
                },
                user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    assert tags.try_get_tag_by_name('source') is None
    assert tags.get_tag_by_name('target').post_count == 1
Beispiel #51
0
 def get(self, ctx, tag_name):
     auth.verify_privilege(ctx.user, 'tags:view')
     tag = tags.get_tag_by_name(tag_name)
     return tags.serialize_tag_with_details(tag)
Beispiel #52
0
 def get(self, ctx, tag_name):
     auth.verify_privilege(ctx.user, 'tags:view')
     tag = tags.get_tag_by_name(tag_name)
     if not tag:
         raise tags.TagNotFoundError('Tag %r not found.' % tag_name)
     return _serialize_tag_with_details(tag)
Beispiel #53
0
def get_tag(ctx, params):
    auth.verify_privilege(ctx.user, 'tags:view')
    tag = tags.get_tag_by_name(params['tag_name'])
    return _serialize(ctx, tag)
Beispiel #54
0
def _get_tag(params: Dict[str, str]) -> model.Tag:
    return tags.get_tag_by_name(params["tag_name"])