RevStatus.py
RevStatus.py
import datetime
from pathlib import Path
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMessageBox, QTableWidgetItem
from Logger import setup_logger
from constants import WIRLogFilePath
from Word import WordHandler
class SearchRevStatus:
def __init__(self, main_window, ui):
self.main_window = main_window
if not ui or type(ui) == bool:
print("Error: UI object is invalid.")
return
self.ui = ui
self.logger = setup_logger()
self.file_path = Path(WIRLogFilePath)
def Search_WIRLog(self):
wir_no_to_find = self.ui.WIRNo_QLE1.text().strip()
rev_to_find = self.ui.Rev_QCB1.currentText().strip() or '0'
if not wir_no_to_find:
self._show_dialog("Error", "Please enter WIR No to search.",
QMessageBox.Warning)
return
try:
int(wir_no_to_find) # Validate WIR number as integer
except ValueError:
self._show_dialog("Error", "Invalid WIR No. Please enter a valid
number.", QMessageBox.Warning)
return
try:
found_records = [row for row in csv.DictReader(open(self.file_path,
'r', newline=''))
if row['WIR No'].strip() == wir_no_to_find]
if found_records:
self._populate_table_view(found_records)
else:
self._show_dialog("Information", f"WIR No {wir_no_to_find} not
found.", QMessageBox.Information)
except FileNotFoundError:
self._show_dialog("Error", "WIR Log file not found!",
QMessageBox.Critical)
except Exception as e:
self._show_dialog("Error", f"Unexpected error: {str(e)}",
QMessageBox.Critical)
items = [
f"{record.get('WIR No', '').strip()} - {record.get('Revision',
'').strip()}",
record.get('Sub Activity', '').strip(),
f"P.{record.get('Villa No', 'N/A').strip()}- {record.get('Level',
'N/A').strip()}- {record.get('Part', 'N/A').strip()}",
record.get('Status', '').strip(),
record.get('Inspection Date', '').strip(),
record.get('Description', '').strip()
def create_revision(self):
wir_no_to_find = self.ui.WIRNo_QLE1.text().strip()
rev_to_find = self.ui.Rev_QCB2.currentText().strip()
if not wir_no_to_find:
self._show_dialog("Error", "Please enter WIR No to create revision.",
QMessageBox.Warning)
return
try:
all_records = list(csv.DictReader(open(self.file_path, 'r',
newline='')))
found_records = [row for row in all_records if row['WIR No'].strip() ==
wir_no_to_find]
if not found_records:
self._show_dialog("Error", f"WIR No {wir_no_to_find} not found.",
QMessageBox.Warning)
return
new_record = found_records[-1].copy()
new_record.update({
'Revision': str(new_revision),
'Created Date': datetime.datetime.now().strftime('%d %b %Y'),
'Inspection Date':
self.ui.Inspection_Date_QCB3.currentText().strip(),
'Status': "Under Review"
})
all_records.insert(0, new_record)
self._overwrite_csv(all_records)
self._populate_table_view([new_record] + found_records)
except FileNotFoundError:
self._show_dialog("Error", "WIR Log file not found!",
QMessageBox.Critical)
except Exception as e:
self._show_dialog("Error", f"An error occurred: {str(e)}",
QMessageBox.Critical)
def update_wir_status_and_comment(self):
wir_no_to_update = self.ui.WIRNo_QLE1.text().strip()
rev_to_update = self.ui.Rev_QCB1.currentText().strip()
new_status = self.ui.Status_QCB.currentText().strip()
new_comment_date = self.ui.CommentDate_QCB.currentText().strip()
updated_records = []
record_found = False
try:
with open(self.file_path, 'r', newline='') as f:
reader = csv.DictReader(f)
fieldnames = reader.fieldnames
for row in reader:
if row['WIR No'].strip() == wir_no_to_update and
row['Revision'].strip() == rev_to_update:
row['Status'] = new_status
row['Comment Date'] = new_comment_date
record_found = True
updated_records.append(row)
if record_found:
with open(self.file_path, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(updated_records)
self._show_dialog("Success", f"WIR No {wir_no_to_update} Rev
{rev_to_update} updated successfully!",
QMessageBox.Information)
self.Search_WIRLog() # Refresh the table view
else:
self._show_dialog("Error", f"WIR No {wir_no_to_update} Rev
{rev_to_update} not found.",
QMessageBox.Warning)
self._clear_ui_fields()
except FileNotFoundError:
self._show_dialog("Error", "WIR Log file not found!",
QMessageBox.Critical)
except Exception as e:
self._show_dialog("Error", f"An error occurred: {str(e)}",
QMessageBox.Critical)
def _clear_ui_fields(self):
self.ui.WIRNo_QLE1.clear()
self.ui.Rev_QCB1.setCurrentIndex(-1)
self.ui.Inspection_Date_QCB3.setCurrentIndex(-1)
self.ui.Status_QCB.setCurrentIndex(-1)
self.ui.CommentDate_QCB.setCurrentIndex(-1)
for row in range(self.ui.tableWidget.rowCount()):
for col in range(self.ui.tableWidget.columnCount()):
item = self.ui.tableWidget.item(row, col)
if item:
item.setText("")