Skip to content

Commit fcc5d68

Browse files
committed
Work In progress
Work in progress saving lifecycle dates
1 parent a2cca41 commit fcc5d68

File tree

6 files changed

+338
-35
lines changed

6 files changed

+338
-35
lines changed

arho_feature_template/core/models.py

+21
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from arho_feature_template.utils.misc_utils import LANGUAGE, get_layer_by_name, iface
1515

1616
if TYPE_CHECKING:
17+
from datetime import datetime
18+
1719
from qgis.core import QgsFeature, QgsGeometry
1820

1921

@@ -321,6 +323,24 @@ def from_config_data(cls, data: dict) -> RegulationGroup:
321323
)
322324

323325

326+
@dataclass
327+
class LifeCycle:
328+
status_id: str | None
329+
id_: int | None = None
330+
plan_id: str | None = None
331+
# plan_id_: int | None = None
332+
plan_regulation_id: str | None = None
333+
plan_proposition_id: str | None = None
334+
starting_at: datetime | None = None
335+
ending_at: datetime | None = None
336+
# might not need the following
337+
land_use_are_id: str | None = None
338+
other_area_id: str | None = None
339+
line_id: str | None = None
340+
land_use_point_id: str | None = None
341+
other_point_id: str | None = None
342+
343+
324344
@dataclass
325345
class PlanFeature:
326346
geom: QgsGeometry | None = None # Need to allow None for feature templates
@@ -344,6 +364,7 @@ class Plan:
344364
description: str | None = None
345365
plan_type_id: str | None = None
346366
lifecycle_status_id: str | None = None
367+
# lifecycle: list[LifeCycle] = field(default_factory=list)
347368
record_number: str | None = None
348369
matter_management_identifier: str | None = None
349370
permanent_plan_identifier: str | None = None

arho_feature_template/core/plan_manager.py

+36
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from arho_feature_template.core.lambda_service import LambdaService
1212
from arho_feature_template.core.models import (
1313
FeatureTemplateLibrary,
14+
LifeCycle,
1415
Plan,
1516
PlanFeature,
1617
RegulationGroupCategory,
@@ -25,6 +26,7 @@
2526
from arho_feature_template.gui.docks.new_feature_dock import NewFeatureDock
2627
from arho_feature_template.gui.tools.inspect_plan_features_tool import InspectPlanFeatures
2728
from arho_feature_template.project.layers.code_layers import PlanRegulationGroupTypeLayer
29+
from arho_feature_template.project.layers.lifecycle_layers import LifeCycleLayer
2830
from arho_feature_template.project.layers.plan_layers import (
2931
LandUseAreaLayer,
3032
LandUsePointLayer,
@@ -182,6 +184,7 @@ def edit_plan(self):
182184
attribute_form = PlanAttributeForm(plan_model, self.get_regulation_group_libraries())
183185
if attribute_form.exec_():
184186
feature = save_plan(attribute_form.model)
187+
# lifecycle_features = save_lifecycle(attribute_form.lifecycle_model)
185188

186189
def add_new_plan_feature(self):
187190
if not handle_unsaved_changes():
@@ -213,6 +216,8 @@ def _plan_geom_digitized(self, feature: QgsFeature):
213216
attribute_form = PlanAttributeForm(plan_model, self.get_regulation_group_libraries())
214217
if attribute_form.exec_():
215218
feature = save_plan(attribute_form.model)
219+
lifecycle_features = save_lifecycle(attribute_form.lifecycle_model, feature["id"])
220+
# print(lifecycle_features)
216221
plan_to_be_activated = feature["id"]
217222
else:
218223
plan_to_be_activated = self.previous_active_plan_id
@@ -241,6 +246,7 @@ def _plan_feature_geom_digitized(self, feature: QgsFeature):
241246
)
242247
if attribute_form.exec_():
243248
save_plan_feature(attribute_form.model)
249+
# save_lifecycle(attribute_form.lifecycle_model)
244250

245251
def edit_plan_feature(self, feature: QgsFeature, layer_name: str):
246252
layer_class = FEATURE_LAYER_NAME_TO_CLASS_MAP[layer_name]
@@ -459,6 +465,19 @@ def save_plan(plan: Plan) -> QgsFeature:
459465
regulation_group_feature = save_regulation_group(regulation_group, plan_id)
460466
save_regulation_group_association(regulation_group_feature["id"], PlanLayer.name, plan_id)
461467

468+
# Save plan lifecycles
469+
# if hasattr(plan, "lifecycle"):
470+
# print("Has attribute lifecycle!")
471+
# if plan.lifecycle:
472+
# print("Plan has a lifecycle!")
473+
# for lifecycle in plan.lifecycle:
474+
# print(f"Lifecycle found: {lifecycle}!")
475+
# save_lifecycle(lifecycle)
476+
# else:
477+
# print("Plan doesnt have any lifecycles!")
478+
# else:
479+
# print("No attribute lifecycle!")
480+
462481
return feature
463482

464483

@@ -555,6 +574,23 @@ def save_regulation(regulation: Regulation) -> QgsFeature:
555574
return feature
556575

557576

577+
def save_lifecycle(lifecycles: list[LifeCycle]) -> list[QgsFeature]:
578+
"""Save a list of LifeCycle objects to the layer."""
579+
lifecycle_layer = LifeCycleLayer.get_from_project()
580+
if not lifecycle_layer:
581+
raise RuntimeError("Lifecycle layer not found in the project.")
582+
583+
saved_features = []
584+
for lifecycle in lifecycles:
585+
# Create or update a feature from the model
586+
feature = LifeCycleLayer.feature_from_model(lifecycle)
587+
lifecycle_layer.addFeature(feature)
588+
saved_features.append(feature)
589+
590+
lifecycle_layer.commitChanges()
591+
return saved_features
592+
593+
558594
def save_proposition(proposition: Proposition) -> QgsFeature:
559595
feature = PlanPropositionLayer.feature_from_model(proposition)
560596
layer = PlanPropositionLayer.get_from_project()

arho_feature_template/gui/dialogs/plan_attribute_form.py

+75-4
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
55

66
from qgis.PyQt import uic
77
from qgis.PyQt.QtCore import Qt
8+
from qgis.PyQt.QtGui import QStandardItem, QStandardItemModel
89
from qgis.PyQt.QtWidgets import (
910
QComboBox,
11+
QDateEdit,
1012
QDialog,
1113
QDialogButtonBox,
1214
QLineEdit,
15+
QListView,
16+
QPushButton,
1317
QSizePolicy,
1418
QSpacerItem,
1519
QTextEdit,
1620
QTreeWidgetItem,
1721
)
1822

19-
from arho_feature_template.core.models import Plan, RegulationGroup, RegulationGroupLibrary
23+
from arho_feature_template.core.models import LifeCycle, Plan, RegulationGroup, RegulationGroupLibrary
2024
from arho_feature_template.gui.components.plan_regulation_group_widget import RegulationGroupWidget
2125
from arho_feature_template.gui.components.tree_with_search_widget import TreeWithSearchWidget
2226
from arho_feature_template.project.layers.code_layers import (
@@ -40,7 +44,7 @@ class PlanAttributeForm(QDialog, FormClass): # type: ignore
4044
organisation_combo_box: CodeComboBox
4145
description_text_edit: QTextEdit
4246
plan_type_combo_box: HierarchicalCodeComboBox
43-
lifecycle_status_combo_box: CodeComboBox
47+
# lifecycle_status_combo_box: CodeComboBox
4448
record_number_line_edit: QLineEdit
4549
producers_plan_identifier_line_edit: QLineEdit
4650
matter_management_identifier_line_edit: QLineEdit
@@ -49,6 +53,12 @@ class PlanAttributeForm(QDialog, FormClass): # type: ignore
4953
plan_regulation_group_libraries_combobox: QComboBox
5054
regulation_groups_tree_layout: QVBoxLayout
5155

56+
lifecycle_status_combo_box: CodeComboBox
57+
lifecycle_start_date: QDateEdit
58+
lifecycle_end_date: QDateEdit
59+
add_lifecycle_button: QPushButton
60+
lifecycle_list: QListView
61+
5262
button_box: QDialogButtonBox
5363

5464
def __init__(self, plan: Plan, regulation_group_libraries: list[RegulationGroupLibrary], parent=None):
@@ -95,6 +105,10 @@ def __init__(self, plan: Plan, regulation_group_libraries: list[RegulationGroupL
95105
for regulation_group in plan.general_regulations:
96106
self.add_plan_regulation_group(regulation_group)
97107

108+
self.lifecycle_model = QStandardItemModel()
109+
self.lifecycle_list.setModel(self.lifecycle_model)
110+
self.add_lifecycle_button.clicked.connect(self.save_lifecycle)
111+
98112
self.button_box.button(QDialogButtonBox.Ok).setEnabled(False)
99113
self.button_box.accepted.connect(self._on_ok_clicked)
100114

@@ -106,7 +120,8 @@ def _check_required_fields(self) -> None:
106120
self.name_line_edit.text() != ""
107121
and self.plan_type_combo_box.value() is not None
108122
and self.organisation_combo_box.value() is not None
109-
and self.lifecycle_status_combo_box.value() is not None
123+
# and self.lifecycle_status_combo_box.value() is not None
124+
and self.lifecycle_model.rowCount() > 0
110125
):
111126
ok_button.setEnabled(True)
112127
else:
@@ -153,6 +168,60 @@ def init_plan_regulation_group_library(self, library: RegulationGroupLibrary):
153168

154169
# ---
155170

171+
def save_lifecycle(self):
172+
# Get values from the widgets
173+
status = self.lifecycle_status_combo_box.currentText() # Get the selected text from the combo box
174+
start_date = self.lifecycle_start_date.date().toString("yyyy-MM-dd") # Format the QDate as a string
175+
end_date = self.lifecycle_end_date.date().toString("yyyy-MM-dd") if self.lifecycle_end_date.date() else None
176+
177+
# Format the lifecycle entry
178+
date_range = f"{start_date} - {end_date}" if end_date else start_date
179+
lifecycle_entry = f"{status} | {date_range}"
180+
181+
# Add to the model
182+
item = QStandardItem(lifecycle_entry)
183+
self.lifecycle_model.appendRow(item)
184+
185+
# Optionally, check required fields again
186+
self._check_required_fields()
187+
188+
def into_lifecycle_model(self) -> list[LifeCycle]:
189+
print("Calling into_lifecycle_model at plan_attribute_form.py")
190+
lifecycles = []
191+
192+
for row in range(self.lifecycle_model.rowCount()):
193+
item = self.lifecycle_model.item(row)
194+
if item:
195+
lifecycle_entry = item.text()
196+
parts = lifecycle_entry.split(" | ")
197+
status = parts[0]
198+
date_range = parts[1]
199+
date_parts = date_range.split(" - ")
200+
start_date = date_parts[0]
201+
end_date = date_parts[1] if len(date_parts) > 1 else None # End date is optional
202+
203+
print(f"Got status: {status}")
204+
print(f"Got status_id: {LifeCycleStatusLayer.get_id_by_value(status)}")
205+
206+
# Add the lifecycle to the list
207+
lifecycles.append(
208+
LifeCycle(
209+
status_id=LifeCycleStatusLayer.get_id_by_value(status),
210+
plan_id="52aadc88-feb0-443b-a423-f5a50d0bb9fc",
211+
land_use_are_id=None,
212+
other_area_id=None,
213+
line_id=None,
214+
land_use_point_id=None,
215+
other_point_id=None,
216+
starting_at=start_date,
217+
ending_at=end_date,
218+
plan_regulation_id=None,
219+
plan_proposition_id=None,
220+
)
221+
)
222+
223+
return lifecycles
224+
156225
def into_model(self) -> Plan:
157226
return Plan(
158227
id_=self.plan.id_,
@@ -164,11 +233,13 @@ def into_model(self) -> Plan:
164233
record_number=self.record_number_line_edit.text() or None,
165234
producers_plan_identifier=self.producers_plan_identifier_line_edit.text() or None,
166235
matter_management_identifier=self.matter_management_identifier_line_edit.text() or None,
167-
lifecycle_status_id=self.lifecycle_status_combo_box.value(),
236+
lifecycle_status_id=self.lifecycle_status_combo_box.value(), # Need to get the lifecycle with the latest lifecycle
237+
# lifecycle=self.get_lifecycles(),
168238
general_regulations=[reg_group_widget.into_model() for reg_group_widget in self.regulation_group_widgets],
169239
geom=self.plan.geom,
170240
)
171241

172242
def _on_ok_clicked(self):
173243
self.model = self.into_model()
244+
self.lifecycle_model = self.into_lifecycle_model()
174245
self.accept()

0 commit comments

Comments
 (0)