Esempio n. 1
0
    def post(self, ctx):
        auth.verify_privilege(ctx.user, 'posts:create')
        content = ctx.get_file('content', required=True)
        tag_names = ctx.get_param_as_list('tags', required=True)
        safety = ctx.get_param_as_string('safety', required=True)
        source = ctx.get_param_as_string('source',
                                         required=False,
                                         default=None)
        if ctx.has_param('contentUrl') and not source:
            source = ctx.get_param_as_string('contentUrl')
        relations = ctx.get_param_as_list('relations', required=False) or []
        notes = ctx.get_param_as_list('notes', required=False) or []
        flags = ctx.get_param_as_list('flags', required=False) or []

        post = posts.create_post(content, tag_names, ctx.user)
        posts.update_post_safety(post, safety)
        posts.update_post_source(post, source)
        posts.update_post_relations(post, relations)
        posts.update_post_notes(post, notes)
        posts.update_post_flags(post, flags)
        if ctx.has_file('thumbnail'):
            posts.update_post_thumbnail(post, ctx.get_file('thumbnail'))
        ctx.session.add(post)
        snapshots.save_entity_creation(post, ctx.user)
        ctx.session.commit()
        tags.export_to_json()
        return posts.serialize_post_with_details(post, ctx.user)
Esempio n. 2
0
    def post(self, ctx):
        auth.verify_privilege(ctx.user, 'posts:create')
        content = ctx.get_file('content', required=True)
        tag_names = ctx.get_param_as_list('tags', required=True)
        safety = ctx.get_param_as_string('safety', required=True)
        source = ctx.get_param_as_string('source', required=False, default=None)
        if ctx.has_param('contentUrl') and not source:
            source = ctx.get_param_as_string('contentUrl')
        relations = ctx.get_param_as_list('relations', required=False) or []
        notes = ctx.get_param_as_list('notes', required=False) or []
        flags = ctx.get_param_as_list('flags', required=False) or []

        post = posts.create_post(content, tag_names, ctx.user)
        posts.update_post_safety(post, safety)
        posts.update_post_source(post, source)
        posts.update_post_relations(post, relations)
        posts.update_post_notes(post, notes)
        posts.update_post_flags(post, flags)
        if ctx.has_file('thumbnail'):
            posts.update_post_thumbnail(post, ctx.get_file('thumbnail'))
        ctx.session.add(post)
        snapshots.save_entity_creation(post, ctx.user)
        ctx.session.commit()
        tags.export_to_json()
        return posts.serialize_post_with_details(post, ctx.user)
Esempio n. 3
0
def test_not_merging_operations_by_different_users(
        fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user1, user2 = [user_factory(), user_factory()]
    db.session.add_all([tag, user1, user2])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user1)
        tag.names = [db.TagName('changed')]
        snapshots.save_entity_modification(tag, user2)
    assert db.session.query(db.Snapshot).count() == 2
Esempio n. 4
0
def test_not_merging_operations_by_different_users(fake_datetime, tag_factory,
                                                   user_factory):
    tag = tag_factory(names=['dummy'])
    user1, user2 = [user_factory(), user_factory()]
    db.session.add_all([tag, user1, user2])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user1)
        tag.names = [db.TagName('changed')]
        snapshots.save_entity_modification(tag, user2)
    assert db.session.query(db.Snapshot).count() == 2
Esempio n. 5
0
 def post(self, ctx):
     auth.verify_privilege(ctx.user, 'tag_categories:create')
     name = ctx.get_param_as_string('name', required=True)
     color = ctx.get_param_as_string('color', required=True)
     category = tag_categories.create_category(name, color)
     ctx.session.add(category)
     ctx.session.flush()
     snapshots.save_entity_creation(category, ctx.user)
     ctx.session.commit()
     tags.export_to_json()
     return tag_categories.serialize_category_with_details(category)
Esempio n. 6
0
def test_merging_modification_to_creation(tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[0].data['names'] == ['changed']
Esempio n. 7
0
def test_merging_modification_to_creation(tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[0].data['names'] == ['changed']
Esempio n. 8
0
def test_not_merging_due_to_time_difference(
        fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:10:01'):
        snapshots.save_entity_modification(tag, user)
    assert db.session.query(db.Snapshot).count() == 2
Esempio n. 9
0
def test_not_merging_due_to_time_difference(fake_datetime, tag_factory,
                                            user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:10:01'):
        snapshots.save_entity_modification(tag, user)
    assert db.session.query(db.Snapshot).count() == 2
Esempio n. 10
0
def test_not_adding_snapshot_if_data_doesnt_change(fake_datetime, tag_factory,
                                                   user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    with fake_datetime('14:00:00'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[0].data['names'] == ['dummy']
Esempio n. 11
0
def test_not_adding_snapshot_if_data_doesnt_change(
        fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    with fake_datetime('14:00:00'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[0].data['names'] == ['dummy']
Esempio n. 12
0
def test_merging_deletion_all_the_way_deletes_all_snapshots(
        fake_datetime, tag_factory, user_factory, expected_operation):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:00:01'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('13:00:02'):
        snapshots.save_entity_deletion(tag, user)
    assert db.session.query(db.Snapshot).count() == 0
Esempio n. 13
0
def test_merging_deletion_all_the_way_deletes_all_snapshots(
        fake_datetime, tag_factory, user_factory, expected_operation):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:00:01'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('13:00:02'):
        snapshots.save_entity_deletion(tag, user)
    assert db.session.query(db.Snapshot).count() == 0
Esempio n. 14
0
    def post(self, ctx):
        auth.verify_privilege(ctx.user, 'tags:create')

        names = ctx.get_param_as_list('names', required=True)
        category = ctx.get_param_as_string('category', required=True)
        suggestions = ctx.get_param_as_list(
            'suggestions', required=False, default=[])
        implications = ctx.get_param_as_list(
            'implications', required=False, default=[])

        tag = tags.create_tag(names, category, suggestions, implications)
        ctx.session.add(tag)
        ctx.session.flush()
        snapshots.save_entity_creation(tag, ctx.user)
        ctx.session.commit()
        tags.export_to_json()
        return tags.serialize_tag_with_details(tag)
Esempio n. 15
0
def test_get_serialized_history(fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory(name='the-user')
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('2016-04-19 13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    db.session.flush()
    with fake_datetime('2016-04-19 13:10:01'):
        snapshots.save_entity_modification(tag, user)
    assert snapshots.get_serialized_history(tag) == [
        {
            'operation': 'modified',
            'time': datetime.datetime(2016, 4, 19, 13, 10, 1),
            'type': 'tag',
            'id': 'changed',
            'user': '******',
            'data': {
                'names': ['changed'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
            'earlier-data': {
                'names': ['dummy'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
        },
        {
            'operation': 'created',
            'time': datetime.datetime(2016, 4, 19, 13, 0, 0),
            'type': 'tag',
            'id': 'dummy',
            'user': '******',
            'data': {
                'names': ['dummy'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
            'earlier-data': None,
        },
    ]
Esempio n. 16
0
def test_merging_resets_merging_time_window(
        fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:09:59'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('13:19:59'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].data['names'] == ['changed again']
Esempio n. 17
0
def test_get_serialized_history(fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory(name='the-user')
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('2016-04-19 13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    db.session.flush()
    with fake_datetime('2016-04-19 13:10:01'):
        snapshots.save_entity_modification(tag, user)
    assert snapshots.get_serialized_history(tag) == [
        {
            'operation': 'modified',
            'time': datetime.datetime(2016, 4, 19, 13, 10, 1),
            'type': 'tag',
            'id': 'changed',
            'user': '******',
            'data': {
                'names': ['changed'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
            'earlier-data': {
                'names': ['dummy'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
        },
        {
            'operation': 'created',
            'time': datetime.datetime(2016, 4, 19, 13, 0, 0),
            'type': 'tag',
            'id': 'dummy',
            'user': '******',
            'data': {
                'names': ['dummy'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
            'earlier-data': None,
        },
    ]
Esempio n. 18
0
def test_merging_resets_merging_time_window(fake_datetime, tag_factory,
                                            user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:09:59'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('13:19:59'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].data['names'] == ['changed again']
Esempio n. 19
0
def test_merging_modifications(fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('14:00:00'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('14:00:01'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 2
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[1].operation == db.Snapshot.OPERATION_MODIFIED
    assert results[0].data['names'] == ['dummy']
    assert results[1].data['names'] == ['changed again']
Esempio n. 20
0
    def post(self, ctx):
        auth.verify_privilege(ctx.user, 'tags:create')

        names = ctx.get_param_as_list('names', required=True)
        category = ctx.get_param_as_string('category', required=True)
        suggestions = ctx.get_param_as_list('suggestions',
                                            required=False,
                                            default=[])
        implications = ctx.get_param_as_list('implications',
                                             required=False,
                                             default=[])

        tag = tags.create_tag(names, category, suggestions, implications)
        ctx.session.add(tag)
        ctx.session.flush()
        snapshots.save_entity_creation(tag, ctx.user)
        ctx.session.commit()
        tags.export_to_json()
        return tags.serialize_tag_with_details(tag)
Esempio n. 21
0
def test_merging_modifications(fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('14:00:00'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('14:00:01'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 2
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[1].operation == db.Snapshot.OPERATION_MODIFIED
    assert results[0].data['names'] == ['dummy']
    assert results[1].data['names'] == ['changed again']