Skip to content

Commit

Permalink
Refactor Museum functionality (#498)
Browse files Browse the repository at this point in the history
* Refactor Museum

* Reformatted test_dtos.py file

* Fixed complexity and refactored fragment_schema.py and test_fragment.py
  • Loading branch information
kartikpaliwal authored Dec 15, 2023
1 parent 282b460 commit 2032685
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 16 deletions.
33 changes: 27 additions & 6 deletions ebl/fragmentarium/application/fragment_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from ebl.bibliography.application.reference_schema import ReferenceSchema
from ebl.common.application.schemas import AccessionSchema
from ebl.fragmentarium.domain.museum import Museum
from ebl.common.domain.period import Period, PeriodModifier
from ebl.fragmentarium.application.archaeology_schemas import ArchaeologySchema
from ebl.fragmentarium.application.genre_schema import GenreSchema
Expand Down Expand Up @@ -42,6 +43,17 @@ def filter_none(self, data, **kwargs):
return pydash.omit_by(data, pydash.is_none)


class MuseumSchema(Schema):
museumName = fields.String(required=True)
city = fields.String(required=True)
country = fields.String(required=True)
url = fields.Url(required=True)

@post_load
def make_museum(self, data, **kwargs) -> Museum:
return Museum(**data)


class RecordEntrySchema(Schema):
user = fields.String(required=True)
type = ValueEnumField(RecordType, required=True)
Expand Down Expand Up @@ -168,7 +180,7 @@ class FragmentSchema(Schema):
traditional_references = fields.List(
fields.String(), data_key="traditionalReferences"
)
museum = fields.String(required=True)
museum = fields.Nested(MuseumSchema, required=True)
width = fields.Nested(MeasureSchema, required=True)
length = fields.Nested(MeasureSchema, required=True)
thickness = fields.Nested(MeasureSchema, required=True)
Expand Down Expand Up @@ -211,19 +223,28 @@ class FragmentSchema(Schema):

@post_load
def make_fragment(self, data, **kwargs):
self._convert_lists_to_tuples(data)
self._ensure_museum_object(data)

if "authorized_scopes" in data:
data["authorized_scopes"] = list(data["authorized_scopes"])
if "dates_in_text" in data:
data["dates_in_text"] = list(data["dates_in_text"])

return Fragment(**data)

def _convert_lists_to_tuples(self, data):
data["references"] = tuple(data["references"])
data["genres"] = tuple(data["genres"])
data["line_to_vec"] = tuple(map(tuple, data["line_to_vec"]))
if "projects" in data:
data["projects"] = tuple(data["projects"])
if data["uncurated_references"] is not None:
data["uncurated_references"] = tuple(data["uncurated_references"])
if "authorized_scopes" in data:
data["authorized_scopes"] = list(data["authorized_scopes"])
if "dates_in_text" in data:
data["dates_in_text"] = list(data["dates_in_text"])

return Fragment(**data)
def _ensure_museum_object(self, data):
if "museum" in data and not isinstance(data["museum"], Museum):
data["museum"] = Museum(**data["museum"])

@post_dump
def filter_none(self, data, **kwargs):
Expand Down
5 changes: 3 additions & 2 deletions ebl/fragmentarium/domain/fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

import attr
import pydash

from ebl.bibliography.domain.reference import Reference
from ebl.common.domain.accession import Accession
from ebl.common.domain.period import Period, PeriodModifier
from ebl.common.domain.scopes import Scope
from ebl.fragmentarium.application.matches.create_line_to_vec import create_line_to_vec
from ebl.fragmentarium.domain.archaeology import Archaeology
from ebl.fragmentarium.domain.museum import Museum
from ebl.fragmentarium.domain.folios import Folios
from ebl.fragmentarium.domain.genres import genres
from ebl.fragmentarium.domain.joins import Joins
Expand Down Expand Up @@ -114,12 +114,13 @@ class ExternalNumbers:
@attr.s(auto_attribs=True, frozen=True)
class Fragment:
number: MuseumNumber
museum: Museum
accession: Optional[Accession] = None
publication: str = ""
description: str = ""
collection: str = ""
legacy_script: str = ""
museum: str = ""

width: Measure = Measure()
length: Measure = Measure()
thickness: Measure = Measure()
Expand Down
15 changes: 15 additions & 0 deletions ebl/fragmentarium/domain/museum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import attr


@attr.s
class Museum:
museumName: str = attr.ib()
city: str = attr.ib()
country: str = attr.ib()
url: str = attr.ib()

def __init__(self, museumName="", city="", country="", url=""):
self.museumName = museumName
self.city = city
self.country = country
self.url = url
17 changes: 16 additions & 1 deletion ebl/tests/corpus/test_mongo_text_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ebl.errors import DuplicateError, NotFoundError
from ebl.fragmentarium.application.joins_schema import JoinSchema
from ebl.fragmentarium.domain.fragment import Fragment
from ebl.fragmentarium.domain.museum import Museum
from ebl.fragmentarium.domain.joins import Join, Joins
from ebl.tests.factories.corpus import (
ChapterFactory,
Expand All @@ -36,6 +37,13 @@
from ebl.transliteration.domain.word_tokens import Word
from ebl.transliteration.application.signs_visitor import SignsVisitor

dummy_museum = Museum(
museumName="Dummy Museum",
city="Dummy City",
country="Dummy Country",
url="https://dummy-museum.org/",
)

TEXTS_COLLECTION = "texts"
CHAPTERS_COLLECTION = "chapters"
JOINS_COLLECTION = "joins"
Expand Down Expand Up @@ -203,7 +211,14 @@ def test_finding_text(
chapter = attr.evolve(CHAPTER, uncertain_fragments=(UNCERTAIN_FRAGMENT,))
when_text_in_collection(database, text)
when_chapter_in_collection(database, chapter)
fragment_repository.create(Fragment(UNCERTAIN_FRAGMENT))
dummy_museum = Museum(
museumName="Dummy Museum",
city="Dummy City",
country="Dummy Country",
url="https://dummy-museum.org/",
)
fragment_repository.create(Fragment(UNCERTAIN_FRAGMENT, museum=dummy_museum))

for reference in text.references:
bibliography_repository.create(reference.document)

Expand Down
14 changes: 12 additions & 2 deletions ebl/tests/factories/fragment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Sequence

from ebl.fragmentarium.domain.museum import Museum
import factory.fuzzy
import random
from ebl.common.domain.accession import Accession
Expand Down Expand Up @@ -176,13 +176,23 @@ class Meta:
)


class MuseumFactory(factory.Factory):
class Meta:
model = Museum

museumName = "The British Museum"
city = "London"
country = "GBR"
url = "https://www.britishmuseum.org/"


class FragmentFactory(factory.Factory):
class Meta:
model = Fragment

number = factory.Sequence(lambda n: MuseumNumber("X", str(n)))
accession = factory.Sequence(lambda n: Accession("A", str(n)))
museum = factory.Faker("word")
museum = factory.SubFactory(MuseumFactory)
collection = factory.Faker("word")
publication = factory.Faker("sentence")
description = factory.Faker("text")
Expand Down
7 changes: 6 additions & 1 deletion ebl/tests/fragmentarium/test_dtos.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,18 @@ def expected_dto(lemmatized_fragment, has_photo):
"collection": lemmatized_fragment.collection,
"legacyScript": lemmatized_fragment.legacy_script,
"traditionalReferences": lemmatized_fragment.traditional_references,
"museum": {
"museumName": lemmatized_fragment.museum.museumName,
"city": lemmatized_fragment.museum.city,
"country": lemmatized_fragment.museum.country,
"url": lemmatized_fragment.museum.url,
},
"script": ScriptSchema().dump(lemmatized_fragment.script),
"date": DateSchema().dump(lemmatized_fragment.date),
"datesInText": [
DateSchema().dump(date) for date in lemmatized_fragment.dates_in_text
],
"notes": NotesSchema().dump(lemmatized_fragment.notes),
"museum": lemmatized_fragment.museum,
"signs": lemmatized_fragment.signs,
"record": [
{"user": entry.user, "type": entry.type.value, "date": entry.date}
Expand Down
20 changes: 16 additions & 4 deletions ebl/tests/fragmentarium/test_fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from freezegun import freeze_time
import pytest
from ebl.common.domain.period import Period

from ebl.fragmentarium.domain.museum import Museum
from ebl.fragmentarium.domain.folios import Folio, Folios
from ebl.fragmentarium.domain.fragment import (
ExternalNumbers,
Expand Down Expand Up @@ -79,8 +79,14 @@ def test_script():


def test_museum():
fragment = FragmentFactory.build(museum="Museum")
assert fragment.museum == "Museum"
fragment = FragmentFactory.build()
expected_museum = Museum(
museumName="The British Museum",
city="London",
country="GBR",
url="https://www.britishmuseum.org/",
)
assert fragment.museum == expected_museum


def test_length():
Expand Down Expand Up @@ -115,7 +121,13 @@ def test_signs():

def test_record():
record = RecordFactory.build()
fragment = Fragment(MuseumNumber.of("X.1"), record=record)
museum = Museum(
museumName="Some Museum",
city="Some City",
country="Some Country",
url="https://some-museum.org/",
)
fragment = Fragment(MuseumNumber.of("X.1"), museum, record=record)
assert fragment.record == record


Expand Down

0 comments on commit 2032685

Please sign in to comment.