Skip to content

Commit 692a351

Browse files
committed
Uniform (de)serializing localized text attributes
1 parent 335afe3 commit 692a351

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

arho_feature_template/core/models.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ def from_feature(cls, feature: QgsFeature) -> RegulationConfig:
268268
return cls(
269269
id=feature["id"],
270270
regulation_code=feature["value"],
271-
name=feature["name"][LANGUAGE],
272-
description=feature["description"][LANGUAGE] if feature["description"] else "",
271+
name=feature["name"].get(LANGUAGE) if feature["name"] else None,
272+
description=feature["description"].get(LANGUAGE) if feature["description"] else None,
273273
status=feature["status"],
274274
level=feature["level"],
275275
parent_id=feature["parent_id"],

arho_feature_template/project/layers/code_layers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class AdditionalInformationTypeLayer(AbstractCodeLayer):
4848
@classmethod
4949
def get_additional_information_name(cls, info_type: str) -> str | None:
5050
attribute_value = cls.get_attribute_value_by_another_attribute_value("name", "value", info_type)
51-
return cast(str, attribute_value[LANGUAGE]) if attribute_value else None
51+
return cast(str, attribute_value.get(LANGUAGE)) if attribute_value else None
5252

5353

5454
class PlanRegulationGroupTypeLayer(AbstractCodeLayer):

arho_feature_template/project/layers/plan_layers.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def feature_from_model(cls, model: Plan) -> QgsFeature:
7979

8080
feature = cls.initialize_feature_from_model(model)
8181
feature.setGeometry(model.geom)
82-
feature["name"] = {LANGUAGE: model.name}
83-
feature["description"] = {LANGUAGE: model.description}
82+
feature["name"] = {LANGUAGE: model.name if model.name else None}
83+
feature["description"] = {LANGUAGE: model.description if model.description else None}
8484
feature["permanent_plan_identifier"] = model.permanent_plan_identifier
8585
feature["record_number"] = model.record_number
8686
feature["producers_plan_identifier"] = model.producers_plan_identifier
@@ -99,8 +99,8 @@ def model_from_feature(cls, feature: QgsFeature) -> Plan:
9999
]
100100
return Plan(
101101
geom=feature.geometry(),
102-
name=feature["name"][LANGUAGE],
103-
description=feature["description"][LANGUAGE],
102+
name=feature["name"].get(LANGUAGE) if feature["name"] else None,
103+
description=feature["description"].get(LANGUAGE) if feature["description"] else None,
104104
permanent_plan_identifier=feature["permanent_plan_identifier"],
105105
record_number=feature["record_number"],
106106
producers_plan_identifier=feature["producers_plan_identifier"],
@@ -123,7 +123,7 @@ def model_from_feature(cls, feature: QgsFeature) -> Plan:
123123
@classmethod
124124
def get_plan_name(cls, plan_id: str) -> str:
125125
attribute_value = cls.get_attribute_value_by_another_attribute_value("name", "id", plan_id)
126-
return attribute_value[LANGUAGE] if attribute_value else "Nimetön"
126+
return attribute_value.get(LANGUAGE, "Nimetön") if attribute_value else "Nimetön"
127127

128128

129129
class PlanFeatureLayer(AbstractPlanLayer):
@@ -135,9 +135,9 @@ def feature_from_model(cls, model: PlanFeature, plan_id: str | None = None) -> Q
135135

136136
feature = cls.initialize_feature_from_model(model)
137137
feature.setGeometry(model.geom)
138-
feature["name"] = {LANGUAGE: model.name if model.name else ""}
138+
feature["name"] = {LANGUAGE: model.name if model.name else None}
139139
feature["type_of_underground_id"] = model.type_of_underground_id
140-
feature["description"] = {LANGUAGE: model.description if model.description else ""}
140+
feature["description"] = {LANGUAGE: model.description if model.description else None}
141141
feature["plan_id"] = plan_id if plan_id else get_active_plan_id()
142142

143143
return feature
@@ -152,8 +152,8 @@ def model_from_feature(cls, feature: QgsFeature) -> PlanFeature:
152152
geom=feature.geometry(),
153153
type_of_underground_id=feature["type_of_underground_id"],
154154
layer_name=cls.get_from_project().name(),
155-
name=feature["name"][LANGUAGE],
156-
description=feature["description"][LANGUAGE],
155+
name=feature["name"].get(LANGUAGE) if feature["name"] else None,
156+
description=feature["description"].get(LANGUAGE),
157157
regulation_groups=[
158158
RegulationGroupLayer.model_from_feature(feat) for feat in regulation_group_features if feat is not None
159159
],
@@ -196,7 +196,7 @@ def feature_from_model(cls, model: RegulationGroup, plan_id: str | None = None)
196196
feature = cls.initialize_feature_from_model(model)
197197

198198
feature["short_name"] = model.short_name if model.short_name else None
199-
feature["name"] = {LANGUAGE: model.name}
199+
feature["name"] = {LANGUAGE: model.name if model.name else None}
200200
feature["type_of_plan_regulation_group_id"] = model.type_code_id
201201
feature["plan_id"] = (
202202
plan_id
@@ -210,7 +210,7 @@ def feature_from_model(cls, model: RegulationGroup, plan_id: str | None = None)
210210
def model_from_feature(cls, feature: QgsFeature) -> RegulationGroup:
211211
return RegulationGroup(
212212
type_code_id=feature["type_of_plan_regulation_group_id"],
213-
name=feature["name"][LANGUAGE],
213+
name=feature["name"].get(LANGUAGE) if feature["name"] else None,
214214
short_name=feature["short_name"],
215215
color_code=None,
216216
group_number=None,
@@ -335,11 +335,11 @@ def update_feature_from_attribute_value_model(value: AttributeValue | None, feat
335335
feature["numeric_range_min"] = value.numeric_range_min
336336
feature["numeric_range_max"] = value.numeric_range_max
337337
feature["unit"] = value.unit
338-
feature["text_value"] = {LANGUAGE: value.text_value} if value.text_value is not None else None
338+
feature["text_value"] = {LANGUAGE: value.text_value if value.text_value else None}
339339
feature["text_syntax"] = value.text_syntax
340340
feature["code_list"] = value.code_list
341341
feature["code_value"] = value.code_value
342-
feature["code_title"] = {LANGUAGE: value.code_title} if value.code_title is not None else None
342+
feature["code_title"] = {LANGUAGE: value.code_title if value.code_title else None}
343343
feature["height_reference_point"] = value.height_reference_point
344344

345345

@@ -435,7 +435,7 @@ class PlanPropositionLayer(AbstractPlanLayer):
435435
def feature_from_model(cls, model: Proposition) -> QgsFeature:
436436
feature = cls.initialize_feature_from_model(model)
437437

438-
feature["text_value"] = {LANGUAGE: model.value}
438+
feature["text_value"] = {LANGUAGE: model.value if model.value else None}
439439
feature["plan_regulation_group_id"] = model.regulation_group_id
440440
feature["ordering"] = model.proposition_number
441441
feature["plan_theme_id"] = model.theme_id
@@ -446,7 +446,7 @@ def feature_from_model(cls, model: Proposition) -> QgsFeature:
446446
@classmethod
447447
def model_from_feature(cls, feature: QgsFeature) -> Proposition:
448448
return Proposition(
449-
value=feature["text_value"][LANGUAGE],
449+
value=feature["text_value"].get(LANGUAGE) if feature["text_value"] else None,
450450
regulation_group_id=feature["plan_regulation_group_id"],
451451
proposition_number=feature["ordering"],
452452
theme_id=feature["plan_theme_id"],
@@ -477,7 +477,7 @@ class DocumentLayer(AbstractPlanLayer):
477477
def feature_from_model(cls, model: Document) -> QgsFeature:
478478
feature = cls.initialize_feature_from_model(model)
479479

480-
feature["name"] = {LANGUAGE: model.name}
480+
feature["name"] = {LANGUAGE: model.name if model.name else None}
481481
feature["url"] = model.url
482482
feature["type_of_document_id"] = model.type_of_document_id
483483
feature["decision"] = model.decision
@@ -496,7 +496,7 @@ def feature_from_model(cls, model: Document) -> QgsFeature:
496496
@classmethod
497497
def model_from_feature(cls, feature: QgsFeature) -> Document:
498498
return Document(
499-
name=feature["name"][LANGUAGE],
499+
name=feature["name"].get(LANGUAGE) if feature["name"] else None,
500500
url=feature["url"],
501501
type_of_document_id=feature["type_of_document_id"],
502502
decision=feature["decision"],

qgisprojekti.qgz

1.54 KB
Binary file not shown.

0 commit comments

Comments
 (0)