Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Käyttötarkoituskohdistus ja Katja-asetus #345

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion database/codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@ class TypeOfAdditionalInformation(CodeBase):
"child_values": [
"paakayttotarkoitus",
"osaAlue",
"poisluettavaKayttotarkoitus",
"yhteystarve",
],
},
{
"value": "kayttotarkoituskohdistusTaiPoisluettavaKayttotarkoitus",
"name": {
"fin": "Käyttötarkoituskohdistus tai poisluettava käyttötarkoitus"
},
"child_values": ["kayttotarkoituskohdistus", "poisluettavaKayttotarkoitus"],
},
{
"value": "olemassaolo",
"name": {"fin": "Olemassaolo"},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""add separate fields for intended use allocations

Revision ID: 0d4bf02462e9
Revises: b8d3238d6b0a
Create Date: 2024-09-13 16:03:47.595496

"""
from typing import Sequence, Union

import geoalchemy2
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "0d4bf02462e9"
down_revision: Union[str, None] = "b8d3238d6b0a"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"plan_regulation",
sa.Column(
"intended_use_allocation_or_exclusion_id",
sa.UUID(as_uuid=False),
nullable=True,
),
schema="hame",
)
op.add_column(
"plan_regulation",
sa.Column(
"first_intended_use_allocation_id",
sa.UUID(as_uuid=False),
nullable=True,
),
schema="hame",
)
op.add_column(
"plan_regulation",
sa.Column(
"second_intended_use_allocation_id",
sa.UUID(as_uuid=False),
nullable=True,
),
schema="hame",
)
op.create_foreign_key(
"intended_use_allocation_or_exclusion_id_fkey",
"plan_regulation",
"type_of_additional_information",
["intended_use_allocation_or_exclusion_id"],
["id"],
source_schema="hame",
referent_schema="codes",
)
op.create_foreign_key(
"second_intended_use_allocation_id_fkey",
"plan_regulation",
"type_of_plan_regulation",
["second_intended_use_allocation_id"],
["id"],
source_schema="hame",
referent_schema="codes",
)
op.create_foreign_key(
"first_intended_use_allocation_id_fkey",
"plan_regulation",
"type_of_plan_regulation",
["first_intended_use_allocation_id"],
["id"],
source_schema="hame",
referent_schema="codes",
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(
"first_intended_use_allocation_id_fkey",
"plan_regulation",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"second_intended_use_allocation_id_fkey",
"plan_regulation",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"intended_use_allocation_or_exclusion_id_fkey",
"plan_regulation",
schema="hame",
type_="foreignkey",
)
op.drop_column(
"plan_regulation", "second_intended_use_allocation_id", schema="hame"
)
op.drop_column("plan_regulation", "first_intended_use_allocation_id", schema="hame")
op.drop_column(
"plan_regulation",
"intended_use_allocation_or_exclusion_id",
schema="hame",
)
# ### end Alembic commands ###
52 changes: 51 additions & 1 deletion database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ class PlanRegulation(PlanBase):
)
# Let's load all the codes for objects joined.
type_of_plan_regulation = relationship(
"TypeOfPlanRegulation", backref="plan_regulations", lazy="joined"
"TypeOfPlanRegulation",
foreign_keys=[type_of_plan_regulation_id],
backref="plan_regulations",
lazy="joined",
)
# Let's load all the codes for objects joined.
type_of_verbal_plan_regulation = relationship(
Expand All @@ -204,6 +207,33 @@ class PlanRegulation(PlanBase):
),
nullable=True,
)
# Käyttötarkoituskohdistus/poisluettava käyttötarkoitus
intended_use_allocation_or_exclusion_id: Mapped[uuid.UUID] = mapped_column(
ForeignKey(
"codes.type_of_additional_information.id",
name="intended_use_allocation_or_exclusion_id_fkey",
),
nullable=True,
)
# Käyttötarkoituskohdistuksen/poisluettavan käyttötarkoituksen kaavamääräyksen
# tyyppi
# https://koodistot.suomi.fi/code;registryCode=rytj;schemeCode=RY_Kaavamaarayksen_Lisatiedonlaji;codeCode=kayttotarkoituskohdistus
# https://koodistot.suomi.fi/code;registryCode=rytj;schemeCode=RY_Kaavamaarayksen_Lisatiedonlaji;codeCode=poisluettavaKayttotarkoitus
first_intended_use_allocation_id: Mapped[uuid.UUID] = mapped_column(
ForeignKey(
"codes.type_of_plan_regulation.id",
name="first_intended_use_allocation_id_fkey",
),
nullable=True,
)
second_intended_use_allocation_id: Mapped[uuid.UUID] = mapped_column(
ForeignKey(
"codes.type_of_plan_regulation.id",
name="second_intended_use_allocation_id_fkey",
),
nullable=True,
)

# Olemassaolo
existence_id: Mapped[uuid.UUID] = mapped_column(
ForeignKey(
Expand Down Expand Up @@ -267,6 +297,26 @@ class PlanRegulation(PlanBase):
backref="intended_use_plan_regulations",
lazy="joined",
)
# Let's load all the codes for objects joined.
intended_use_allocation_or_exclusion = relationship(
"TypeOfAdditionalInformation",
foreign_keys=[intended_use_allocation_or_exclusion_id],
backref="intended_use_allocation_plan_regulations",
lazy="joined",
)
first_intended_use_allocation = relationship(
"TypeOfPlanRegulation",
foreign_keys=[first_intended_use_allocation_id],
backref="first_intended_use_plan_regulations",
lazy="joined",
)
second_intended_use_allocation = relationship(
"TypeOfPlanRegulation",
foreign_keys=[second_intended_use_allocation_id],
backref="second_intended_use_plan_regulations",
lazy="joined",
)

existence = relationship(
"TypeOfAdditionalInformation",
foreign_keys=[existence_id],
Expand Down
34 changes: 30 additions & 4 deletions database/ryhti_client/ryhti_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
NameOfPlanCaseDecision,
TypeOfDecisionMaker,
TypeOfInteractionEvent,
TypeOfPlanRegulation,
TypeOfProcessingEvent,
decisionmaker_by_status,
decisions_by_status,
Expand Down Expand Up @@ -308,7 +309,7 @@ def get_plan_regulation(self, plan_regulation: models.PlanRegulation) -> Dict:
plan_regulation.type_of_verbal_plan_regulation.uri
]
regulation_dict["additionalInformations"] = []
for code_value in [
for additional_information in [
plan_regulation.intended_use,
plan_regulation.existence,
plan_regulation.regulation_type_additional_information,
Expand All @@ -318,18 +319,43 @@ def get_plan_regulation(self, plan_regulation: models.PlanRegulation) -> Dict:
plan_regulation.disturbance_prevention,
plan_regulation.construction_control,
]:
if code_value:
if additional_information:
regulation_dict["additionalInformations"].append(
{"type": code_value.uri}
{"type": additional_information.uri}
)
# Treat intended use allocation separately, it requires extra code values:
if plan_regulation.intended_use_allocation_or_exclusion:
for intended_use_value in (
plan_regulation.first_intended_use_allocation,
plan_regulation.second_intended_use_allocation,
):
# if intended_use_value is missing, we cannot just add empty
# additional information
if intended_use_value:
additional_information = {
"type": plan_regulation.intended_use_allocation_or_exclusion.uri
}
additional_information["value"] = dict()
additional_information["value"]["dataType"] = "code"
additional_information["value"]["code"] = intended_use_value.uri
additional_information["value"][
"codeList"
] = TypeOfPlanRegulation.code_list_uri
regulation_dict["additionalInformations"].append(
additional_information
)
if plan_regulation.numeric_value:
regulation_dict["value"] = {
"dataType": "decimal",
# we have to use simplejson because numbers are Decimal
"number": plan_regulation.numeric_value,
"unitOfMeasure": plan_regulation.unit,
}
elif plan_regulation.text_value:
elif (
plan_regulation.text_value.get("fin")
or plan_regulation.text_value.get("swe")
or plan_regulation.text_value.get("eng")
):
regulation_dict["value"] = {
"dataType": "LocalizedText",
"text": plan_regulation.text_value,
Expand Down
28 changes: 28 additions & 0 deletions database/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,25 @@ def verbal_plan_regulation_instance(
return instance


@pytest.fixture(scope="module")
def intended_use_plan_regulation_instance(
session,
preparation_status_instance,
type_of_plan_regulation_instance,
plan_regulation_group_instance,
):
instance = models.PlanRegulation(
name={"fin": "test_regulation"},
lifecycle_status=preparation_status_instance,
type_of_plan_regulation=type_of_plan_regulation_instance,
plan_regulation_group=plan_regulation_group_instance,
ordering=4,
)
session.add(instance)
session.commit()
return instance


@pytest.fixture(scope="module")
def general_plan_regulation_instance(
session,
Expand Down Expand Up @@ -988,10 +1007,12 @@ def complete_test_plan(
point_text_plan_regulation_instance: models.PlanRegulation,
numeric_plan_regulation_instance: models.PlanRegulation,
verbal_plan_regulation_instance: models.PlanRegulation,
intended_use_plan_regulation_instance: models.PlanRegulation,
general_plan_regulation_instance: models.PlanRegulation,
plan_proposition_instance: models.PlanProposition,
plan_theme_instance: codes.PlanTheme,
type_of_additional_information_instance: codes.TypeOfAdditionalInformation,
type_of_plan_regulation_instance: codes.TypeOfPlanRegulation,
participation_plan_presenting_for_public_decision: codes.NameOfPlanCaseDecision,
plan_material_presenting_for_public_decision: codes.NameOfPlanCaseDecision,
draft_plan_presenting_for_public_decision: codes.NameOfPlanCaseDecision,
Expand Down Expand Up @@ -1026,6 +1047,13 @@ def complete_test_plan(
verbal_plan_regulation_instance.intended_use = (
type_of_additional_information_instance
)
intended_use_plan_regulation_instance.plan_theme = plan_theme_instance
intended_use_plan_regulation_instance.intended_use_allocation_or_exclusion = (
type_of_additional_information_instance
)
intended_use_plan_regulation_instance.first_intended_use_allocation = (
type_of_plan_regulation_instance
)
general_plan_regulation_instance.plan_theme = plan_theme_instance
general_plan_regulation_instance.intended_use = (
type_of_additional_information_instance # noqa
Expand Down
8 changes: 4 additions & 4 deletions database/test/test_koodistot_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def koodistot_data(mock_koodistot, loader):
# data should also contain the local codes
assert len(data[codes.TypeOfPlanRegulationGroup]) == 5
# for mixed local and remote codes, the data should contain both
assert len(data[codes.TypeOfAdditionalInformation]) == 5
assert len(data[codes.TypeOfAdditionalInformation]) == 6
return data


Expand All @@ -422,7 +422,7 @@ def changed_koodistot_data(changed_mock_koodistot, loader):
# data should also contain the local codes
assert len(data[codes.TypeOfPlanRegulationGroup]) == 5
# for mixed local and remote codes, the data should contain both
assert len(data[codes.TypeOfAdditionalInformation]) == 5
assert len(data[codes.TypeOfAdditionalInformation]) == 6
return data


Expand Down Expand Up @@ -590,7 +590,7 @@ def assert_data_is_imported(main_db_params):
cur.execute(f"SELECT count(*) FROM codes.type_of_plan_regulation_group")
assert cur.fetchone()[0] == 5
cur.execute(f"SELECT count(*) FROM codes.type_of_additional_information")
assert cur.fetchone()[0] == 5
assert cur.fetchone()[0] == 6
check_code_parents(cur)
finally:
conn.close()
Expand All @@ -609,7 +609,7 @@ def assert_changed_data_is_imported(main_db_params):
cur.execute(f"SELECT count(*) FROM codes.type_of_plan_regulation_group")
assert cur.fetchone()[0] == 5
cur.execute(f"SELECT count(*) FROM codes.type_of_additional_information")
assert cur.fetchone()[0] == 5
assert cur.fetchone()[0] == 6
check_code_parents(cur)
finally:
conn.close()
Expand Down
Loading
Loading