class AnnotationResource:
    def __init__(self, annotation_service: AnnotationsService):
        self._annotation_service = annotation_service

    @falcon.before(require_scope, "annotate:fragments")
    @validate(AnnotationsSchema(), AnnotationsSchema())
    def on_post(self, req: falcon.Request, resp: falcon.Response, number: str):
        if number == req.media.get("fragmentNumber"):
            annotations = self._annotation_service.update(
                AnnotationsSchema().load(req.media), req.context.user)
            resp.media = AnnotationsSchema().dump(annotations)
        else:
            raise falcon.HTTPUnprocessableEntity(
                description="Fragment numbers do not match.")

    @falcon.before(require_scope, "read:fragments")
    @validate(None, AnnotationsSchema())
    def on_get(self, req, resp: falcon.Response, number: str):
        museum_number = parse_museum_number(number)
        if req.params.get("generateAnnotations") == "true":
            annotations = self._annotation_service.generate_annotations(
                museum_number)
        else:
            annotations = self._annotation_service.find(museum_number)
        resp.media = AnnotationsSchema().dump(annotations)
 def on_post(self, req: falcon.Request, resp: falcon.Response, number: str):
     if number == req.media.get("fragmentNumber"):
         annotations = self._annotation_service.update(
             AnnotationsSchema().load(req.media), req.context.user)
         resp.media = AnnotationsSchema().dump(annotations)
     else:
         raise falcon.HTTPUnprocessableEntity(
             description="Fragment numbers do not match.")
Beispiel #3
0
def test_update_invalid_number(client):
    annotations = AnnotationsFactory.build()
    body = AnnotationsSchema().dumps(annotations)
    url = "/fragments/invalid/annotations"
    post_result = client.simulate_post(url, body=body)

    assert post_result.status == falcon.HTTP_UNPROCESSABLE_ENTITY
    def query_by_museum_number(self, number: MuseumNumber) -> Annotations:
        try:
            result = self._collection.find_one({"fragmentNumber": str(number)})

            return AnnotationsSchema().load(result, unknown=EXCLUDE)
        except NotFoundError:
            return Annotations(number)
Beispiel #5
0
def test_update_not_allowed(guest_client):
    annotations = AnnotationsFactory.build()
    body = AnnotationsSchema().dumps(annotations)
    url = "/fragments/not match/annotations"
    result = guest_client.simulate_post(url, body=body)

    assert result.status == falcon.HTTP_FORBIDDEN
 def retrieve_all_non_empty(self) -> List[Annotations]:
     result = self._collection.find_many(
         {"annotations": {
             "$exists": True,
             "$ne": []
         }})
     return AnnotationsSchema().load(result, unknown=EXCLUDE, many=True)
def test_query_by_museum_number(database, annotations_repository):
    annotations = AnnotationsFactory.build()
    fragment_number = annotations.fragment_number

    database[COLLECTION].insert_one(AnnotationsSchema().dump(annotations))

    assert annotations_repository.query_by_museum_number(fragment_number) == annotations
Beispiel #8
0
def test_update(client):
    annotations = AnnotationsFactory.build()
    fragment_number = annotations.fragment_number
    body = AnnotationsSchema().dumps(annotations)
    url = f"/fragments/{fragment_number}/annotations"
    post_result = client.simulate_post(url, body=body)

    expected_json = AnnotationsSchema().dump(annotations)

    assert post_result.status == falcon.HTTP_OK
    assert post_result.json == expected_json

    get_result = client.simulate_get(
        f"/fragments/{fragment_number}/annotations",
        params={"generateAnnotations": False},
    )
    assert get_result.json == expected_json
 def on_get(self, req, resp: falcon.Response, number: str):
     museum_number = parse_museum_number(number)
     if req.params.get("generateAnnotations") == "true":
         annotations = self._annotation_service.generate_annotations(
             museum_number)
     else:
         annotations = self._annotation_service.find(museum_number)
     resp.media = AnnotationsSchema().dump(annotations)
def test_create(database, annotations_repository):
    annotations = AnnotationsFactory.build()
    fragment_number = annotations.fragment_number

    annotations_repository.create_or_update(annotations)

    assert database[COLLECTION].find_one(
        {"fragmentNumber": str(fragment_number)}, {"_id": False}
    ) == AnnotationsSchema().dump(annotations)
def test_retrieve_all_non_empty(database, annotations_repository):
    empty_annotation = AnnotationsFactory.build(annotations=[])
    annotations = AnnotationsFactory.build_batch(4)

    database[COLLECTION].insert_many(
        AnnotationsSchema(many=True).dump([*annotations, empty_annotation])
    )

    assert annotations_repository.retrieve_all_non_empty() == annotations
 def update(self, annotations: Annotations, user: User) -> Annotations:
     old_annotations = self._annotations_repository.query_by_museum_number(
         annotations.fragment_number)
     _id = str(annotations.fragment_number)
     schema = AnnotationsSchema()
     self._changelog.create(
         "annotations",
         user.profile,
         {
             "_id": _id,
             **schema.dump(old_annotations)
         },
         {
             "_id": _id,
             **schema.dump(annotations)
         },
     )
     self._annotations_repository.create_or_update(annotations)
     return annotations
def test_find_by_sign(database, annotations_repository):
    annotations = AnnotationsFactory.build_batch(5)
    sign_query = annotations[0].annotations[0].data.sign_name
    database[COLLECTION].insert_many(AnnotationsSchema(many=True).dump(annotations))

    results = annotations_repository.find_by_sign(sign_query)

    assert len(results) >= 1
    for result in results:
        for annotation in result.annotations:
            assert annotation.data.sign_name == sign_query
Beispiel #14
0
def test_find_annotations(client):
    fragment_number = MuseumNumber("X", "2")
    annotations = Annotations(fragment_number)

    result = client.simulate_get(
        f"/fragments/{fragment_number}/annotations",
        params={"generateAnnotations": False},
    )

    expected_json = AnnotationsSchema().dump(annotations)
    assert result.status == falcon.HTTP_OK
    assert result.json == expected_json
 def find_by_sign(self, sign: str) -> Sequence[Annotations]:
     query = {"$regex": re.escape(sign), "$options": "i"}
     result = self._collection.aggregate([
         {
             "$match": {
                 "annotations.data.signName": query
             }
         },
         {
             "$project": {
                 "fragmentNumber": 1,
                 "annotations": {
                     "$filter": {
                         "input": "$annotations",
                         "as": "annotation",
                         "cond": {
                             "$eq": ["$$annotation.data.signName", sign]
                         },
                     }
                 },
             }
         },
     ])
     return AnnotationsSchema().load(result, many=True, unknown=EXCLUDE)
 def create_or_update(self, annotations: Annotations) -> None:
     self._collection.replace_one(
         AnnotationsSchema().dump(annotations),
         {"fragmentNumber": str(annotations.fragment_number)},
         True,
     )
def test_retrieve_all(database, annotations_repository):
    annotations = AnnotationsFactory.build_batch(5)
    database[COLLECTION].insert_many(AnnotationsSchema(many=True).dump(annotations))

    assert annotations_repository.retrieve_all_non_empty() == annotations
def test_dump():
    assert AnnotationsSchema().dump(ANNOTATIONS) == SERIALIZED
Beispiel #19
0
from ebl.ebl_ai_client import EblAiClient
from ebl.fragmentarium.application.annotations_schema import AnnotationsSchema
from ebl.fragmentarium.application.annotations_service import AnnotationsService
from ebl.fragmentarium.domain.annotation import Annotations
from ebl.transliteration.domain.museum_number import MuseumNumber
from ebl.tests.conftest import create_test_photo
from ebl.tests.factories.annotation import AnnotationsFactory

SCHEMA = AnnotationsSchema()


def test_generate_annotations(
    annotations_repository, photo_repository, changelog, when
):
    fragment_number = MuseumNumber.of("X.0")

    image_file = create_test_photo("K.2")

    when(photo_repository).query_by_file_name(f"{fragment_number}.jpg").thenReturn(
        image_file
    )
    ebl_ai_client = EblAiClient("mock-localhost:8001")
    service = AnnotationsService(
        ebl_ai_client, annotations_repository, photo_repository, changelog
    )

    expected = Annotations(fragment_number, tuple())
    when(ebl_ai_client).generate_annotations(fragment_number, image_file, 0).thenReturn(
        expected
    )
def test_load():
    assert AnnotationsSchema().load(SERIALIZED) == ANNOTATIONS