-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't use QSqlDatabase #61
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,4 @@ | ||||||||||||||||||||
from PySide6.QtCore import Qt | ||||||||||||||||||||
from PySide6.QtSql import QSqlTableModel | ||||||||||||||||||||
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt | ||||||||||||||||||||
from PySide6.QtWidgets import ( | ||||||||||||||||||||
QAbstractItemView, | ||||||||||||||||||||
QGroupBox, | ||||||||||||||||||||
|
@@ -8,6 +7,98 @@ | |||||||||||||||||||
QVBoxLayout, | ||||||||||||||||||||
QWidget, | ||||||||||||||||||||
) | ||||||||||||||||||||
from sqlalchemy import delete | ||||||||||||||||||||
|
||||||||||||||||||||
from alinka.db.connection import db_session | ||||||||||||||||||||
from alinka.db.models import TeamMember | ||||||||||||||||||||
from alinka.db.queries import get_team_members, upsert_team_members | ||||||||||||||||||||
from alinka.schemas import TeamMemberDbSchema | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
class TeamMemberTableModel(QAbstractTableModel): | ||||||||||||||||||||
def __init__(self): | ||||||||||||||||||||
self.insert_row = None | ||||||||||||||||||||
super().__init__() | ||||||||||||||||||||
|
||||||||||||||||||||
def columnCount(self, parent=QModelIndex()): | ||||||||||||||||||||
return len(TeamMember.__table__.columns) | ||||||||||||||||||||
|
||||||||||||||||||||
def data(self, index, role=Qt.DisplayRole): | ||||||||||||||||||||
"""Return the data for the given index.""" | ||||||||||||||||||||
if not index.isValid(): | ||||||||||||||||||||
return None | ||||||||||||||||||||
if role == Qt.DisplayRole: | ||||||||||||||||||||
if self.insert_row is not None and index.row() + 1 == self.rowCount(): | ||||||||||||||||||||
return self.insert_row.get(TeamMember.__table__.columns.keys()[index.column()], "") | ||||||||||||||||||||
Comment on lines
+31
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you split this line to make it more readable?
Suggested change
|
||||||||||||||||||||
return getattr( | ||||||||||||||||||||
get_team_members()[index.row()], | ||||||||||||||||||||
TeamMember.__table__.columns.keys()[index.column()], | ||||||||||||||||||||
) | ||||||||||||||||||||
return None | ||||||||||||||||||||
Comment on lines
+33
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you splilt it?
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
def headerData(self, section, orientation, role=Qt.DisplayRole): | ||||||||||||||||||||
if role == Qt.DisplayRole: | ||||||||||||||||||||
if orientation == Qt.Horizontal: | ||||||||||||||||||||
return TeamMember.__table__.columns.keys()[section] | ||||||||||||||||||||
if orientation == Qt.Vertical: | ||||||||||||||||||||
return section + 1 | ||||||||||||||||||||
return None | ||||||||||||||||||||
|
||||||||||||||||||||
def rowCount(self, parent=QModelIndex()): | ||||||||||||||||||||
return len(get_team_members()) + (1 if self.insert_row is not None else 0) | ||||||||||||||||||||
|
||||||||||||||||||||
Comment on lines
+47
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just suggestion to place |
||||||||||||||||||||
def flags(self, index): | ||||||||||||||||||||
"""Set flags for each cell.""" | ||||||||||||||||||||
if not index.isValid(): | ||||||||||||||||||||
return Qt.NoItemFlags | ||||||||||||||||||||
|
||||||||||||||||||||
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable | Qt.ItemIsUserCheckable | ||||||||||||||||||||
|
||||||||||||||||||||
def setData(self, index, value, role=Qt.EditRole): | ||||||||||||||||||||
if not index.isValid(): | ||||||||||||||||||||
return False | ||||||||||||||||||||
if self.insert_row is not None and index.row() + 1 == self.rowCount(): | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WDYT to split it into more meaningful lines?
Suggested change
|
||||||||||||||||||||
self.insert_row[TeamMember.__table__.columns.keys()[index.column()]] = value | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You call for this |
||||||||||||||||||||
tm_dict = { | ||||||||||||||||||||
k: self.data(index.siblingAtColumn(i)) for i, k in enumerate(TeamMember.__table__.columns.keys()) | ||||||||||||||||||||
} | ||||||||||||||||||||
# this is quasi validation - we need to figure out better approach here: | ||||||||||||||||||||
del tm_dict["id"] | ||||||||||||||||||||
if all(v for k, v in tm_dict.items()): | ||||||||||||||||||||
with db_session() as db: | ||||||||||||||||||||
tm = TeamMember(**tm_dict) | ||||||||||||||||||||
db.add(tm) | ||||||||||||||||||||
db.commit() | ||||||||||||||||||||
Comment on lines
+68
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls write dedicated query in |
||||||||||||||||||||
self.insert_row = None | ||||||||||||||||||||
self.dataChanged.emit(index, index) | ||||||||||||||||||||
return True | ||||||||||||||||||||
return False | ||||||||||||||||||||
|
||||||||||||||||||||
tm_dict = {k: self.data(index.siblingAtColumn(i)) for i, k in enumerate(TeamMember.__table__.columns.keys())} | ||||||||||||||||||||
tm_dict[TeamMember.__table__.columns.keys()[index.column()]] = value | ||||||||||||||||||||
tm = TeamMemberDbSchema.model_validate(tm_dict) | ||||||||||||||||||||
upsert_team_members([tm]) | ||||||||||||||||||||
|
||||||||||||||||||||
self.dataChanged.emit(index, index) | ||||||||||||||||||||
return True | ||||||||||||||||||||
|
||||||||||||||||||||
def removeRow(self, row): | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest to add typing for all methods. |
||||||||||||||||||||
if self.insert_row is not None and row + 1 == self.rowCount(): | ||||||||||||||||||||
self.insert_row = None | ||||||||||||||||||||
else: | ||||||||||||||||||||
db_id = self.data(self.createIndex(row, 0)) | ||||||||||||||||||||
with db_session() as db: | ||||||||||||||||||||
stmt = delete(TeamMember).where(TeamMember.id == db_id) | ||||||||||||||||||||
db.execute(stmt) | ||||||||||||||||||||
db.commit() | ||||||||||||||||||||
Comment on lines
+90
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. Can you implement it in |
||||||||||||||||||||
|
||||||||||||||||||||
self.layoutChanged.emit() | ||||||||||||||||||||
return True | ||||||||||||||||||||
|
||||||||||||||||||||
def insertRow(self, row): | ||||||||||||||||||||
self.insert_row = {} | ||||||||||||||||||||
self.layoutChanged.emit() | ||||||||||||||||||||
return True | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
class TeamMemberTableGroup(QGroupBox): | ||||||||||||||||||||
|
@@ -17,10 +108,7 @@ def __init__(self, parent: QWidget): | |||||||||||||||||||
layout = QVBoxLayout(self) | ||||||||||||||||||||
layout.setAlignment(Qt.AlignTop) | ||||||||||||||||||||
|
||||||||||||||||||||
self.table_model = QSqlTableModel() | ||||||||||||||||||||
self.table_model.setTable("team_member") | ||||||||||||||||||||
self.table_model.setEditStrategy(QSqlTableModel.OnFieldChange) | ||||||||||||||||||||
self.table_model.select() | ||||||||||||||||||||
self.table_model = TeamMemberTableModel() | ||||||||||||||||||||
|
||||||||||||||||||||
self.table = QTableView() | ||||||||||||||||||||
self.table.clicked.connect(self.row_selected_event) | ||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You perform this check
self.insert_row is not None
4 times in this class. Maybe you can write wrapper with meaningful name:and this second check: