From ea13aac5b877bc17d0581264663077c508b08e0f Mon Sep 17 00:00:00 2001 From: csae8092 Date: Fri, 1 Mar 2024 14:13:51 +0100 Subject: [PATCH 1/3] use the first of year/month --- apis_core/helper_functions/DateParser.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/apis_core/helper_functions/DateParser.py b/apis_core/helper_functions/DateParser.py index fc3a716..6af7313 100644 --- a/apis_core/helper_functions/DateParser.py +++ b/apis_core/helper_functions/DateParser.py @@ -1,6 +1,5 @@ -import math import re -from datetime import datetime, timedelta +from datetime import datetime def parse_date(date_string: str) -> (datetime, datetime, datetime): @@ -303,12 +302,7 @@ def parse_iso_date(date_string): if date_ab > date_bis: raise ValueError("'ab-date' must be before 'bis-date' in time") - # calculate difference between start and end date of range, - # and use it to calculate a single date for usage as median. - days_delta_half = math.floor( - (date_bis - date_ab).days / 2, - ) - date_single = date_ab + timedelta(days=days_delta_half) + date_single = date_ab elif date_ab is not None and date_bis is None: # date is only the start of a range, save it also as the single date From 5088dc2c146843131de3c6c7cbf6e6f7669c19d3 Mon Sep 17 00:00:00 2001 From: csae8092 Date: Sun, 3 Mar 2024 08:55:20 +0100 Subject: [PATCH 2/3] migrations, let's see how well this will work --- README.md | 10 +- ...ions_alter_institution_options_and_more.py | 52 ++++++++ ...ptions_alter_eventwork_options_and_more.py | 117 ++++++++++++++++++ 3 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 apis_core/apis_entities/migrations/0003_alter_event_options_alter_institution_options_and_more.py create mode 100644 apis_core/apis_relations/migrations/0003_alter_eventevent_options_alter_eventwork_options_and_more.py diff --git a/README.md b/README.md index 29c2d6e..5cef35b 100644 --- a/README.md +++ b/README.md @@ -25,4 +25,12 @@ in order to avoid errors on a new instance you'll need to set an environment var * bundling js-code (used for network-vis) is not part of the docker-setup * for development make sure the vite-dev-server is running `pnpm run vite` -* for building new bundle, run `pn run build` \ No newline at end of file +* for building new bundle, run `pn run build` + + +## reset sequence +```SQL +BEGIN; +SELECT setval(pg_get_serial_sequence('"django_migrations"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "django_migrations"; +COMMIT; +``` \ No newline at end of file diff --git a/apis_core/apis_entities/migrations/0003_alter_event_options_alter_institution_options_and_more.py b/apis_core/apis_entities/migrations/0003_alter_event_options_alter_institution_options_and_more.py new file mode 100644 index 0000000..e2e70e8 --- /dev/null +++ b/apis_core/apis_entities/migrations/0003_alter_event_options_alter_institution_options_and_more.py @@ -0,0 +1,52 @@ +# Generated by Django 5.0.1 on 2024-03-01 14:24 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("apis_entities", "0002_initial"), + ] + + operations = [ + migrations.AlterModelOptions( + name="event", + options={ + "ordering": ["id"], + "verbose_name": "Ereignis", + "verbose_name_plural": "Ereignisse", + }, + ), + migrations.AlterModelOptions( + name="institution", + options={ + "ordering": ["id"], + "verbose_name": "Institution", + "verbose_name_plural": "Institutionen", + }, + ), + migrations.AlterModelOptions( + name="person", + options={ + "ordering": ["id"], + "verbose_name": "Person", + "verbose_name_plural": "Personen", + }, + ), + migrations.AlterModelOptions( + name="place", + options={ + "ordering": ["id"], + "verbose_name": "Ort", + "verbose_name_plural": "Orte", + }, + ), + migrations.AlterModelOptions( + name="work", + options={ + "ordering": ["id"], + "verbose_name": "Werk", + "verbose_name_plural": "Werke", + }, + ), + ] diff --git a/apis_core/apis_relations/migrations/0003_alter_eventevent_options_alter_eventwork_options_and_more.py b/apis_core/apis_relations/migrations/0003_alter_eventevent_options_alter_eventwork_options_and_more.py new file mode 100644 index 0000000..a31cc98 --- /dev/null +++ b/apis_core/apis_relations/migrations/0003_alter_eventevent_options_alter_eventwork_options_and_more.py @@ -0,0 +1,117 @@ +# Generated by Django 5.0.1 on 2024-03-01 14:24 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("apis_relations", "0002_initial"), + ] + + operations = [ + migrations.AlterModelOptions( + name="eventevent", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="eventwork", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="institutionevent", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="institutioninstitution", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="institutionplace", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="institutionwork", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="personevent", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="personinstitution", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="personperson", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="personplace", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="personwork", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="placeevent", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="placeplace", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="placework", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + migrations.AlterModelOptions( + name="workwork", + options={ + "default_manager_name": "objects", + "ordering": ["start_date", "id"], + }, + ), + ] From 039f9d0444292d13d1ac421fddd7413377746f3a Mon Sep 17 00:00:00 2001 From: csae8092 Date: Sun, 3 Mar 2024 09:32:08 +0100 Subject: [PATCH 3/3] mgm funcion to save all; tests for date parser --- .../management/commands/save_all.py | 26 +++++++++++++++++++ apis_core/apis_entities/tests.py | 14 ++++++++++ 2 files changed, 40 insertions(+) create mode 100644 apis_core/apis_entities/management/commands/save_all.py diff --git a/apis_core/apis_entities/management/commands/save_all.py b/apis_core/apis_entities/management/commands/save_all.py new file mode 100644 index 0000000..de12bdf --- /dev/null +++ b/apis_core/apis_entities/management/commands/save_all.py @@ -0,0 +1,26 @@ +import os +import warnings + +from datetime import datetime +from tqdm import tqdm +from django.conf import settings +from django.core.management.base import BaseCommand + +from apis_core.apis_metainfo.models import TempEntityClass +from dumper.utils import write_report + +warnings.filterwarnings("ignore") + + +class Command(BaseCommand): + help = "saves all TempEntity objects" + + def handle(self, *args, **kwargs): + start_time = datetime.now().strftime(settings.PMB_TIME_PATTERN) + items = TempEntityClass.objects.all() + print(f"{items.count()} objects to save") + for x in tqdm(items, total=items.count()): + x.save() + end_time = datetime.now().strftime(settings.PMB_TIME_PATTERN) + report = [os.path.basename(__file__), start_time, end_time] + write_report(report) diff --git a/apis_core/apis_entities/tests.py b/apis_core/apis_entities/tests.py index fe795bb..2471031 100644 --- a/apis_core/apis_entities/tests.py +++ b/apis_core/apis_entities/tests.py @@ -7,6 +7,7 @@ from apis_core.apis_entities.forms import get_entities_form from apis_core.apis_entities.models import Person, Place +from apis_core.helper_functions.DateParser import parse_date from apis_core.apis_metainfo.models import Uri from normdata.forms import NormDataImportForm from normdata.utils import ( @@ -402,3 +403,16 @@ def test_028_beacons(self): url = reverse("apis_core:wikidata_beacon") r = client.get(url) self.assertEqual(r.status_code, 200) + + def test_029_parse_date(self): + dates = [ + ["1900", ["1900-01-01", "1900-01-01", "1900-12-31"]], + ["1800-02", ["1800-02-01", "1800-02-01", "1800-02-28"]], + ["1800-02-02", ["1800-02-02", "None", "None"]], + ["um 1900<1900-03-03>", ["1900-03-03", "None", "None"]], + ] + for x in dates: + results = parse_date(x[0]) + for i, r in enumerate(results): + date_str = f"{r}"[:10] + self.assertEqual(date_str, x[1][i])