From 8772a13163248ed65d44158e5957bf4cb2f311d8 Mon Sep 17 00:00:00 2001 From: scammo Date: Mon, 4 Oct 2021 18:10:31 +0200 Subject: [PATCH 1/5] availibility --- .pre-commit-config.yaml | 1 + ki/actions/seed.py | 22 ++++++++++++++---- ki/handlers/update_profile.py | 4 +++- ki/models.py | 10 ++++++-- ki/test/test_profile_endpoint.py | 9 +++++--- migrations/versions/459520b01f34_.py | 34 ++++++++++++++++++++++++++++ migrations/versions/99f6b0756445_.py | 24 ++++++++++++++++++++ 7 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 migrations/versions/459520b01f34_.py create mode 100644 migrations/versions/99f6b0756445_.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 38232bc..529438e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: AGPL-3.0-or-later +repos: - repo: local hooks: - id: flake8 diff --git a/ki/actions/seed.py b/ki/actions/seed.py index ff02112..fa3e819 100644 --- a/ki/actions/seed.py +++ b/ki/actions/seed.py @@ -26,7 +26,15 @@ def seed_contacttypes(): db.session.add(ContactType(id=int(contacttype["id"]), name=contacttype["name"])) -def seed_user(nickname, visible=False, skills=[], languages=[], volunteerwork="", availability="", freetext=""): +def seed_user(nickname, + visible=False, + skills=[], + languages=[], + volunteerwork="", + availabilityStatus=False, + freetext="", + availabilityText="", + availabilityHoursPerWeek=42): app.logger.info(f"seeding {nickname} \\o/") user = User(auth_id=nickname) @@ -35,7 +43,9 @@ def seed_user(nickname, visible=False, skills=[], languages=[], volunteerwork="" profile = Profile(nickname=nickname, pronouns="", volunteerwork=volunteerwork, - availability=availability, + availabilityStatus=availabilityStatus, + availabilityText=availabilityText, + availabilityHoursPerWeek=availabilityHoursPerWeek, freetext=freetext, visible=visible, user=user) @@ -91,7 +101,9 @@ def seed(dev: bool): peters_profile = Profile(nickname="peternichtlustig", pronouns="Herr Dr. Dr.", volunteerwork="Gartenverein", - availability="Immer", + availabilityStatus=True, + availabilityHoursPerWeek=42, + availabilityText="Immer", freetext="Ich mag Kaffee", user=peter) db.session.add(peters_profile) @@ -135,7 +147,9 @@ def seed(dev: bool): seed_user("dirtydieter", visible=True, volunteerwork="Müll sammeln", - availability="Nur nachts", + availabilityStatus=True, + availabilityHoursPerWeek=24, + availabilityText="Nur Nachts!", freetext="1001010010111!!!", skills=[(Skill.skill_id_php, 5)]) diff --git a/ki/handlers/update_profile.py b/ki/handlers/update_profile.py index ad70991..3722b94 100644 --- a/ki/handlers/update_profile.py +++ b/ki/handlers/update_profile.py @@ -135,7 +135,9 @@ def update_profile(user_id: int): profile.pronouns = request.json.get("pronouns", "") profile.volunteerwork = request.json.get("volunteerwork", "") - profile.availability = request.json.get("availability", "") + profile.availabilityStatus = request.json.get("availabilityStatus", False) + profile.availabilityText = request.json.get("availabilityText", "") + profile.availabilityHoursPerWeek = request.json.get("availabilityHoursPerWeek", 0) profile.freetext = request.json.get("freetext", "") profile.visible = request.json.get("visible", False) diff --git a/ki/models.py b/ki/models.py index af0ee5f..3206497 100644 --- a/ki/models.py +++ b/ki/models.py @@ -32,7 +32,11 @@ class Profile(db.Model): pronouns = Column(String(25), default="") volunteerwork = Column(String(4000), default="") freetext = Column(String(4000), default="") - availability = Column(String(4000), default="") + + availabilityStatus = Column(Boolean, default=False) + availabilityText = Column(String(4000), default="") + availabilityHoursPerWeek = Column(Integer, default=0) + visible = Column(Boolean, nullable=False, default=False) created = Column(DateTime, nullable=False, default=datetime.now) updated = Column(DateTime, onupdate=datetime.now, nullable=False, default=datetime.now) @@ -50,7 +54,9 @@ class Profile(db.Model): "nickname": self.nickname, "pronouns": self.pronouns, "volunteerwork": self.volunteerwork, - "availability": self.availability, + "availabilityStatus": self.availabilityStatus, + "availabilityText": self.availabilityText, + "availabilityHoursPerWeek": self.availabilityHoursPerWeek, "freetext": self.freetext, "visible": self.visible, "address": self.address.to_dict() if self.address else None, diff --git a/ki/test/test_profile_endpoint.py b/ki/test/test_profile_endpoint.py index febc6a0..698b5d3 100644 --- a/ki/test/test_profile_endpoint.py +++ b/ki/test/test_profile_endpoint.py @@ -33,7 +33,8 @@ class TestProfileEndpoint(ApiTest): data = { "pronouns": "Monsieur", "volunteerwork": "ja", - "availability": "Nie", + "availabilityStatus": False, + "availabilityText": "Nie", "freetext": "Hallo", "visible": True, "address": { @@ -110,7 +111,7 @@ class TestProfileEndpoint(ApiTest): profile = user.profile self.assertEqual("Monsieur", profile.pronouns) self.assertEqual("ja", profile.volunteerwork) - self.assertEqual("Nie", profile.availability) + self.assertEqual("Nie", profile.availabilityText) self.assertEqual("Hallo", profile.freetext) self.assertTrue(profile.visible) @@ -198,7 +199,9 @@ class TestProfileEndpoint(ApiTest): "user_id": 1, "nickname": "peternichtlustig", "pronouns": "Herr Dr. Dr.", - "availability": "Immer", + "availabilityStatus": True, + "availabilityHoursPerWeek": 25, + "availabilityText": "Immer", "freetext": "Ich mag Kaffee", "volunteerwork": "Gartenverein", "visible": False, diff --git a/migrations/versions/459520b01f34_.py b/migrations/versions/459520b01f34_.py new file mode 100644 index 0000000..bdd5e78 --- /dev/null +++ b/migrations/versions/459520b01f34_.py @@ -0,0 +1,34 @@ +"""empty message + +Revision ID: 459520b01f34 +Revises: 99f6b0756445 +Create Date: 2021-10-03 14:45:30.389359 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '459520b01f34' +down_revision = '99f6b0756445' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('profile', sa.Column('availabilityStatus', sa.Boolean(), nullable=True)) + op.add_column('profile', sa.Column('availabilityText', sa.String(length=4000), nullable=True)) + op.add_column('profile', sa.Column('availabilityHoursPerWeek', sa.Integer(), nullable=True)) + op.drop_column('profile', 'availability') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('profile', sa.Column('availability', sa.VARCHAR(length=4000), nullable=True)) + op.drop_column('profile', 'availabilityHoursPerWeek') + op.drop_column('profile', 'availabilityText') + op.drop_column('profile', 'availabilityStatus') + # ### end Alembic commands ### diff --git a/migrations/versions/99f6b0756445_.py b/migrations/versions/99f6b0756445_.py new file mode 100644 index 0000000..8544e8f --- /dev/null +++ b/migrations/versions/99f6b0756445_.py @@ -0,0 +1,24 @@ +"""empty message + +Revision ID: 99f6b0756445 +Revises: 9183e2335b05 +Create Date: 2021-10-03 14:45:10.980556 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '99f6b0756445' +down_revision = '9183e2335b05' +branch_labels = None +depends_on = None + + +def upgrade(): + pass + + +def downgrade(): + pass From cac14b4cfb1c82d222a209ab5c2ae75750b1abdf Mon Sep 17 00:00:00 2001 From: scammo Date: Mon, 4 Oct 2021 19:25:24 +0200 Subject: [PATCH 2/5] fix migrations issues, richtige variablen bennung --- ki/actions/seed.py | 24 ++++++++++++------------ ki/handlers/update_profile.py | 6 +++--- ki/models.py | 12 ++++++------ ki/test/test_profile_endpoint.py | 15 +++++++++------ migrations/versions/459520b01f34_.py | 19 ++++++++++--------- migrations/versions/99f6b0756445_.py | 24 ------------------------ 6 files changed, 40 insertions(+), 60 deletions(-) delete mode 100644 migrations/versions/99f6b0756445_.py diff --git a/ki/actions/seed.py b/ki/actions/seed.py index fa3e819..17fede2 100644 --- a/ki/actions/seed.py +++ b/ki/actions/seed.py @@ -31,10 +31,10 @@ def seed_user(nickname, skills=[], languages=[], volunteerwork="", - availabilityStatus=False, + availability_status=False, freetext="", - availabilityText="", - availabilityHoursPerWeek=42): + availability_text="", + availability_hours_per_week=42): app.logger.info(f"seeding {nickname} \\o/") user = User(auth_id=nickname) @@ -43,9 +43,9 @@ def seed_user(nickname, profile = Profile(nickname=nickname, pronouns="", volunteerwork=volunteerwork, - availabilityStatus=availabilityStatus, - availabilityText=availabilityText, - availabilityHoursPerWeek=availabilityHoursPerWeek, + availability_status=availability_status, + availability_text=availability_text, + availability_hours_per_week=availability_hours_per_week, freetext=freetext, visible=visible, user=user) @@ -101,9 +101,9 @@ def seed(dev: bool): peters_profile = Profile(nickname="peternichtlustig", pronouns="Herr Dr. Dr.", volunteerwork="Gartenverein", - availabilityStatus=True, - availabilityHoursPerWeek=42, - availabilityText="Immer", + availability_status=True, + availability_hours_per_week=42, + availability_text="Immer", freetext="Ich mag Kaffee", user=peter) db.session.add(peters_profile) @@ -147,9 +147,9 @@ def seed(dev: bool): seed_user("dirtydieter", visible=True, volunteerwork="Müll sammeln", - availabilityStatus=True, - availabilityHoursPerWeek=24, - availabilityText="Nur Nachts!", + availability_status=True, + availability_hours_per_week=24, + availability_text="Nur Nachts!", freetext="1001010010111!!!", skills=[(Skill.skill_id_php, 5)]) diff --git a/ki/handlers/update_profile.py b/ki/handlers/update_profile.py index 3722b94..762935b 100644 --- a/ki/handlers/update_profile.py +++ b/ki/handlers/update_profile.py @@ -135,9 +135,9 @@ def update_profile(user_id: int): profile.pronouns = request.json.get("pronouns", "") profile.volunteerwork = request.json.get("volunteerwork", "") - profile.availabilityStatus = request.json.get("availabilityStatus", False) - profile.availabilityText = request.json.get("availabilityText", "") - profile.availabilityHoursPerWeek = request.json.get("availabilityHoursPerWeek", 0) + profile.availability_status = request.json.get("availability_status", False) + profile.availability_text = request.json.get("availability_text", "") + profile.availability_hours_per_week = request.json.get("availability_hours_per_week", 0) profile.freetext = request.json.get("freetext", "") profile.visible = request.json.get("visible", False) diff --git a/ki/models.py b/ki/models.py index 3206497..1347e8e 100644 --- a/ki/models.py +++ b/ki/models.py @@ -33,9 +33,9 @@ class Profile(db.Model): volunteerwork = Column(String(4000), default="") freetext = Column(String(4000), default="") - availabilityStatus = Column(Boolean, default=False) - availabilityText = Column(String(4000), default="") - availabilityHoursPerWeek = Column(Integer, default=0) + availability_status = Column(Boolean, default=False) + availability_text = Column(String(4000), default="") + availability_hours_per_week = Column(Integer, default=0) visible = Column(Boolean, nullable=False, default=False) created = Column(DateTime, nullable=False, default=datetime.now) @@ -54,9 +54,9 @@ class Profile(db.Model): "nickname": self.nickname, "pronouns": self.pronouns, "volunteerwork": self.volunteerwork, - "availabilityStatus": self.availabilityStatus, - "availabilityText": self.availabilityText, - "availabilityHoursPerWeek": self.availabilityHoursPerWeek, + "availability_status": self.availability_status, + "availability_text": self.availability_text, + "availability_hours_per_week": self.availability_hours_per_week, "freetext": self.freetext, "visible": self.visible, "address": self.address.to_dict() if self.address else None, diff --git a/ki/test/test_profile_endpoint.py b/ki/test/test_profile_endpoint.py index 698b5d3..af51a5d 100644 --- a/ki/test/test_profile_endpoint.py +++ b/ki/test/test_profile_endpoint.py @@ -33,8 +33,9 @@ class TestProfileEndpoint(ApiTest): data = { "pronouns": "Monsieur", "volunteerwork": "ja", - "availabilityStatus": False, - "availabilityText": "Nie", + "availability_status": False, + "availability_text": "Nie", + "availability_hours_per_week": "23", "freetext": "Hallo", "visible": True, "address": { @@ -111,7 +112,9 @@ class TestProfileEndpoint(ApiTest): profile = user.profile self.assertEqual("Monsieur", profile.pronouns) self.assertEqual("ja", profile.volunteerwork) - self.assertEqual("Nie", profile.availabilityText) + self.assertEqual(False, profile.availability_status) + self.assertEqual("Nie", profile.availability_text) + self.assertEqual(23, profile.availability_hours_per_week) self.assertEqual("Hallo", profile.freetext) self.assertTrue(profile.visible) @@ -199,9 +202,9 @@ class TestProfileEndpoint(ApiTest): "user_id": 1, "nickname": "peternichtlustig", "pronouns": "Herr Dr. Dr.", - "availabilityStatus": True, - "availabilityHoursPerWeek": 25, - "availabilityText": "Immer", + "availability_status": True, + "availability_hours_per_week": 42, + "availability_text": "Immer", "freetext": "Ich mag Kaffee", "volunteerwork": "Gartenverein", "visible": False, diff --git a/migrations/versions/459520b01f34_.py b/migrations/versions/459520b01f34_.py index bdd5e78..75bf85b 100644 --- a/migrations/versions/459520b01f34_.py +++ b/migrations/versions/459520b01f34_.py @@ -11,24 +11,25 @@ import sqlalchemy as sa # revision identifiers, used by Alembic. revision = '459520b01f34' -down_revision = '99f6b0756445' +down_revision = '9183e2335b05' branch_labels = None depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### - op.add_column('profile', sa.Column('availabilityStatus', sa.Boolean(), nullable=True)) - op.add_column('profile', sa.Column('availabilityText', sa.String(length=4000), nullable=True)) - op.add_column('profile', sa.Column('availabilityHoursPerWeek', sa.Integer(), nullable=True)) - op.drop_column('profile', 'availability') + with op.batch_alter_table("profile") as batch_op: + batch_op.add_column(sa.Column('availability_status', sa.Boolean(), nullable=False)) + batch_op.add_column(sa.Column('availability_text', sa.String(length=4000), nullable=True)) + batch_op.add_column(sa.Column('availability_hours_per_week', sa.Integer(), nullable=True)) + batch_op.drop_column('availability') # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### - op.add_column('profile', sa.Column('availability', sa.VARCHAR(length=4000), nullable=True)) - op.drop_column('profile', 'availabilityHoursPerWeek') - op.drop_column('profile', 'availabilityText') - op.drop_column('profile', 'availabilityStatus') + with op.batch_alter_table("profile") as batch_op: + batch_op.drop_column('profile', 'availability_hours_per_week') + batch_op.drop_column('profile', 'availability_text') + batch_op.drop_column('profile', 'availability_status') # ### end Alembic commands ### diff --git a/migrations/versions/99f6b0756445_.py b/migrations/versions/99f6b0756445_.py deleted file mode 100644 index 8544e8f..0000000 --- a/migrations/versions/99f6b0756445_.py +++ /dev/null @@ -1,24 +0,0 @@ -"""empty message - -Revision ID: 99f6b0756445 -Revises: 9183e2335b05 -Create Date: 2021-10-03 14:45:10.980556 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '99f6b0756445' -down_revision = '9183e2335b05' -branch_labels = None -depends_on = None - - -def upgrade(): - pass - - -def downgrade(): - pass From 55b83f6efa77fd622a9ccc763a9da3aeaf18549b Mon Sep 17 00:00:00 2001 From: scammo Date: Thu, 7 Oct 2021 08:08:50 +0200 Subject: [PATCH 3/5] migration revision id fix --- migrations/versions/459520b01f34_.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations/versions/459520b01f34_.py b/migrations/versions/459520b01f34_.py index 75bf85b..b510dea 100644 --- a/migrations/versions/459520b01f34_.py +++ b/migrations/versions/459520b01f34_.py @@ -1,7 +1,7 @@ """empty message Revision ID: 459520b01f34 -Revises: 99f6b0756445 +Revises: 9183e2335b05 Create Date: 2021-10-03 14:45:30.389359 """ From 2387fb5e19abb35d8eeeb12e5edd6641ccfcbfa2 Mon Sep 17 00:00:00 2001 From: scammo Date: Mon, 11 Oct 2021 18:22:33 +0200 Subject: [PATCH 4/5] best migration and doku --- README.md | 2 +- migrations/versions/459520b01f34_.py | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c5d48b6..c3863c9 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ SPDX-License-Identifier: AGPL-3.0-or-later Ggf. vorher aufräumen ``` -rm data/ki.sqlite +rm storage/ki.sqlite ``` ``` diff --git a/migrations/versions/459520b01f34_.py b/migrations/versions/459520b01f34_.py index b510dea..23fabc6 100644 --- a/migrations/versions/459520b01f34_.py +++ b/migrations/versions/459520b01f34_.py @@ -19,17 +19,16 @@ depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### with op.batch_alter_table("profile") as batch_op: + batch_op.alter_column('availability', new_column_name='availability_text') batch_op.add_column(sa.Column('availability_status', sa.Boolean(), nullable=False)) - batch_op.add_column(sa.Column('availability_text', sa.String(length=4000), nullable=True)) batch_op.add_column(sa.Column('availability_hours_per_week', sa.Integer(), nullable=True)) - batch_op.drop_column('availability') # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### with op.batch_alter_table("profile") as batch_op: - batch_op.drop_column('profile', 'availability_hours_per_week') - batch_op.drop_column('profile', 'availability_text') - batch_op.drop_column('profile', 'availability_status') + batch_op.alter_column('availability_text', new_column_name='availability') + batch_op.drop_column('availability_hours_per_week') + batch_op.drop_column('availability_status') # ### end Alembic commands ### From 794eb7456c8aa6ee3dc1752ba9d3a2d7609395c3 Mon Sep 17 00:00:00 2001 From: scammo Date: Mon, 11 Oct 2021 18:42:20 +0200 Subject: [PATCH 5/5] server_default boolean --- migrations/versions/459520b01f34_.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/migrations/versions/459520b01f34_.py b/migrations/versions/459520b01f34_.py index 23fabc6..a4e78d1 100644 --- a/migrations/versions/459520b01f34_.py +++ b/migrations/versions/459520b01f34_.py @@ -7,6 +7,7 @@ Create Date: 2021-10-03 14:45:30.389359 """ from alembic import op import sqlalchemy as sa +from sqlalchemy.sql import expression # revision identifiers, used by Alembic. @@ -20,7 +21,7 @@ def upgrade(): # ### commands auto generated by Alembic - please adjust! ### with op.batch_alter_table("profile") as batch_op: batch_op.alter_column('availability', new_column_name='availability_text') - batch_op.add_column(sa.Column('availability_status', sa.Boolean(), nullable=False)) + batch_op.add_column(sa.Column('availability_status', sa.Boolean(), server_default=expression.true(), nullable=False)) batch_op.add_column(sa.Column('availability_hours_per_week', sa.Integer(), nullable=True)) # ### end Alembic commands ###