def test_prepare_form_genre(self, entry_original): doc = ResourceDocument() resource = Resource.from_gsx_entry(entry_original) paratext = Resource.paratext_from_gsx_entry(entry_original, resource) assert len(doc.prepare_form_genre(resource)) == 0 resource.subjects.add(search_term_or_none("fast-forms", "History")) assert len(doc.prepare_form_genre(resource)) == 2 paratext.subjects.add(search_term_or_none("fast-forms", "Periodicals")) assert len(doc.prepare_form_genre(resource)) == 4
def unload_translator(apps, schema_editor): Contribution = apps.get_model("core", "Contribution") ControlledTerm = apps.get_model("controlled_vocabulary", "ControlledTerm") term = search_term_or_none("wikidata", "author") author = ControlledTerm.objects.get(id=term.id) term = search_term_or_none("wikidata", "translator") translator = ControlledTerm.objects.get(id=term.id) for c in Contribution.objects.filter(roles__label=translator.label): c.roles.add(author) c.roles.remove(translator) c.save()
def fast_topic_to_fast_forms(apps, schema_editor): ControlledTerm = apps.get_model("controlled_vocabulary", "ControlledTerm") Resource = apps.get_model("core", "Resource") term = search_term_or_none("fast-topic", "essays") essays_ft = ControlledTerm.objects.get(id=term.id) term = search_term_or_none("fast-forms", "essays") essays_ff = ControlledTerm.objects.get(id=term.id) for resource in Resource.objects.filter(subjects=essays_ft): resource.subjects.remove(essays_ft) resource.subjects.add(essays_ff) resource.save()
def load_translator(apps, schema_editor): Contribution = apps.get_model("core", "Contribution") ControlledTerm = apps.get_model("controlled_vocabulary", "ControlledTerm") term = search_term_or_none("wikidata", "author") author = ControlledTerm.objects.get(id=term.id) term = search_term_or_none("wikidata", "translator") translator = ControlledTerm.objects.get(id=term.id) for c in Contribution.objects.filter(roles__label=author.label): if (c.resource.relationships.filter( relationship_type__label="translation of").count() > 0): c.roles.add(translator) c.roles.remove(author) c.save()
def test_prepare_classifications_translation(self, entry_original): doc = ResourceDocument() resource = Resource.from_gsx_entry(entry_original) paratext = Resource.paratext_from_gsx_entry(entry_original, resource) assert len(doc.prepare_classifications_translation(resource)) == 0 resource.classifications.add( Classification(edition=search_term_or_none("rt-tt", "Integral")), bulk=False, ) assert len(doc.prepare_classifications_translation(resource)) == 2 paratext.classifications.add( Classification(edition=search_term_or_none("rt-tt", "Partial")), bulk=False, ) assert len(doc.prepare_classifications_translation(resource)) == 4
def test_prepare_subjects(self, entry_original): doc = ResourceDocument() resource = Resource.from_gsx_entry(entry_original) paratext = Resource.paratext_from_gsx_entry(entry_original, resource) assert len(doc.prepare_subjects(resource)) == 2 paratext.subjects.add(search_term_or_none("fast-topic", "Operas")) assert len(doc.prepare_subjects(resource)) == 4
def test_prepare_classifications_paratext(self, entry_original): doc = ResourceDocument() resource = Resource.from_gsx_entry(entry_original) paratext = Resource.paratext_from_gsx_entry(entry_original, resource) assert len(doc.prepare_classifications_paratext(resource)) == 0 paratext.classifications.add( Classification(edition=search_term_or_none("rt-pt", "Preface")), bulk=False, ) assert len(doc.prepare_classifications_paratext(resource)) == 3
def test_prepare_classifications_printing_publishing(self, entry_original): doc = ResourceDocument() resource = Resource.from_gsx_entry(entry_original) paratext = Resource.paratext_from_gsx_entry(entry_original, resource) assert len( doc.prepare_classifications_printing_publishing(resource)) == 0 resource.classifications.add( Classification(edition=search_term_or_none("rt-ppt", "Forgeries")), bulk=False, ) assert len( doc.prepare_classifications_printing_publishing(resource)) == 2 paratext.classifications.add( Classification(edition=search_term_or_none("rt-ppt", "Piracies")), bulk=False, ) assert len( doc.prepare_classifications_printing_publishing(resource)) == 4
def test_prepare_languages(self, entry_original): doc = ResourceDocument() resource = Resource.from_gsx_entry(entry_original) paratext = Resource.paratext_from_gsx_entry(entry_original, resource) assert len(doc.prepare_languages(resource)) == 2 paratext.languages.add( ResourceLanguage( language=search_term_or_none("iso639-2", "english")), bulk=False, ) assert len(doc.prepare_languages(resource)) == 4
def convert_paratext_terms(apps, schema_editor, cls): ControlledTerm = apps.get_model("controlled_vocabulary", "ControlledTerm") Classification = apps.get_model("core", "Classification") vocabulary = VocabularyParatextFunctions() for term in vocabulary._get_searchable_terms(): label = term[0] found = search_term_or_none(cls.prefix, label) if found: ct = ControlledTerm.objects.get(id=found.id) for classification in Classification.objects.filter( edition__label=label): classification.edition = ct classification.save()
def test_prepare_contributions(self, entry_original, person): doc = ResourceDocument() resource = Resource.from_gsx_entry(entry_original) paratext = Resource.paratext_from_gsx_entry(entry_original, resource) assert len(doc.prepare_contributions(resource)) == 3 person.name = "Anonymous Badger" person.save() contribution = Contribution(resource=resource, agent=person) contribution.save() contribution.roles.add(search_term_or_none("wikidata", "bookseller")) paratext.contributions.add(contribution, bulk=False) assert len(doc.prepare_contributions(resource)) == 4 contribution = doc.prepare_contributions(resource)[2] assert contribution["agent"]["name"] == "Anonymous"
def from_gsx_entry(entry: Dict[str, Dict[str, str]]) -> Optional["Organisation"]: """Gets or creates a new `Organisation` from a Google Spreadsheet dictionary `entry`.""" if not entry: return None name = get_gsx_entry_value(entry, "organisation") if not name: return None org, _ = Organisation.objects.get_or_create(name=name) value = get_gsx_entry_value(entry, "type") term = search_term_or_none("wikidata", value) if term: org.roles.add(term) value = get_gsx_entry_value(entry, "location") place = get_geonames_place_from_gsx_place(value) if place: org.based_near.add(place) org.save() return org
def from_gsx_entry(entry: Dict[str, Dict[str, str]]) -> Optional["Person"]: """Gets or creates a new `Person` from a Google Spreadsheet dictionary `entry`.""" if not entry: return None name = get_gsx_entry_value(entry, "name") if not name: return None person, _ = Person.objects.get_or_create(name=name) # person_field: gsx_field fields_mapping = { "given_name": "firstname", "family_name": "lastname", "gender": "gender", } for key in fields_mapping.keys(): value = get_gsx_entry_value(entry, fields_mapping[key]) if value: setattr(person, key, value) fields_mapping = { "date_birth": "birth", "date_death": "death", } for key in fields_mapping.keys(): value = Date.from_date_display( get_gsx_entry_value(entry, fields_mapping[key]) ) if value: setattr(person, key, value) fields_mapping = { "place_birth": "locationbirth", "place_death": "locationdeath", } for key in fields_mapping.keys(): place_names = get_gsx_entry_value(entry, fields_mapping[key]) if place_names: for name in place_names.split("; "): place = get_geonames_place_from_gsx_place(name) if place: setattr(person, key, place) place_names = get_gsx_entry_value(entry, "locationsresidence") if place_names: for name in place_names.split("; "): place = get_geonames_place_from_gsx_place(name) if place: person.based_near.add(place) occupations = get_gsx_entry_value(entry, "occupations") if occupations: for name in occupations.split("; "): term = search_term_or_none("wikidata", name) if term: person.roles.add(term) organisations = get_gsx_entry_value(entry, "organisations") if organisations: for name in organisations.split("; "): try: org = Organisation.objects.get(name=name) person.member_of.add(org) except Organisation.DoesNotExist: pass collaborators = get_gsx_entry_value(entry, "collaborators") if collaborators: for name in collaborators.split("; "): p, _ = Person.objects.get_or_create(name=name) person.knows.add(p) person.save() return person