Skip to content

Commit d504a46

Browse files
committed
add deleting and required field checking for plan documents
1 parent 5b57fcc commit d504a46

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

arho_feature_template/core/plan_manager.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -498,27 +498,32 @@ def save_plan(plan: Plan) -> QgsFeature:
498498
edit_text="Kaavan muokkaus" if editing else "Kaavan luominen",
499499
)
500500

501-
# Check for deleted general regulations
501+
plan_id = feature["id"]
502502
if editing:
503+
# Check for deleted general regulations
503504
for association in RegulationGroupAssociationLayer.get_dangling_associations(
504-
plan.general_regulations, feature["id"], PlanLayer.name
505+
plan.general_regulations, plan_id, PlanLayer.name
505506
):
506507
_delete_feature(
507508
association,
508509
RegulationGroupAssociationLayer.get_from_project(),
509510
"Kaavamääräysryhmän assosiaation poisto",
510511
)
511512

513+
# Check for documents to be deleted
514+
doc_layer = DocumentLayer.get_from_project()
515+
for doc_feature in DocumentLayer.get_documents_to_delete(plan.documents, plan):
516+
_delete_feature(doc_feature, doc_layer, "Asiakirjan poisto")
517+
512518
# Save general regulations
513519
if plan.general_regulations:
514520
for regulation_group in plan.general_regulations:
515-
plan_id = feature["id"]
516521
regulation_group_feature = save_regulation_group(regulation_group, plan_id)
517522
save_regulation_group_association(regulation_group_feature["id"], PlanLayer.name, plan_id)
518523

519524
# Save documents
520525
for document in plan.documents:
521-
document.plan_id = feature["id"]
526+
document.plan_id = plan_id
522527
save_document(document)
523528

524529
return feature

arho_feature_template/gui/components/plan_document_widget.py

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
class DocumentWidget(QWidget, FormClass): # type: ignore
3434
"""A widget representation of a plan document."""
3535

36+
document_edited = pyqtSignal()
3637
delete_signal = pyqtSignal(QWidget)
3738

3839
def __init__(self, document: Document, parent=None):
@@ -73,6 +74,13 @@ def __init__(self, document: Document, parent=None):
7374
self.retention_time.populate_from_code_layer(RetentionTimeLayer)
7475
self.personal_data_content.populate_from_code_layer(PersonalDataContentLayer)
7576

77+
self.name.textChanged.connect(self.document_edited.emit)
78+
self.document_type.currentIndexChanged.connect(self.document_edited.emit)
79+
self.publicity.currentIndexChanged.connect(self.document_edited.emit)
80+
self.language.currentIndexChanged.connect(self.document_edited.emit)
81+
self.retention_time.currentIndexChanged.connect(self.document_edited.emit)
82+
self.personal_data_content.currentIndexChanged.connect(self.document_edited.emit)
83+
7684
# List of widgets for hiding / showing
7785
self.widgets: list[tuple[QLabel, QWidget]] = [
7886
(self.url_label, self.url),
@@ -113,6 +121,16 @@ def __init__(self, document: Document, parent=None):
113121
if document.confirmation_date:
114122
self._add_confirmation_date(document.confirmation_date)
115123

124+
def is_ok(self) -> bool:
125+
return (
126+
self.name.text() != ""
127+
and self.document_type.value() is not None
128+
and self.publicity.value() is not None
129+
and self.language.value() is not None
130+
and self.retention_time.value() is not None
131+
and self.personal_data_content.value() is not None
132+
)
133+
116134
def _add_widgets(self, label: QLabel, widget: QWidget):
117135
self.form_layout.addRow(label, widget)
118136
self.widgets.append((label, widget))

arho_feature_template/gui/dialogs/plan_attribute_form.py

+5
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def _check_required_fields(self) -> None:
122122
and self.plan_type_combo_box.value() is not None
123123
and self.organisation_combo_box.value() is not None
124124
and self.lifecycle_status_combo_box.value() is not None
125+
and all(document_widget.is_ok() for document_widget in self.document_widgets)
125126
):
126127
ok_button.setEnabled(True)
127128
else:
@@ -176,18 +177,22 @@ def init_plan_regulation_group_library(self, library: RegulationGroupLibrary):
176177

177178
def add_new_document(self):
178179
self.add_document(Document())
180+
self._check_required_fields()
179181

180182
def add_document(self, document: Document):
181183
widget = DocumentWidget(document, parent=self.documents_scroll_contents)
182184
widget.delete_signal.connect(self.delete_document)
185+
widget.document_edited.connect(self._check_required_fields)
183186
self.documents_layout.insertWidget(1, widget)
184187
self.document_widgets.append(widget)
185188

186189
def delete_document(self, document_widget: DocumentWidget):
187190
document_widget.delete_signal.disconnect()
191+
document_widget.document_edited.disconnect()
188192
self.documents_layout.removeWidget(document_widget)
189193
self.document_widgets.remove(document_widget)
190194
document_widget.deleteLater()
195+
self._check_required_fields()
191196

192197
# ---
193198

arho_feature_template/project/layers/plan_layers.py

+9
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,15 @@ def model_from_feature(cls, feature: QgsFeature) -> Document:
446446
id_=feature["id"],
447447
)
448448

449+
@classmethod
450+
def get_documents_to_delete(cls, documents: list[Document], plan: Plan) -> list[QgsFeature]:
451+
updated_document_ids = [doc.id_ for doc in documents]
452+
return [
453+
doc
454+
for doc in cls.get_features_by_attribute_value("plan_id", str(plan.id_))
455+
if doc["id"] not in updated_document_ids
456+
]
457+
449458

450459
class SourceDataLayer(AbstractPlanLayer):
451460
name = "Lähtötietoaineistot"

0 commit comments

Comments
 (0)