Edit.py
Edit.py
import datetime
from pathlib import Path
from PyQt5.QtWidgets import QMessageBox
from Logger import setup_logger
from Word import WordHandler
from constants import WIRLogFilePath
from load_combobox_items_1 import ComboBoxLoader
class SearchEditing:
def __init__(self, main_window, ui):
self.main_window = main_window
self.ui = ui
self.logger = setup_logger()
self.file_path = Path(WIRLogFilePath)
self.combo_loader = ComboBoxLoader()
self.combo_loader.load_activities(self.ui.Activity_QCB2)
self.combo_loader.load_data(self.ui.Villa_QCB2, 0)
self.combo_loader.load_dates(self.ui.Inspection_Date_QCB2, 27)
def Search_WIRLog(self):
wir_no_to_find = self.ui.WIRNo_QLE2.text().strip()
rev_to_find = self.ui.Rev_QCB2.currentText().strip()
self.ui.WIRNo_QLE2.setEnabled(False)
self.ui.Rev_QCB2.setEnabled(False)
if not wir_no_to_find:
self._show_dialog("Error", "Please enter WIR No to search.",
QMessageBox.Warning)
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
return
try:
wir_no_int = int(wir_no_to_find)
except ValueError:
self._show_dialog("Error", "Invalid WIR No. Please enter a valid
number.", QMessageBox.Warning)
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
return
# Initialize variables
latest_revision = None
found_record = None
revisions = [] # Store all revisions for the WIR No
try:
with open(self.file_path, 'r', newline='') as f:
reader = csv.DictReader(f)
if current_wir_no == wir_no_to_find:
revisions.append(int(current_rev)) # Store revision
numbers as integers
# Determine the latest revision
if revisions:
latest_revision = str(max(revisions)) # Convert back to string for
UI
# If no revision was selected, notify the user and set the latest
revision
if not rev_to_find:
if latest_revision:
self._show_dialog(
"Information",
f"No revision selected. The latest revision for WIR No
{wir_no_to_find} is {latest_revision}.",
QMessageBox.Information
)
self.ui.Rev_QCB2.setCurrentText(latest_revision) # Auto-set
latest revision
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
return # **STOP HERE TO AVOID FALSE "NOT FOUND" MESSAGE**
except FileNotFoundError:
self._show_dialog("Error", "WIR Log file not found!",
QMessageBox.Critical)
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
except Exception as e:
self._show_dialog("Error", f"An unexpected error occurred during
search: {str(e)}", QMessageBox.Critical)
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
def Save_WIRLog(self):
wir_no_to_update = self.ui.WIRNo_QLE2.text().strip()
rev_to_update = self.ui.Rev_QCB2.currentText().strip()
activity_index = self.ui.Activity_QCB2.currentIndex()
villa_text = self.ui.Villa_QCB2.currentText().strip()
# Collect data from controls
updated_record = {
'WIR No': wir_no_to_update,
'Revision': rev_to_update,
'Activity': self.ui.Activity_QCB2.currentText().strip(),
'Sub Activity': self.ui.Sub_Activity_QCB2.currentText().strip(),
'Description': self.ui.Description_QTE2.toPlainText().strip(),
'Villa': f"Plot # {villa_text}" if activity_index < 5 else f"Villa #
{villa_text}",
'Level': self.ui.Level_QCB2.currentText().strip(),
'Part': self.ui.Part_QCB2.currentText().strip(),
'Created Date':datetime.date.today().strftime('%d %b %Y'),
'Inspection Date': self.ui.Inspection_Date_QCB2.currentText().strip(),
'Status': "Under Review",
}
print(f" villa # {'Villa'}")
# Remove empty fields
updated_record = {k: v for k, v in updated_record.items() if v}
if not fieldnames:
self._show_dialog("Error", "CSV file is missing headers!",
QMessageBox.Critical)
return
all_records = []
record_updated = False
if record_updated:
with open(self.file_path, 'w', newline='') as writeFile:
writer = csv.DictWriter(writeFile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(all_records)
self._show_dialog("Success", f"WIR No {wir_no_to_update} Rev
{rev_to_update} updated successfully!",
QMessageBox.Information)
self.ui.groupBox_2.setEnabled(False)
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
self._clear_ui_fields()
else:
self._show_dialog("Error", f"WIR No {wir_no_to_update} Rev
{rev_to_update} not found for update!",
QMessageBox.Warning)
except FileNotFoundError:
self._show_dialog("Error", "WIR Log file not found!",
QMessageBox.Critical)
except Exception as e:
self._show_dialog("Error", f"An unexpected error occurred during save:
{str(e)}", QMessageBox.Critical)
def Cancel_SearchWIR(self):
"""Resets the Search WIR tab to its initial state."""
self.ui.groupBox_2.setEnabled(False) # Disable the group box
self.ui.WIRNo_QLE2.setEnabled(True) # Re-enable WIR No input
self.ui.Rev_QCB2.setEnabled(True) # Re-enable Revision input
def _clear_ui_fields(self):
"""Clears the UI fields in Tab 3."""
self.ui.WIRNo_QLE2.clear() # Clear WIR No input field (optional)
self.ui.Rev_QCB2.setCurrentIndex(0) # Reset Revision Combobox to default
(index 0, which is "") (optional)
self.ui.Activity_QCB2.setCurrentIndex(-1) # or setCurrentText("") to clear
without triggering currentIndexChanged signal if needed
self.ui.Sub_Activity_QCB2.setCurrentIndex(-1)
self.ui.Description_QTE2.clear()
self.ui.Villa_QCB2.setCurrentIndex(-1)
self.ui.Level_QCB2.setCurrentIndex(-1)
self.ui.Part_QCB2.setCurrentIndex(-1)
self.ui.Inspection_Date_QCB2.setCurrentIndex(-1)