Beispiel #1
0
def generate_post_signature(post: model.Post, content: bytes) -> None:
    try:
        unpacked_signature = image_hash.generate_signature(content)
        packed_signature = image_hash.pack_signature(unpacked_signature)
        words = image_hash.generate_words(unpacked_signature)

        db.session.add(model.PostSignature(
            post=post, signature=packed_signature, words=words))
    except errors.ProcessingError:
        if not config.config['allow_broken_uploads']:
            raise InvalidPostContentError(
                'Unable to generate image hash data.')
Beispiel #2
0
def test_cascade_deletions(post_factory, user_factory, tag_factory,
                           comment_factory):
    user = user_factory()
    tag1 = tag_factory()
    tag2 = tag_factory()
    related_post1 = post_factory()
    related_post2 = post_factory()
    post = post_factory()
    comment = comment_factory(post=post, user=user)
    db.session.add_all(
        [user, tag1, tag2, post, related_post1, related_post2, comment])
    db.session.flush()

    score = model.PostScore()
    score.post = post
    score.user = user
    score.time = datetime(1997, 1, 1)
    score.score = 1
    favorite = model.PostFavorite()
    favorite.post = post
    favorite.user = user
    favorite.time = datetime(1997, 1, 1)
    feature = model.PostFeature()
    feature.post = post
    feature.user = user
    feature.time = datetime(1997, 1, 1)
    note = model.PostNote()
    note.post = post
    note.polygon = ''
    note.text = ''
    signature = model.PostSignature()
    signature.post = post
    signature.signature = b'testvalue'
    signature.words = list(range(50))
    db.session.add_all([score, favorite, feature, note, signature])
    db.session.flush()

    post.user = user
    post.tags.append(tag1)
    post.tags.append(tag2)
    post.relations.append(related_post1)
    related_post2.relations.append(post)
    post.scores.append(score)
    post.favorited_by.append(favorite)
    post.features.append(feature)
    post.notes.append(note)
    db.session.commit()

    assert not db.session.dirty
    assert post.user is not None and post.user.user_id is not None
    assert len(post.relations) == 1
    assert db.session.query(model.User).count() == 1
    assert db.session.query(model.Tag).count() == 2
    assert db.session.query(model.Post).count() == 3
    assert db.session.query(model.PostTag).count() == 2
    assert db.session.query(model.PostRelation).count() == 2
    assert db.session.query(model.PostScore).count() == 1
    assert db.session.query(model.PostNote).count() == 1
    assert db.session.query(model.PostFeature).count() == 1
    assert db.session.query(model.PostFavorite).count() == 1
    assert db.session.query(model.PostSignature).count() == 1
    assert db.session.query(model.Comment).count() == 1

    db.session.delete(post)
    db.session.commit()

    assert not db.session.dirty
    assert db.session.query(model.User).count() == 1
    assert db.session.query(model.Tag).count() == 2
    assert db.session.query(model.Post).count() == 2
    assert db.session.query(model.PostTag).count() == 0
    assert db.session.query(model.PostRelation).count() == 0
    assert db.session.query(model.PostScore).count() == 0
    assert db.session.query(model.PostNote).count() == 0
    assert db.session.query(model.PostFeature).count() == 0
    assert db.session.query(model.PostFavorite).count() == 0
    assert db.session.query(model.PostSignature).count() == 0
    assert db.session.query(model.Comment).count() == 0