-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngo_page.py
256 lines (199 loc) · 10.5 KB
/
ngo_page.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
from PyQt6 import QtWidgets, uic, QtCore
from PyQt6.QtCore import QDate, QTimer
from PyQt6.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget, QHeaderView
import sys
import pyodbc
from worker_data import WorkerData
from view_project import ViewProject
from new_project import NewProject
from new_area import NewArea
from new_category import NewCategory
import connectionString
class NGOPage(QtWidgets.QMainWindow):
def __init__(self, ngoID):
super().__init__()
uic.loadUi('Screens/NGOPage.ui' , self)
#set windows title
self.setWindowTitle("NGO Screen")
self.ngoName.setDisabled(True)
self.ngoEmail.setDisabled(True)
self.ngoAddress.setDisabled(True)
self.ngoRegDate.setDisabled(True)
self.ngoSaveBtn.setDisabled(True)
self.ngoEditBtn.clicked.connect(self.EditNGO)
self.ngoSaveBtn.clicked.connect(lambda: self.SaveNGO(ngoID))
self.ngoWorkersBtn.clicked.connect(lambda: self.Workers(ngoID))
self.ngoAddAreaBtn.clicked.connect(lambda: self.AddArea(ngoID))
self.ngoDeleteAreaBtn.clicked.connect(lambda: self.DeleteArea(ngoID))
self.ngoAddCategoryBtn.clicked.connect(lambda: self.AddCategory(ngoID))
self.ngoDeleteCategoryBtn.clicked.connect(lambda: self.DeleteCategory(ngoID))
self.ngoViewProjectBtn.clicked.connect(self.ShowProject)
self.ngoAddProjectBtn.clicked.connect(lambda: self.AddProject(ngoID))
self.ngoDeleteProjectBtn.clicked.connect(lambda: self.DeleteProject(ngoID))
self.ngoLogoutBtn.clicked.connect(self.Logout)
self.loadData(ngoID)
def refreshData(self, ngoID):
self.loadData(ngoID)
def loadData(self, ngoID):
connection = pyodbc.connect(connectionString.connection_string)
cursor=connection.cursor()
cursor.execute("SELECT name, ngoEmail, address, regDate FROM NGO WHERE ngoID = ?", ngoID)
ngoData = cursor.fetchall()[0]
self.ngoName.setText(ngoData[0])
self.ngoEmail.setText(ngoData[1])
self.ngoAddress.setText(ngoData[2])
self.ngoRegDate.setDate(ngoData[3])
self.ngoName.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.ngoEmail.home(False)
self.ngoAddress.home(False)
cursor.execute("SELECT areaName FROM Area WHERE areaCode in (SELECT areaCode FROM OperatingAreas WHERE ngoID = ?)", ngoID)
areas = [x[0] for x in cursor.fetchall()]
model = QtCore.QStringListModel()
model.setStringList(areas)
self.ngoOperatingAreas.setModel(model)
cursor.execute("SELECT categoryName FROM Category WHERE categoryName in (SELECT categoryName FROM OperatingCategories WHERE ngoID = ?)", ngoID)
categories = [x[0] for x in cursor.fetchall()]
model = QtCore.QStringListModel()
model.setStringList(categories)
self.ngoOperatingCategories.setModel(model)
header = self.ngoProjects.horizontalHeader()
for i in range(self.ngoProjects.columnCount()):
header.setSectionResizeMode(i, QHeaderView.ResizeMode.Stretch)
self.ngoProjects.clearContents()
self.ngoProjects.setRowCount(0)
cursor.execute("SELECT projectName, scale, startDate, endDate FROM Project WHERE ngoID = ?", ngoID)
for row_number, row_data in enumerate(cursor):
self.ngoProjects.insertRow(row_number)
for column_number, data in enumerate(row_data):
if column_number == 3 and data == None:
data = "-"
self.ngoProjects.setItem(row_number, column_number, QTableWidgetItem(str(data)))
self.ngoProjects.item(row_number, column_number).setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
# Close the database connection
connection.close()
def EditNGO(self):
self.ngoName.setDisabled(False)
self.ngoEmail.setDisabled(False)
self.ngoAddress.setDisabled(False)
self.ngoRegDate.setDisabled(False)
self.ngoSaveBtn.setDisabled(False)
#change edit button to cancel button
self.ngoEditBtn.setText("Cancel")
self.ngoEditBtn.clicked.connect(self.CancelEdit)
def CancelEdit(self):
self.ngoName.setDisabled(True)
self.ngoEmail.setDisabled(True)
self.ngoAddress.setDisabled(True)
self.ngoRegDate.setDisabled(True)
self.ngoSaveBtn.setDisabled(True)
self.ngoEditBtn.setText("Edit")
self.ngoEditBtn.clicked.connect(self.EditNGO)
def SaveNGO(self, ngoID):
Dialog = QtWidgets.QMessageBox()
Dialog.setWindowTitle("Confirmation Box")
Dialog.setText("Are You Sure You Want To Save The Changes?")
Dialog.setStandardButtons(QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No)
Option = Dialog.exec()
if Option == QtWidgets.QMessageBox.StandardButton.Yes:
connecion = pyodbc.connect(connectionString.connection_string)
cursor = connecion.cursor()
cursor.execute("UPDATE NGO SET name = ?, ngoEmail = ?, address = ?, regDate = ? WHERE ngoID = ?", self.ngoName.text(), self.ngoEmail.text(), self.ngoAddress.text(), self.ngoRegDate.date().toPyDate(), ngoID)
# print("updated")
# print(ngoID)
connecion.commit()
connecion.close()
self.ngoName.setDisabled(True)
self.ngoEmail.setDisabled(True)
self.ngoAddress.setDisabled(True)
self.ngoRegDate.setDisabled(True)
self.ngoSaveBtn.setDisabled(True)
#change text from cancel to edit
self.ngoEditBtn.setText("Edit")
self.ngoEditBtn.setDisabled(False)
def Workers(self, ngoID):
self.workers_page = WorkerData(ngoID)
self.workers_page.show()
def AddArea(self, ngoID):
self.new_area = NewArea(ngoID)
self.new_area.show()
self.new_area.areaDoneBtn.clicked.connect(lambda: self.refreshData(ngoID))
def DeleteArea(self, ngoID):
Dialog = QtWidgets.QMessageBox()
Dialog.setWindowTitle("Confirmation Box")
Dialog.setText("Are You Sure You Want To Delete This Area?")
Dialog.setStandardButtons(QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No)
Option = Dialog.exec()
if Option == QtWidgets.QMessageBox.StandardButton.Yes:
#get selected row from QListView
indexes = self.ngoOperatingAreas.selectionModel().selectedIndexes()
if indexes:
selected_index = indexes[0] # Assuming single selection
areaName = selected_index.data()
connection = pyodbc.connect(connectionString.connection_string)
cursor = connection.cursor()
cursor.execute("SELECT areaCode FROM Area WHERE areaName = ?", areaName)
areaCode = cursor.fetchall()[0][0]
cursor.execute("DELETE FROM OperatingAreas WHERE areaCode = ?", areaCode)
connection.commit()
connection.close()
self.refreshData(ngoID)
def AddCategory(self, ngoID):
self.new_category = NewCategory(ngoID)
self.new_category.show()
self.new_category.categoryDoneBtn.clicked.connect(lambda: self.refreshData(ngoID))
def DeleteCategory(self, ngoID):
Dialog = QtWidgets.QMessageBox()
Dialog.setWindowTitle("Confirmation Box")
Dialog.setText("Are You Sure You Want To Delete This Category?")
Dialog.setStandardButtons(QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No)
Option = Dialog.exec()
if Option == QtWidgets.QMessageBox.StandardButton.Yes:
indexes = self.ngoOperatingCategories.selectionModel().selectedIndexes()
if indexes:
selected_index = indexes[0] # Assuming single selection
categoryName = selected_index.data()
connection = pyodbc.connect(connectionString.connection_string)
cursor = connection.cursor()
cursor.execute("DELETE FROM OperatingCategories WHERE categoryName = ?", categoryName)
connection.commit()
connection.close()
self.refreshData(ngoID)
def ShowProject(self):
projectName = self.ngoProjects.item(self.ngoProjects.currentRow(), 0).text()
connection = pyodbc.connect(connectionString.connection_string)
cursor = connection.cursor()
cursor.execute("SELECT projectID FROM Project WHERE projectName = ?", projectName)
projectID = cursor.fetchall()[0][0]
self.projects_page = ViewProject(projectID)
self.projects_page.show()
def AddProject(self, ngoID):
self.new_project = NewProject(ngoID)
self.new_project.show()
self.new_project.projectDoneBtn.clicked.connect(lambda: self.refreshData(ngoID))
def DeleteProject(self, ngoID):
Dialog = QtWidgets.QMessageBox()
Dialog.setWindowTitle("Confirmation Box")
Dialog.setText("Are You Sure You Want To Delete This Project?")
Dialog.setStandardButtons(QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No)
Option = Dialog.exec()
if Option == QtWidgets.QMessageBox.StandardButton.Yes:
projectName = self.ngoProjects.item(self.ngoProjects.currentRow(), 0).text()
connection = pyodbc.connect(connectionString.connection_string)
cursor = connection.cursor()
cursor.execute("SELECT projectID FROM Project WHERE projectName = ?", projectName)
projectID = cursor.fetchall()[0][0]
#delete from WorkerProject
cursor.execute("DELETE FROM WorkerProject WHERE projectID = ?", projectID)
#delete from Donations
cursor.execute("DELETE FROM Donation WHERE projectID = ?", projectID)
#delete from project
cursor.execute("DELETE FROM Project WHERE projectName = ?", projectName)
connection.commit()
connection.close()
self.refreshData(ngoID)
def Logout(self):
self.close()
#open homescreen
from ui import UI
self.home = UI()
self.home.show()