Addresses #17 but it feels like we should have these definitions in the dbConfig. Which leads to the question of moving from QtSql to Pony ORM
95 lines
2.8 KiB
Python
95 lines
2.8 KiB
Python
import enum
|
|
from typing import Any
|
|
|
|
from PySide6.QtCore import (
|
|
QAbstractTableModel,
|
|
QDate,
|
|
QModelIndex,
|
|
QPersistentModelIndex,
|
|
Qt,
|
|
)
|
|
from PySide6.QtGui import QFont
|
|
from PySide6.QtSql import QSqlQuery
|
|
|
|
from lib.utils import query_error
|
|
|
|
|
|
class docketModel(QAbstractTableModel):
|
|
entries: list[list[str | None]] = []
|
|
|
|
class ColumnNames(enum.IntEnum):
|
|
date = 0
|
|
text = 1
|
|
|
|
def __init__(self, case_id: int | None = None) -> None:
|
|
super(docketModel, self).__init__()
|
|
if case_id == None:
|
|
return
|
|
self.newCase(case_id)
|
|
return
|
|
|
|
def newCase(self, case_id: int) -> None:
|
|
self.beginResetModel()
|
|
self.entries = []
|
|
query = QSqlQuery()
|
|
query.prepare(
|
|
"SELECT * FROM entries WHERE case_id = :cid " "ORDER BY entry_id"
|
|
)
|
|
q2 = QSqlQuery()
|
|
q2.prepare("SELECT * FROM documents WHERE entry_id = :eid")
|
|
|
|
query.bindValue(":cid", case_id)
|
|
if not query.exec():
|
|
query_error(query)
|
|
while query.next():
|
|
date = query.value("date")
|
|
assert isinstance(date, QDate)
|
|
row = [date.toString("MMMM d, yyyy"), query.value(3)]
|
|
self.entries.append(row)
|
|
q2.bindValue(":eid", query.value("entry_id"))
|
|
if not q2.exec():
|
|
query_error(q2)
|
|
row = []
|
|
while q2.next():
|
|
row.append(
|
|
f'<a href="{q2.value("url")}">{q2.value("name")}</a>'
|
|
)
|
|
if len(row) > 0:
|
|
self.entries.append([None, " ".join(row)])
|
|
self.endResetModel()
|
|
pass
|
|
|
|
def rowCount(
|
|
self, _: QModelIndex | QPersistentModelIndex = QModelIndex()
|
|
) -> int:
|
|
return len(self.entries)
|
|
|
|
def columnCount(
|
|
self, _: QModelIndex | QPersistentModelIndex = QModelIndex()
|
|
) -> int:
|
|
return 2
|
|
|
|
def data(
|
|
self, index: QModelIndex | QPersistentModelIndex, role: int = 0
|
|
) -> Any:
|
|
if not index.isValid():
|
|
return None
|
|
if role == Qt.ItemDataRole.DisplayRole:
|
|
return self.entries[index.row()][index.column()]
|
|
if role == Qt.ItemDataRole.TextAlignmentRole and index.column() == 0:
|
|
return Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignTop
|
|
return
|
|
|
|
def headerData(
|
|
self, section: int, orientation: Qt.Orientation, role: int = 0
|
|
) -> str | None | QFont:
|
|
if orientation == Qt.Orientation.Vertical:
|
|
return None
|
|
if role == Qt.ItemDataRole.FontRole:
|
|
font = QFont()
|
|
font.setBold(True)
|
|
return font
|
|
if role != Qt.ItemDataRole.DisplayRole:
|
|
return None
|
|
return ["Date", "Proceedings and Orders"][section]
|