From 66bd9c6eec53af4d9532c47255fd0ee8d62c4df8 Mon Sep 17 00:00:00 2001 From: Ryan Heuser Date: Wed, 17 Jan 2024 16:37:05 -0500 Subject: [PATCH] working now with shared set of cols --- mep/people/admin.py | 37 ++++++++++++++++++++++++++++++++----- mep/people/models.py | 13 ++++++++++++- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/mep/people/admin.py b/mep/people/admin.py index b44e48a7..2adb8073 100644 --- a/mep/people/admin.py +++ b/mep/people/admin.py @@ -25,6 +25,7 @@ Profession, Relationship, RelationshipType, + ViafSignalHandler ) from import_export.admin import ( ImportExportModelAdmin, @@ -34,6 +35,30 @@ from import_export.fields import Field from parasolr.django.signals import IndexableSignalHandler +PERSON_IMPORT_EXPORT_COLUMNS = ( + 'id', + 'name', + 'birth_year', + 'death_year', + 'gender', + 'nationalities', + 'notes', + 'start_year', + 'end_year', + 'mep_id', + 'sort_name', + 'viaf_id', + 'is_organization', + 'verified', + 'title', + 'profession', + 'relations', + 'public_notes', + 'locations', + 'slug', + 'updated_at', +) + class InfoURLInline(CollapsibleTabularInline): model = InfoURL @@ -449,11 +474,13 @@ def before_import(self, dataset, *args, **kwargs): dataset.headers = [x.lower().replace(' ','_') for x in dataset.headers] # turn off indexing temporarily IndexableSignalHandler.disconnect() + ViafSignalHandler.disconnect() def after_import(self, *args, **kwargs): super().after_import(*args, **kwargs) # reconnect indexing signal handler IndexableSignalHandler.connect() + ViafSignalHandler.connect() def before_import_row(self, row, **kwargs): # gender to one char @@ -465,26 +492,26 @@ def fmt_gender(x): name = Field(column_name='name', attribute='name') gender = Field(column_name='gender', attribute='gender') nationalities = Field( - column_name='nationality', + column_name='nationalities', attribute='nationalities', widget=ManyToManyWidget(Country, field='name', separator=';') ) class Meta: model = Person - fields = ('name','gender','nationalities') - import_id_fields = ('name',) + import_id_fields = ('slug',) + export_order = PERSON_IMPORT_EXPORT_COLUMNS skip_unchanged = True report_skipped = True -class PersonAdminWithImport(PersonAdmin, ImportExportModelAdmin): +class PersonAdminImportExport(PersonAdmin, ImportExportModelAdmin): resource_class = PersonResource change_list_template = "templates/admin/people/person/change_list.html" # enable default admin to see imported data -admin.site.register(Person, PersonAdminWithImport) +admin.site.register(Person, PersonAdminImportExport) admin.site.register(Country, CountryAdmin) admin.site.register(Location, LocationAdmin) admin.site.register(Profession, NamedNotableAdmin) diff --git a/mep/people/models.py b/mep/people/models.py index fc39f14e..3ca6a685 100644 --- a/mep/people/models.py +++ b/mep/people/models.py @@ -26,6 +26,17 @@ logger = logging.getLogger(__name__) +VIAF_SIGNAL_ACTIVE=True +class ViafSignalHandler: + def disconnect(): + global VIAF_SIGNAL_ACTIVE + VIAF_SIGNAL_ACTIVE = False + def connect(): + global VIAF_SIGNAL_ACTIVE + VIAF_SIGNAL_ACTIVE = True + def is_connected(): + return VIAF_SIGNAL_ACTIVE + class Country(Named): """Countries, for documenting nationalities of a :class:`Person` @@ -533,7 +544,7 @@ def save(self, *args, **kwargs): """Adds birth and death dates if they aren't already set and there's a viaf id for the record""" - if self.viaf_id and not self.birth_year and not self.death_year: + if ViafSignalHandler.is_connected() and self.viaf_id and not self.birth_year and not self.death_year: self.set_birth_death_years() # if slug has changed, save the old one as a past slug