From 5a5f7dae9e5f42b28daafd0c87bac232b6dc5391 Mon Sep 17 00:00:00 2001 From: Gunther Weissenbaeck Date: Tue, 10 Oct 2023 12:05:23 +0200 Subject: [PATCH] =?UTF-8?q?=C3=BCbernahme=20rescuer=20to=20finder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/bird/forms.py | 4 +- .../0002_alter_fallenbird_comment.py | 18 +++++ app/rescuer_to_finder.py | 72 +++++++++++++++++++ bin/rescuer_to_finder | 8 +++ bin/scripts/rescuer_to_finder.py | 72 +++++++++++++++++++ 5 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 app/bird/migrations/0002_alter_fallenbird_comment.py create mode 100644 app/rescuer_to_finder.py create mode 100644 bin/rescuer_to_finder create mode 100644 bin/scripts/rescuer_to_finder.py diff --git a/app/bird/forms.py b/app/bird/forms.py index 84fc109..0b1ef9b 100644 --- a/app/bird/forms.py +++ b/app/bird/forms.py @@ -25,8 +25,8 @@ class BirdAddForm(forms.ModelForm): "place", "find_circumstances", "diagnostic_finding", - "comment", "finder", + "comment", ] labels = { "bird_identifier": _("Kennung"), @@ -57,6 +57,7 @@ class BirdEditForm(forms.ModelForm): "sent_to", "find_circumstances", "diagnostic_finding", + "finder", "comment", ] labels = { @@ -69,5 +70,6 @@ class BirdEditForm(forms.ModelForm): "sent_to": _("Übermittelt nach"), "find_circumstances": _("Fundumstände"), "diagnostic_finding": _("Diagnose bei Fund"), + "finder": _("Finder"), "comment": _("Bermerkung"), } diff --git a/app/bird/migrations/0002_alter_fallenbird_comment.py b/app/bird/migrations/0002_alter_fallenbird_comment.py new file mode 100644 index 0000000..ca64834 --- /dev/null +++ b/app/bird/migrations/0002_alter_fallenbird_comment.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.6 on 2023-10-10 06:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('bird', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='fallenbird', + name='comment', + field=models.TextField(blank=True, null=True, verbose_name='Bemerkung'), + ), + ] diff --git a/app/rescuer_to_finder.py b/app/rescuer_to_finder.py new file mode 100644 index 0000000..25b98e2 --- /dev/null +++ b/app/rescuer_to_finder.py @@ -0,0 +1,72 @@ +# assign environment variable DB_HOST to variable host +# assign environment variable DB_NAME to variable database +# assign environment variable DB_USER to variable db_user +# assign environment variable DB_PASSWORD to variable db_password +# assign environment variable DB_PORT to variable db_port + + +# establish connection to database +import os +import psycopg2 +import psycopg2.extras +import psycopg2.extensions + +host = os.environ['DB_HOST'] +database = os.environ['DB_NAME'] +user = os.environ['DB_USER'] +password = os.environ['DB_PASSWORD'] +port = os.environ['DB_PORT'] + + +conn = psycopg2.connect(host=host, database=database, user=user, password=password, port=port) +conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) +cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) + +# check if column finder exists in table bird_fallenbird +cur.execute("SELECT column_name FROM information_schema.columns WHERE table_name = 'bird_fallenbird' AND column_name = 'finder'") + +# if column finder does not exist in table bird_fallenbird, add column finder to table bird_fallenbird +if not cur.fetchone(): + cur.execute("alter table bird_fallenbird add column finder text") + +# query the table rescuer_rescuer and write the results to a variable +cur.execute("SELECT * FROM rescuer_rescuer") +rescuer_rescuer = cur.fetchall() + +# query the table bird_fallenbird with its columns id and rescuer_id and write the results to a variable +cur.execute("SELECT id, rescuer_id FROM bird_fallenbird") +bird_fallenbird = cur.fetchall() + +# iterate over the results of the query bird_fallenbird +# for each row, iterate over the results of the query rescuer_rescuer +# if the rescuer_id of the bird_fallenbird row matches the id of the rescuer_rescuer row, update the bird_fallenbird row finder with all the data of the rescuer_rescuer +# all rows of rescuer_rescuer should be inserted into the column finder of bird_fallenbird with the same rescuer_id +for row in bird_fallenbird: + for rescuer in rescuer_rescuer: + if row['rescuer_id'] == rescuer['id']: + # the data should be in the format column_name: column_value + finder = '' + for key, value in rescuer.items(): + # exclude the id column + if key != 'id': + if key != 'user_id': + if value != '-': + if key == 'first_name': + key = 'Vorname' + elif key == 'last_name': + key = 'Nachname' + elif key == 'phone': + key = 'Telefonnummer' + elif key == 'street': + key = 'Straße' + elif key == 'street_number': + key = 'Hausnummer' + elif key == 'city': + key = 'Stadt' + elif key == 'zip_code': + key = 'PLZ' + finder += key + ': ' + str(value) + '\n' + finder = finder[:-2] + cur.execute("UPDATE bird_fallenbird SET finder = %s WHERE id = %s", (finder, row['id'])) + + diff --git a/bin/rescuer_to_finder b/bin/rescuer_to_finder new file mode 100644 index 0000000..c5dd7c5 --- /dev/null +++ b/bin/rescuer_to_finder @@ -0,0 +1,8 @@ +#!/bin/bash +echo "Copying rescuer_to_finder.py to django_fbf_web_1" +docker cp ./scripts/rescuer_to_finder.py django_fbf_web_1:/app/rescuer_to_finder.py + +echo "Running rescuer_to_finder.py" +docker exec django_fbf_web_1 python3 /app/rescuer_to_finder.py + +echo "Done" \ No newline at end of file diff --git a/bin/scripts/rescuer_to_finder.py b/bin/scripts/rescuer_to_finder.py new file mode 100644 index 0000000..25b98e2 --- /dev/null +++ b/bin/scripts/rescuer_to_finder.py @@ -0,0 +1,72 @@ +# assign environment variable DB_HOST to variable host +# assign environment variable DB_NAME to variable database +# assign environment variable DB_USER to variable db_user +# assign environment variable DB_PASSWORD to variable db_password +# assign environment variable DB_PORT to variable db_port + + +# establish connection to database +import os +import psycopg2 +import psycopg2.extras +import psycopg2.extensions + +host = os.environ['DB_HOST'] +database = os.environ['DB_NAME'] +user = os.environ['DB_USER'] +password = os.environ['DB_PASSWORD'] +port = os.environ['DB_PORT'] + + +conn = psycopg2.connect(host=host, database=database, user=user, password=password, port=port) +conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) +cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) + +# check if column finder exists in table bird_fallenbird +cur.execute("SELECT column_name FROM information_schema.columns WHERE table_name = 'bird_fallenbird' AND column_name = 'finder'") + +# if column finder does not exist in table bird_fallenbird, add column finder to table bird_fallenbird +if not cur.fetchone(): + cur.execute("alter table bird_fallenbird add column finder text") + +# query the table rescuer_rescuer and write the results to a variable +cur.execute("SELECT * FROM rescuer_rescuer") +rescuer_rescuer = cur.fetchall() + +# query the table bird_fallenbird with its columns id and rescuer_id and write the results to a variable +cur.execute("SELECT id, rescuer_id FROM bird_fallenbird") +bird_fallenbird = cur.fetchall() + +# iterate over the results of the query bird_fallenbird +# for each row, iterate over the results of the query rescuer_rescuer +# if the rescuer_id of the bird_fallenbird row matches the id of the rescuer_rescuer row, update the bird_fallenbird row finder with all the data of the rescuer_rescuer +# all rows of rescuer_rescuer should be inserted into the column finder of bird_fallenbird with the same rescuer_id +for row in bird_fallenbird: + for rescuer in rescuer_rescuer: + if row['rescuer_id'] == rescuer['id']: + # the data should be in the format column_name: column_value + finder = '' + for key, value in rescuer.items(): + # exclude the id column + if key != 'id': + if key != 'user_id': + if value != '-': + if key == 'first_name': + key = 'Vorname' + elif key == 'last_name': + key = 'Nachname' + elif key == 'phone': + key = 'Telefonnummer' + elif key == 'street': + key = 'Straße' + elif key == 'street_number': + key = 'Hausnummer' + elif key == 'city': + key = 'Stadt' + elif key == 'zip_code': + key = 'PLZ' + finder += key + ': ' + str(value) + '\n' + finder = finder[:-2] + cur.execute("UPDATE bird_fallenbird SET finder = %s WHERE id = %s", (finder, row['id'])) + +