23
23
from arho_feature_template .exceptions import FeatureNotFoundError , LayerEditableError , LayerNotFoundError
24
24
from arho_feature_template .project .layers import AbstractLayer
25
25
from arho_feature_template .project .layers .code_layers import PlanRegulationTypeLayer
26
- from arho_feature_template .utils .misc_utils import LANGUAGE , get_active_plan_id , iface
26
+ from arho_feature_template .utils .misc_utils import (
27
+ deserialize_localized_text ,
28
+ get_active_plan_id ,
29
+ iface ,
30
+ serialize_localized_text ,
31
+ )
27
32
28
33
logger = logging .getLogger (__name__ )
29
34
@@ -78,8 +83,8 @@ def feature_from_model(cls, model: Plan) -> QgsFeature:
78
83
79
84
feature = cls .initialize_feature_from_model (model )
80
85
feature .setGeometry (model .geom )
81
- feature ["name" ] = { LANGUAGE : model .name if model . name else None }
82
- feature ["description" ] = { LANGUAGE : model .description if model . description else None }
86
+ feature ["name" ] = serialize_localized_text ( model .name )
87
+ feature ["description" ] = serialize_localized_text ( model .description )
83
88
feature ["permanent_plan_identifier" ] = model .permanent_plan_identifier
84
89
feature ["record_number" ] = model .record_number
85
90
feature ["producers_plan_identifier" ] = model .producers_plan_identifier
@@ -98,8 +103,8 @@ def model_from_feature(cls, feature: QgsFeature) -> Plan:
98
103
]
99
104
return Plan (
100
105
geom = feature .geometry (),
101
- name = feature [ "name" ]. get ( LANGUAGE ) if feature ["name" ] else None ,
102
- description = feature [ "description" ]. get ( LANGUAGE ) if feature ["description" ] else None ,
106
+ name = deserialize_localized_text ( feature ["name" ]) ,
107
+ description = deserialize_localized_text ( feature ["description" ]) ,
103
108
permanent_plan_identifier = feature ["permanent_plan_identifier" ],
104
109
record_number = feature ["record_number" ],
105
110
producers_plan_identifier = feature ["producers_plan_identifier" ],
@@ -122,7 +127,9 @@ def model_from_feature(cls, feature: QgsFeature) -> Plan:
122
127
@classmethod
123
128
def get_plan_name (cls , plan_id : str ) -> str :
124
129
attribute_value = cls .get_attribute_value_by_another_attribute_value ("name" , "id" , plan_id )
125
- return attribute_value .get (LANGUAGE , "Nimetön" ) if attribute_value else "Nimetön"
130
+ name = deserialize_localized_text (attribute_value )
131
+
132
+ return name or "Nimetön"
126
133
127
134
128
135
class PlanFeatureLayer (AbstractPlanLayer ):
@@ -134,9 +141,9 @@ def feature_from_model(cls, model: PlanFeature, plan_id: str | None = None) -> Q
134
141
135
142
feature = cls .initialize_feature_from_model (model )
136
143
feature .setGeometry (model .geom )
137
- feature ["name" ] = { LANGUAGE : model .name if model . name else None }
144
+ feature ["name" ] = serialize_localized_text ( model .name )
138
145
feature ["type_of_underground_id" ] = model .type_of_underground_id
139
- feature ["description" ] = { LANGUAGE : model .description if model . description else None }
146
+ feature ["description" ] = serialize_localized_text ( model .description )
140
147
feature ["plan_id" ] = plan_id if plan_id else get_active_plan_id ()
141
148
142
149
return feature
@@ -151,8 +158,8 @@ def model_from_feature(cls, feature: QgsFeature) -> PlanFeature:
151
158
geom = feature .geometry (),
152
159
type_of_underground_id = feature ["type_of_underground_id" ],
153
160
layer_name = cls .get_from_project ().name (),
154
- name = feature [ "name" ]. get ( LANGUAGE ) if feature ["name" ] else None ,
155
- description = feature ["description" ]. get ( LANGUAGE ),
161
+ name = deserialize_localized_text ( feature ["name" ]) ,
162
+ description = deserialize_localized_text ( feature ["description" ]),
156
163
regulation_groups = [
157
164
RegulationGroupLayer .model_from_feature (feat ) for feat in regulation_group_features if feat is not None
158
165
],
@@ -195,7 +202,7 @@ def feature_from_model(cls, model: RegulationGroup, plan_id: str | None = None)
195
202
feature = cls .initialize_feature_from_model (model )
196
203
197
204
feature ["short_name" ] = model .short_name if model .short_name else None
198
- feature ["name" ] = { LANGUAGE : model .name if model . name else None }
205
+ feature ["name" ] = serialize_localized_text ( model .name )
199
206
feature ["type_of_plan_regulation_group_id" ] = model .type_code_id
200
207
feature ["plan_id" ] = plan_id if plan_id else get_active_plan_id ()
201
208
feature ["id" ] = model .id_ if model .id_ else feature ["id" ]
@@ -205,7 +212,7 @@ def feature_from_model(cls, model: RegulationGroup, plan_id: str | None = None)
205
212
def model_from_feature (cls , feature : QgsFeature ) -> RegulationGroup :
206
213
return RegulationGroup (
207
214
type_code_id = feature ["type_of_plan_regulation_group_id" ],
208
- name = feature [ "name" ]. get ( LANGUAGE ) if feature ["name" ] else None ,
215
+ name = deserialize_localized_text ( feature ["name" ]) ,
209
216
short_name = feature ["short_name" ],
210
217
color_code = None ,
211
218
group_number = None ,
@@ -313,11 +320,11 @@ def attribute_value_model_from_feature(feature: QgsFeature) -> AttributeValue:
313
320
numeric_range_min = feature ["numeric_range_min" ],
314
321
numeric_range_max = feature ["numeric_range_max" ],
315
322
unit = feature ["unit" ],
316
- text_value = feature [ "text_value" ]. get ( LANGUAGE ) if feature ["text_value" ] else None ,
323
+ text_value = deserialize_localized_text ( feature ["text_value" ]) ,
317
324
text_syntax = feature ["text_syntax" ],
318
325
code_list = feature ["code_list" ],
319
326
code_value = feature ["code_value" ],
320
- code_title = feature [ "code_title" ]. get ( LANGUAGE ) if feature ["code_title" ] else None ,
327
+ code_title = deserialize_localized_text ( feature ["code_title" ]) ,
321
328
height_reference_point = feature ["height_reference_point" ],
322
329
)
323
330
@@ -330,11 +337,11 @@ def update_feature_from_attribute_value_model(value: AttributeValue | None, feat
330
337
feature ["numeric_range_min" ] = value .numeric_range_min
331
338
feature ["numeric_range_max" ] = value .numeric_range_max
332
339
feature ["unit" ] = value .unit
333
- feature ["text_value" ] = { LANGUAGE : value .text_value if value . text_value else None }
340
+ feature ["text_value" ] = serialize_localized_text ( value .text_value )
334
341
feature ["text_syntax" ] = value .text_syntax
335
342
feature ["code_list" ] = value .code_list
336
343
feature ["code_value" ] = value .code_value
337
- feature ["code_title" ] = { LANGUAGE : value .code_title if value . code_title else None }
344
+ feature ["code_title" ] = serialize_localized_text ( value .code_title )
338
345
feature ["height_reference_point" ] = value .height_reference_point
339
346
340
347
@@ -428,7 +435,7 @@ class PlanPropositionLayer(AbstractPlanLayer):
428
435
def feature_from_model (cls , model : Proposition ) -> QgsFeature :
429
436
feature = cls .initialize_feature_from_model (model )
430
437
431
- feature ["text_value" ] = { LANGUAGE : model .value if model . value else None }
438
+ feature ["text_value" ] = serialize_localized_text ( model .value )
432
439
feature ["plan_regulation_group_id" ] = model .regulation_group_id
433
440
feature ["ordering" ] = model .proposition_number
434
441
feature ["plan_theme_id" ] = model .theme_id
@@ -438,8 +445,12 @@ def feature_from_model(cls, model: Proposition) -> QgsFeature:
438
445
439
446
@classmethod
440
447
def model_from_feature (cls , feature : QgsFeature ) -> Proposition :
448
+ proposition_value = deserialize_localized_text (feature ["text_value" ])
449
+ if not proposition_value :
450
+ msg = "Proposition value cannot be empty."
451
+ raise ValueError (msg )
441
452
return Proposition (
442
- value = feature [ "text_value" ]. get ( LANGUAGE ) if feature [ "text_value" ] else None ,
453
+ value = proposition_value ,
443
454
regulation_group_id = feature ["plan_regulation_group_id" ],
444
455
proposition_number = feature ["ordering" ],
445
456
theme_id = feature ["plan_theme_id" ],
@@ -468,7 +479,7 @@ class DocumentLayer(AbstractPlanLayer):
468
479
def feature_from_model (cls , model : Document ) -> QgsFeature :
469
480
feature = cls .initialize_feature_from_model (model )
470
481
471
- feature ["name" ] = { LANGUAGE : model .name if model . name else None }
482
+ feature ["name" ] = serialize_localized_text ( model .name )
472
483
feature ["url" ] = model .url
473
484
feature ["type_of_document_id" ] = model .type_of_document_id
474
485
feature ["decision" ] = model .decision
@@ -487,7 +498,7 @@ def feature_from_model(cls, model: Document) -> QgsFeature:
487
498
@classmethod
488
499
def model_from_feature (cls , feature : QgsFeature ) -> Document :
489
500
return Document (
490
- name = feature [ "name" ]. get ( LANGUAGE ) if feature ["name" ] else None ,
501
+ name = deserialize_localized_text ( feature ["name" ]) ,
491
502
url = feature ["url" ],
492
503
type_of_document_id = feature ["type_of_document_id" ],
493
504
decision = feature ["decision" ],
0 commit comments