def finish(self): if not self.tree.selectedIndexes(): return False shareId = self.model.item(self.tree.selectedIndexes()[0].row()).text() shareId = False if not shareId else shareId conn = db.getConnection() cursor = conn.cursor() if not shareId: cursor.execute( 'INSERT INTO sharings (subjectId, sections) VALUES (?, ?)', [ self.id, json.dumps([ self.section_id, self.model.item(self.tree.selectedIndexes()[0].row(), 2).text() ]) ]) self.shareId = cursor.lastrowid else: subjectID = self.model.item(self.tree.selectedIndexes()[0].row(), 2).text().split(',') subjectID.append(self.section_id) cursor.execute('UPDATE sharings SET sections = ? WHERE id = ?', [json.dumps(subjectID), shareId]) self.shareId = shareId conn.commit() conn.close() self.shareMembersText = self.model.item( self.tree.selectedIndexes()[0].row(), 1).text() self.dialog.close()
def stopOperation(self): self.toggleState(False) self.resourceWorker.terminate() self.resourceWorker.runThread = False self.geneticAlgorithm.terminate() self.timer.stop() if len(self.topChromosomes): self.parent.btnStop.setText('View Result') self.parent.btnStop.clicked.disconnect(self.stopOperation) self.parent.btnStop.clicked.connect(self.dialog.close) self.parent.lblCPU.setText('CPU Usage: Stopped') self.parent.lblMemory.setText('Memory Usage: Stopped') self.parent.lblStatus.setText('Status: Stopped') self.totalResource['cpu'] = mean(self.totalResource['cpu']) self.totalResource['memory'] = mean(self.totalResource['memory']) self.meta = [[chromosome[1], chromosome[0].fitnessDetails] for chromosome in self.topChromosomes] conn = db.getConnection() cursor = conn.cursor() cursor.execute('INSERT INTO results (content) VALUES (?)', [Binary( pickle.dumps({'data': [chromosome[0].data for chromosome in self.topChromosomes], 'meta': self.meta, 'time': self.time.toString('hh:mm:ss'), 'resource': self.totalResource, 'rawData': self.data}, pickle.HIGHEST_PROTOCOL))]) conn.commit() conn.close() else: self.dialog.close()
def display(self): self.model.removeRows(0, self.model.rowCount()) conn = db.getConnection() cursor = conn.cursor() cursor.execute('SELECT id, active, name, stay FROM sections') result = cursor.fetchall() conn.close() for instr in result: id = QtGui.QStandardItem(str(instr[0])) id.setEditable(False) availability = QtGui.QStandardItem() availability.setCheckable(True) availability.setCheckState(2 if instr[1] == 1 else 0) availability.setEditable(False) name = QtGui.QStandardItem(instr[2]) stay = QtGui.QStandardItem('TRUE' if instr[3] == 1 else 'FALSE') stay.setEditable(False) name.setEditable(False) edit = QtGui.QStandardItem() edit.setEditable(False) self.model.appendRow([id, availability, name, stay, edit]) frameEdit = QtWidgets.QFrame() btnEdit = QtWidgets.QPushButton('Modifier', frameEdit) btnEdit.clicked.connect(lambda state, id=instr[0]: self.edit(id)) btnDelete = QtWidgets.QPushButton('Supprimer', frameEdit) btnDelete.clicked.connect( lambda state, id=instr[0]: self.delete(id)) frameLayout = QtWidgets.QHBoxLayout(frameEdit) frameLayout.setContentsMargins(0, 0, 0, 0) frameLayout.addWidget(btnEdit) frameLayout.addWidget(btnDelete) self.tree.setIndexWidget(edit.index(), frameEdit)
def finish(self): if not self.parent.lineEditName.text(): return False name = self.parent.lineEditName.text() schedule = json.dumps(self.table.getData()) subjects = [] for row in range(self.model.rowCount()): if self.model.item(row, 1).checkState() == 2: subjects.append(self.model.item(row, 0).text()) subjects = json.dumps(subjects) if self.parent.radioButton_A.isChecked(): section_type = 'A' elif self.parent.radioButton_B.isChecked(): section_type = 'B' else: section_type = 'C' conn = db.getConnection() cursor = conn.cursor() if self.id: cursor.execute( 'UPDATE sections SET name = ?, schedule = ?, section_type = ? WHERE id = ?', [name, schedule, section_type, self.id]) else: cursor.execute( 'INSERT INTO sections (name, schedule, section_type) VALUES (?, ?, ?)', [name, schedule, section_type]) conn.commit() conn.close() self.dialog.close()
def display(self): self.model.removeRows(0, self.model.rowCount()) conn = db.getConnection() cursor = conn.cursor() cursor.execute('SELECT id, active, name FROM rooms') result = cursor.fetchall() conn.close() for entry in result: id = QtGui.QStandardItem(str(entry[0])) id.setEditable(False) availability = QtGui.QStandardItem() availability.setCheckable(True) availability.setCheckState(2 if entry[1] == 1 else 0) availability.setEditable(False) name = QtGui.QStandardItem(entry[2]) name.setEditable(False) edit = QtGui.QStandardItem() edit.setEditable(False) self.model.appendRow([id, availability, name, edit]) frameEdit = QtWidgets.QFrame() btnEdit = QtWidgets.QPushButton('Edit', frameEdit) btnEdit.clicked.connect(lambda state, id=entry[0]: self.edit(id)) btnDelete = QtWidgets.QPushButton('Delete', frameEdit) btnEdit.setStyleSheet('background-color:#833471;color:white;') btnDelete.setStyleSheet('background-color:#747d8c;color:white;') btnDelete.clicked.connect(lambda state, id=entry[0]: self.delete(id)) frameLayout = QtWidgets.QHBoxLayout(frameEdit) frameLayout.setContentsMargins(2, 2, 2, 2) frameLayout.addWidget(btnEdit) frameLayout.addWidget(btnDelete) self.tree.setIndexWidget(edit.index(), frameEdit)
def setupInstructors(self): self.tree = tree = self.parent.treeSchedule self.model = model = QtGui.QStandardItemModel() model.setHorizontalHeaderLabels(['ID', 'Actif', 'Nom']) tree.setModel(model) tree.setColumnHidden(0, True) conn = db.getConnection() cursor = conn.cursor() cursor.execute('SELECT id, name FROM instructors WHERE active = 1') instructors = cursor.fetchall() subjectAssignments = [] if self.id: cursor.execute('SELECT instructors FROM subjects WHERE id = ?', [self.id]) subjectAssignments = list( map(lambda id: int(id), json.loads(cursor.fetchone()[0]))) conn.close() for entry in instructors: id = QtGui.QStandardItem(str(entry[0])) id.setEditable(False) availability = QtGui.QStandardItem() availability.setCheckable(True) availability.setCheckState(2 if entry[0] in subjectAssignments else 0) availability.setEditable(False) name = QtGui.QStandardItem(str(entry[1])) name.setEditable(False) model.appendRow([id, availability, name])
def toggleAvailability(self, item): id = self.model.data(self.model.index(item.row(), 0)) newValue = 1 if item.checkState() == 2 else 0 conn = db.getConnection() cursor = conn.cursor() cursor.execute('UPDATE rooms SET active = ? WHERE id = ?', [newValue, id]) conn.commit() conn.close()
def removeTables(): conn = db.getConnection() cursor = conn.cursor() tables = list( cursor.execute("SELECT name FROM sqlite_master WHERE type IS 'table'")) cursor.executescript(';'.join( ['DROP TABLE IF EXISTS {}'.format(table[0]) for table in tables])) conn.close()
def setSharings(self): self.tree = tree = self.parent.treeSections self.model = model = QtGui.QStandardItemModel() model.setHorizontalHeaderLabels(['ID', 'Sections', 'SectionID']) tree.setModel(model) tree.setColumnHidden(0, True) tree.setColumnHidden(2, True) model.itemChanged.connect(lambda item: self.toggleSharing(item)) conn = db.getConnection() cursor = conn.cursor() # Get sections with mutual subjects if self.section_id: cursor.execute( 'SELECT id, name, subjects FROM sections WHERE active = 1 AND id != ?', [self.section_id]) else: cursor.execute( 'SELECT id, name, subjects FROM sections WHERE active = 1') sections = cursor.fetchall() cursor.execute( 'SELECT id, sections FROM sharings WHERE subjectId = ? AND final = 1', [self.id]) sharings = cursor.fetchall() sharedSections = list( set([ section for sectionGroup in list( map( lambda sharing: list( map(lambda id: int(id), json.loads(sharing[1]))), sharings)) for section in sectionGroup ])) sharings = dict(sharings) conn.close() sectionDict = {} for section in sections: sectionDict[section[0]] = section[1] if self.id not in list( map(lambda id: int(id), json.loads( section[2]))) or section[0] in sharedSections: continue id = QtGui.QStandardItem() id.setEditable(False) sectionList = QtGui.QStandardItem(section[1]) sectionList.setEditable(False) sectionID = QtGui.QStandardItem(str(section[0])) sectionID.setEditable(False) self.model.appendRow([id, sectionList, sectionID]) for key, value in sharings.items(): sectionIDList = list(map(lambda id: int(id), json.loads(value))) if self.section_id in sectionIDList: continue id = QtGui.QStandardItem(str(key)) sectionList = QtGui.QStandardItem(', '.join( list(map(lambda id: sectionDict[id], sectionIDList)))) sectionList.setEditable(False) sectionID = QtGui.QStandardItem(','.join(map(str, sectionIDList))) self.model.appendRow([id, sectionList, sectionID])
def insertRoom(data): conn = db.getConnection() cursor = conn.cursor() if len(data) > 3: cursor.execute('UPDATE rooms SET name = ?, schedule = ?, type = ? WHERE id = ?', data) else: cursor.execute('INSERT INTO rooms (name, schedule, type) VALUES (?, ?, ?)', data) conn.commit() conn.close()
def saveAs(): fileName = QtWidgets.QFileDialog.getSaveFileName(None, 'Enregistrer un Scenario', '', 'Scenario(*.git)') if not fileName[0]: return False with open(fileName[0], 'w+') as file: conn = db.getConnection() for line in conn.iterdump(): file.write('{}\n'.format(line)) conn.close()
def insertInstructor(data): conn = db.getConnection() cursor = conn.cursor() if len(data) > 3: cursor.execute('UPDATE instructors SET name = ?, hours = ?, schedule = ? WHERE id = ?', data) else: cursor.execute('INSERT INTO instructors (name, hours, schedule) VALUES (?, ?, ?)', data) conn.commit() conn.close() return True
def fillForm(self): conn = db.getConnection() cursor = conn.cursor() cursor.execute('SELECT name, hours, schedule FROM instructors WHERE id = ?', [self.id]) result = cursor.fetchone() conn.close() self.parent.lineEditName.setText(str(result[0])) self.parent.lineEditHours.setText(str(result[1])) # Generate timetable from custom schedule self.table = Timetable.Timetable(self.parent.tableSchedule, json.loads(result[2]))
def load(): fileName = QtWidgets.QFileDialog().getOpenFileName(None, 'Charger un Scenario', '', 'Scenario(*.git)') if not fileName[0]: return False with open(fileName[0], 'r') as file: conn = db.getConnection() cursor = conn.cursor() tables = list(cursor.execute("SELECT name FROM sqlite_master WHERE type IS 'table'")) cursor.executescript(';'.join(['DROP TABLE IF EXISTS {}'.format(table[0]) for table in tables])) cursor.executescript(file.read()) conn.close()
def fillForm(self): conn = db.getConnection() cursor = conn.cursor() cursor.execute('SELECT name, schedule, type FROM rooms WHERE id = ?', [self.id]) result = cursor.fetchone() conn.close() self.parent.lineEditName.setText(str(result[0])) self.table = Timetable.Timetable(self.parent.tableSchedule, json.loads(result[1])) if result[2] == 'lec': self.parent.radioLec.setChecked(True) else: self.parent.radioLab.setChecked(True)
def fillForm(self): conn = db.getConnection() cursor = conn.cursor() cursor.execute( 'SELECT name, schedule, stay FROM sections WHERE id = ?', [self.id]) result = cursor.fetchone() conn.close() self.parent.lineEditName.setText(str(result[0])) self.parent.checkStay.setChecked(result[2]) self.table = Timetable.Timetable(self.parent.tableSchedule, json.loads(result[1]))
def insertSubject(data): conn = db.getConnection() cursor = conn.cursor() if len(data) > 7: cursor.execute( 'UPDATE subjects SET name = ?, hours = ?, code = ?, description = ?, instructors = ?, divisible = ?, type = ? WHERE id = ?', data) else: cursor.execute( 'INSERT INTO subjects (name, hours, code, description, instructors, divisible, type) VALUES (?, ?, ?, ?, ?, ?, ?)', data) conn.commit() conn.close()
def delete(self, id): confirm = QtWidgets.QMessageBox() confirm.setIcon(QtWidgets.QMessageBox.Warning) confirm.setText('Are you sure you want to delete this entry?') confirm.setWindowTitle('Confirm Delete') confirm.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) result = confirm.exec_() if result == 16384: conn = db.getConnection() cursor = conn.cursor() cursor.execute('DELETE FROM rooms WHERE id = ?', [id]) conn.commit() conn.close() self.display()
def display(self): self.model.removeRows(0, self.model.rowCount()) conn = db.getConnection() cursor = conn.cursor() cursor.execute( 'SELECT id, code, name, type, instructors FROM subjects') result = cursor.fetchall() cursor.execute('SELECT id, name FROM instructors WHERE active = 1') instructorList = dict(cursor.fetchall()) conn.close() for entry in result: id = QtGui.QStandardItem(str(entry[0])) id.setEditable(False) code = QtGui.QStandardItem(entry[1]) code.setEditable(False) name = QtGui.QStandardItem(entry[2]) name.setEditable(False) type = QtGui.QStandardItem(entry[3].upper()) type.setEditable(False) instructorID = list( set(map(lambda id: int(id), json.loads(entry[4]))).intersection( set(instructorList.keys()))) if len(instructorID) > 3: instructorText = ', '.join( list(map(lambda id: instructorList[id], instructorID[0:3])) ) + ' and ' + str(len(instructorID) - 3) + ' more' elif len(instructorID) > 0: instructorText = ', '.join( list(map(lambda id: instructorList[id], instructorID))) else: instructorText = '' instructors = QtGui.QStandardItem(instructorText) instructors.setEditable(False) edit = QtGui.QStandardItem() edit.setEditable(False) self.model.appendRow([id, code, name, type, instructors, edit]) frameEdit = QtWidgets.QFrame() btnEdit = QtWidgets.QPushButton('Edit', frameEdit) btnEdit.clicked.connect(lambda state, id=entry[0]: self.edit(id)) btnDelete = QtWidgets.QPushButton('Delete', frameEdit) btnDelete.clicked.connect( lambda state, id=entry[0]: self.delete(id)) btnEdit.setStyleSheet('background-color:#833471;color:white;') btnDelete.setStyleSheet('background-color:#747d8c;color:white;') frameLayout = QtWidgets.QHBoxLayout(frameEdit) frameLayout.setContentsMargins(2, 2, 2, 2) frameLayout.addWidget(btnEdit) frameLayout.addWidget(btnDelete) self.tree.setIndexWidget(edit.index(), frameEdit)
def delete(self, id): confirm = QtWidgets.QMessageBox() confirm.setIcon(QtWidgets.QMessageBox.Warning) confirm.setText('Voulez vous vrament supprimer cette entrée?') confirm.setWindowTitle('Supprimer') confirm.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) result = confirm.exec_() if result == 16384: conn = db.getConnection() cursor = conn.cursor() cursor.execute('DELETE FROM rooms WHERE id = ?', [id]) conn.commit() conn.close() self.display()
def getLastResult(self): conn = db.getConnection() cursor = conn.cursor() cursor.execute('SELECT content FROM results WHERE id = (SELECT MAX(id) FROM results)') result = cursor.fetchone() conn.close() if result: self.result = pickle.loads(result[0]) else: messageBox = QtWidgets.QMessageBox() messageBox.setWindowTitle('No Data') messageBox.setIcon(QtWidgets.QMessageBox.Information) messageBox.setText('You haven\'t generated a solution yet!') messageBox.setStandardButtons(QtWidgets.QMessageBox.Ok) messageBox.exec_() self.run = False
def getLastResult(self): conn = db.getConnection() cursor = conn.cursor() cursor.execute( 'SELECT content FROM results WHERE id = (SELECT MAX(id) FROM results)' ) result = cursor.fetchone() conn.close() if result: self.result = pickle.loads(result[0]) else: messageBox = QtWidgets.QMessageBox() messageBox.setWindowTitle('Pas de donnée enregistrée') messageBox.setIcon(QtWidgets.QMessageBox.Information) messageBox.setText('Vous n\'avez pas encore générer de solutions!') messageBox.setStandardButtons(QtWidgets.QMessageBox.Ok) messageBox.exec_() self.run = False
def display(self): # Clear model self.model.removeRows(0, self.model.rowCount()) conn = db.getConnection() cursor = conn.cursor() cursor.execute('SELECT id, active, hours, name FROM instructors') result = cursor.fetchall() conn.close() for instr in result: # ID Item id = QtGui.QStandardItem(str(instr[0])) id.setEditable(False) # Availability Item availability = QtGui.QStandardItem() availability.setCheckable(True) availability.setCheckState(2 if instr[1] == 1 else 0) availability.setEditable(False) # Hours Item hours = QtGui.QStandardItem(str(instr[2])) hours.setEditable(False) # Name Item name = QtGui.QStandardItem(instr[3]) name.setEditable(False) # Edit Item / Container for operation buttons edit = QtGui.QStandardItem() edit.setEditable(False) # Append items to model self.model.appendRow([id, availability, name, hours, edit]) # Create a widget group for edit and delete buttons frameEdit = QtWidgets.QFrame() btnEdit = QtWidgets.QPushButton('Edit', frameEdit) btnEdit.setStyleSheet('background-color:#833471;color:white;') btnEdit.clicked.connect(lambda state, id=instr[0]: self.edit(id)) btnDelete = QtWidgets.QPushButton('Delete', frameEdit) btnDelete.setStyleSheet('background-color:#747d8c;color:white;') btnDelete.clicked.connect(lambda state, id=instr[0]: self.delete(id)) frameLayout = QtWidgets.QHBoxLayout(frameEdit) frameLayout.setContentsMargins(2, 2, 2, 2) frameLayout.addWidget(btnEdit) frameLayout.addWidget(btnDelete) # Append the widget group to edit item self.tree.setIndexWidget(edit.index(), frameEdit)
def checkContents(self): conn = Database.getConnection() cursor = conn.cursor() disabled = False cursor.execute('SELECT id FROM rooms LIMIT 1') if cursor.fetchone(): disabled = True cursor.execute('SELECT id FROM instructors LIMIT 1') if cursor.fetchone(): disabled = True cursor.execute('SELECT id FROM sections LIMIT 1') if cursor.fetchone(): disabled = True cursor.execute('SELECT id FROM subjects LIMIT 1') if cursor.fetchone(): disabled = True self.timeStarting.setDisabled(disabled) self.timeEnding.setDisabled(disabled) self.btnScenGenerate.setDisabled(not disabled) conn.close()
def fillForm(self): conn = db.getConnection() cursor = conn.cursor() cursor.execute('SELECT name, hours, code, description, divisible, type FROM subjects WHERE id = ?', [self.id]) result = cursor.fetchone() conn.close() self.parent.lineEditName.setText(str(result[0])) self.parent.lineEditHours.setText(str(result[1])) self.parent.lineEditCode.setText(str(result[2])) self.parent.lineEditDescription.setText(str(result[3])) if result[4]: self.parent.radioYes.setChecked(True) else: self.parent.radioNo.setChecked(True) if result[5] == 'lec': self.parent.radioLec.setChecked(True) elif result[5] == 'lab': self.parent.radioLab.setChecked(True) else: self.parent.radioAny.setChecked(True)
def finish(self): if not self.parent.lineEditName.text(): return False name = self.parent.lineEditName.text() stay = 1 if self.parent.checkStay.isChecked() else 0 schedule = json.dumps(self.table.getData()) subjects = [] for row in range(self.model.rowCount()): if self.model.item(row, 1).checkState() == 2: subjects.append(self.model.item(row, 0).text()) subjects = json.dumps(subjects) conn = db.getConnection() cursor = conn.cursor() if self.removeShareId: for id in self.removeShareId: cursor.execute('SELECT sections FROM sharings WHERE id = ?', [id]) result = list(map(int, json.loads(cursor.fetchone()[0]))) if len(result) > 2: result.remove(self.id) cursor.execute( 'UPDATE sharings SET sections = ? WHERE id = ?', [json.dumps(result), id]) else: cursor.execute( 'UPDATE sharings SET final = 0 WHERE id = ?', [id]) if self.shareId: for id in self.shareId: cursor.execute('UPDATE sharings SET final = 1 WHERE id = ?', [id]) if self.id: cursor.execute( 'UPDATE sections SET name = ?, schedule = ?, subjects = ?, stay = ? WHERE id = ?', [name, schedule, subjects, stay, self.id]) else: cursor.execute( 'INSERT INTO sections (name, schedule, subjects, stay) VALUES (?, ?, ?, ?)', [name, schedule, subjects, stay]) conn.commit() conn.close() self.dialog.close()
def fillForm(self): conn = db.getConnection() cursor = conn.cursor() cursor.execute( 'SELECT name, hours, code, description, subject_type, withTest FROM subjects WHERE id = ?', [self.id]) result = cursor.fetchone() conn.close() self.parent.lineEditName.setText(str(result[0])) self.parent.lineEditHours.setText(str(result[1])) self.parent.lineEditCode.setText(str(result[2])) self.parent.lineEditDescription.setText(str(result[3])) if result[5]: self.parent.radioWithTest.setChecked(True) else: self.parent.radioWithoutTest.setChecked(True) if result[4] == 'A': self.parent.radioA.setChecked(True) elif result[4] == 'B': self.parent.radioB.setChecked(True) else: self.parent.radioC.setChecked(True)
def setupInstructors(self): self.tree = tree = self.parent.treeSchedule self.model = model = QtGui.QStandardItemModel() model.setHorizontalHeaderLabels( ['ID', 'Available', "Co-Ref", "Will machen Score", 'Name']) tree.setModel(model) tree.setColumnHidden(0, True) conn = db.getConnection() cursor = conn.cursor() cursor.execute('SELECT id, name FROM instructors WHERE active = 1') instructors = cursor.fetchall() subjectAssignments = [] if self.id: cursor.execute('SELECT instructors FROM subjects WHERE id = ?', [self.id]) subjectAssignments = json.loads(cursor.fetchone()[0]) subjectAssignmentsIDs = list( map(lambda d: int(d), subjectAssignments.keys())) conn.close() for entry in instructors: id = QtGui.QStandardItem(str(entry[0])) id.setEditable(False) availability = QtGui.QStandardItem() availability.setCheckable(True) availability.setCheckState(2 if entry[0] in subjectAssignmentsIDs else 0) availability.setEditable(False) co_ref = QtGui.QStandardItem() co_ref.setCheckable(True) co_ref.setCheckState(subjectAssignments[str(entry[0])]["co_ref"] if entry[0] in subjectAssignmentsIDs else 0) co_ref.setEditable(False) do_score = QtGui.QStandardItem( subjectAssignments[str(entry[0])]["score"] if entry[0] in subjectAssignmentsIDs else 0) do_score.setEditable(True) name = QtGui.QStandardItem(str(entry[1])) name.setEditable(False) model.appendRow([id, availability, co_ref, do_score, name])
def __init__(self): self.conn = db.getConnection() self.cursor = self.conn.cursor()
def setupSubjects(self): # Setup subjects tree view self.tree = tree = self.parent.treeSubjects self.model = model = QtGui.QStandardItemModel() model.setHorizontalHeaderLabels( ['ID', 'Available', 'Subject Code', 'Subject Name']) tree.setModel(model) tree.setColumnHidden(0, True) tree.setColumnHidden(5, True) # Populate tree with values conn = db.getConnection() cursor = conn.cursor() # Get subjects for listing cursor.execute('SELECT id, name, code FROM subjects') subjects = cursor.fetchall() # Subjects that the current section have currentSubjects = [] # Subjects that are shared to the current section # [(sharing_id, subject_id, sections [str of list - load using json.loads])] sharedSubjects = [] if self.id: cursor.execute('SELECT subjects FROM sections WHERE id = ?', [self.id]) # Convert result into list of int currentSubjects = list( map(lambda id: int(id), json.loads(cursor.fetchall()[0][0]))) # Get active sharing by subject for id in currentSubjects: cursor.execute( 'SELECT id, subjectId, sections FROM sharings WHERE subjectId = ? AND final = 1', [id]) sharedSubjects.append(cursor.fetchone()) sharedSubjects = [sharing for sharing in sharedSubjects if sharing] # Get section names # {id: name} sectionNames = [] cursor.execute('SELECT id, name FROM sections WHERE active = 1') sectionNames = dict(cursor.fetchall()) conn.close() for subject in subjects: subjectId = QtGui.QStandardItem(str(subject[0])) subjectId.setEditable(False) availability = QtGui.QStandardItem() availability.setCheckable(True) availability.setEditable(False) availability.setCheckState(2 if subject[0] in currentSubjects else 0) shared = QtGui.QStandardItem('') shared.setCheckable(True) shared.setEditable(False) shareId = QtGui.QStandardItem() shareId.setEditable(False) for sharing in sharedSubjects: if sharing[1] != subject[0]: continue sectionList = list( map(lambda id: int(id), json.loads(sharing[2]))) if self.id not in sectionList: continue sectionList.remove(self.id) sectionList = ', '.join( list(map(lambda id: sectionNames[id], sectionList))) shared.setText(sectionList) shared.setCheckState(2) shareId.setText(str(sharing[0])) code = QtGui.QStandardItem(subject[2]) code.setEditable(False) name = QtGui.QStandardItem(subject[1]) name.setEditable(False) model.appendRow([subjectId, availability, code, name]) model.itemChanged.connect(lambda item: self.toggleSharing(item))