From b1c572803ba5295a1c211db8882ac6ae40b2280f Mon Sep 17 00:00:00 2001 From: hraban Date: Fri, 20 Feb 2026 15:25:20 +0100 Subject: [PATCH] =?UTF-8?q?Import=20f=C3=BCr=20authors.yml;=20Doku=20verbe?= =?UTF-8?q?ssert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 + docs/README.md | 11 +- liederquelle/quellen/management/__init__.py | 0 .../quellen/management/commands/__init__.py | 0 .../management/commands/import_authors.py | 103 ++++++++++++++++++ requirements.txt | 3 + 6 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 liederquelle/quellen/management/__init__.py create mode 100644 liederquelle/quellen/management/commands/__init__.py create mode 100644 liederquelle/quellen/management/commands/import_authors.py diff --git a/README.md b/README.md index 42f21ab..de8da62 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,8 @@ und für die öffentliche Rechercheplattform. - Django 6.0 - django-taggit - django-taggit-helpers (uralt, funktioniert aber) +- PyYAML + +# Installation + +Siehe [docs/README.md](src/branch/main/docs/README.md) diff --git a/docs/README.md b/docs/README.md index 5e2b89d..5e4ec54 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,7 +4,7 @@ Wir entwickeln auf Python 3.14. Python 3.12+ funktioniert, ältere haben zumindest Probleme mit Frescobaldi, was hiermit nichts zu tun hat… -- Python installieren von [https://www.python.org/downloads/] +- Python installieren von [python.org](https://www.python.org/downloads/) - `git clone ssh://git@git.notenbund.de:9922/Notenbund/liederquelle.git` - `cd liederquelle` - Python virtual environment (Venv) anlegen: `python3.14 -m venv .venv` @@ -22,7 +22,7 @@ Wir entwickeln auf Python 3.14. Python 3.12+ funktioniert, ältere haben zuminde - Datenbank (SQLite) anlegen: `python3 manage.py makemigrations quellen; python3 manage.py migrate` - Admin anlegen: `python3 manage.py createsuperuser` - Server starten: `python3 manage.py runserver` -- Öffne [http://localhost:8080/admin] im Browser +- Öffne http://localhost:8080/admin im Browser Statt `python3 manage.py` müsste auch der direkte Aufruf des Skripts funktionieren, also `./manage.py`. @@ -35,4 +35,9 @@ Statt `python3 manage.py` müsste auch der direkte Aufruf des Skripts funktionie - `cd liederquelle` - Datenbankstruktur aktualisieren: `python3 manage.py makemigrations quellen; python3 manage.py migrate` - Server starten: `python3 manage.py runserver` -- Öffne [http://localhost:8080/admin] im Browser +- Öffne http://localhost:8080/admin im Browser + +## Im- und Export + +- YAML exportieren: `./manage.py dumpdata --format yaml --indent 2 -o ../fixtures/fixture.yaml` +- `authors.yaml` importieren: `.\manage.py import_authors ` diff --git a/liederquelle/quellen/management/__init__.py b/liederquelle/quellen/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/liederquelle/quellen/management/commands/__init__.py b/liederquelle/quellen/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/liederquelle/quellen/management/commands/import_authors.py b/liederquelle/quellen/management/commands/import_authors.py new file mode 100644 index 0000000..37b4eb5 --- /dev/null +++ b/liederquelle/quellen/management/commands/import_authors.py @@ -0,0 +1,103 @@ +""" +Import für `authors.yml` aus `lilypond-song-includes` + +TODO: +- inherits (sollte 2. Identität für eine Person anlegen) +- details_secret +- aktualisieren statt neu anlegen +- Import direkt aus dem git +- Keine Lösung für trail_name_native_spelling (sehr selten) +""" + +import sys +import copy +from pathlib import Path +from django.core.management.base import BaseCommand, CommandError +from quellen.models import Person, Identity, AuthorID #, AuthorURL, Contact, Medium +from yaml import safe_load, dump + + +class Command(BaseCommand): + help = "Importiere `lilypond-song-includes/data/authors.yml`; anzugeben ist das Verzeichnis, in dem `lilypond-song-includes liegt`" + # TODO: Import aus dem git + default_filename = 'authors.yml' + default_subpath = 'lilypond-song-includes/data/' + default_author = { + 'name': '', + 'full_name': '', + 'name_native_spelling': '', + 'birth_name': '', + 'birth_year': '', + 'death_year': '', + 'comment': '', + 'trail_name': '', + 'organization': '', + } + + def add_arguments(self, parser): + parser.add_argument("source", nargs="?", type=str, help='Quelle der Importdatei') + + def handle(self, *args, **options): + authors = Path(options['source']).resolve() + if authors.is_dir: + a = Path(authors / self.default_filename) + self.stdout.write("Suche %s" % a) + if not a.is_file: + a = Path(authors / Path(self.default_subpath) / self.default_filename) + self.stdout.write("Suche %s" % a) + if not a.is_file: + self.stderr.write('Keine Autoren gefunden in "%s"' % authors) + sys.exit(1) + authors = a + with open(authors, mode='r') as inputfile: + data = safe_load(inputfile) + + for key in data: + self.stdout.write(key) + author = copy.copy(self.default_author) + author.update(data[key]) + if not author['name'] and author['trail_name']: + author['name'] = author['trail_name'] + p = Person( + name = author['name'], + full_name = author['full_name'], + name_native = author['name_native_spelling'], + birth_name = author['birth_name'], + details_secret = False, + birth_year = author['birth_year'], + death_year = author['death_year'], + remarks = author['comment'], + ) + p.save() + pi = Identity( + person = p, + author_id = key, + alias = author['trail_name'], + organization = author['organization'], + ) + pi.save() + for service in 'gnd isni orcid viaf wiki discogs lied brainz'.split(): + dbkey = service + if service=='wiki': + dbkey = 'wikidata' + if dbkey in author and author[dbkey]: + pai = AuthorID( + person = p, + key = AuthorID.IDs.__dict__[service.upper()], + value = author[dbkey], + ) + pai.save() + + + # for poll_id in options["poll_ids"]: + # try: + # poll = Poll.objects.get(pk=poll_id) + # except Poll.DoesNotExist: + # raise CommandError('Poll "%s" does not exist' % poll_id) + # + # poll.opened = False + # poll.save() + # + # self.stdout.write( + # self.style.SUCCESS('Successfully closed poll "%s"' % poll_id) + # ) diff --git a/requirements.txt b/requirements.txt index 17642b2..6805e70 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,6 @@ +pip +pyyaml + django>6,<7 django-dotenv django-taggit