Working code. Need to populate
This commit is contained in:
@@ -5,27 +5,16 @@ import sys
|
||||
import dateparser
|
||||
import requests
|
||||
from typing import NoReturn
|
||||
from PySide6.QtCore import QCoreApplication, QModelIndex, Signal, Qt
|
||||
from PySide6.QtCore import QCoreApplication, QModelIndex, Signal, Qt, Slot
|
||||
from PySide6.QtSql import QSqlDatabase, QSqlQuery, QSqlQueryModel
|
||||
from PySide6.QtWidgets import QAbstractItemView, QApplication, QHeaderView, QMainWindow, QStyledItemDelegate, QTableWidgetItem
|
||||
|
||||
from bs4 import BeautifulSoup, Tag
|
||||
|
||||
from docketModel import docketModel
|
||||
from ui.MainWindow import Ui_MainWindow
|
||||
|
||||
from lib.utils import query_error
|
||||
translate = QCoreApplication.translate
|
||||
def query_error(query: QSqlQuery) -> NoReturn:
|
||||
"""Standarized query error reporter."""
|
||||
print(
|
||||
translate("MainWindow", "SQL Error:\n")
|
||||
+ "{}\n{}\n{}:{}".format(
|
||||
query.executedQuery(),
|
||||
query.boundValues(),
|
||||
query.lastError().type(),
|
||||
query.lastError().text(),
|
||||
)
|
||||
)
|
||||
raise Exception(translate("MainWindow", "SQL Error"))
|
||||
|
||||
class dateDelegate(QStyledItemDelegate):
|
||||
def displayText(self, value, locale) -> str:
|
||||
@@ -64,40 +53,29 @@ class MainWindow(QMainWindow, Ui_MainWindow):
|
||||
self.casesView.doubleClicked.connect(self.rowClicked)
|
||||
self.casesView.clicked.connect(self.rowClicked)
|
||||
|
||||
self.docketWidget.setColumnCount(2)
|
||||
self.docketWidget.setHorizontalHeaderLabels([
|
||||
'Date','Proceedings and Orders',
|
||||
])
|
||||
self.docketWidget.resizeColumnToContents(0)
|
||||
self.docketWidget.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch)
|
||||
self.docketView.setModel(docketModel())
|
||||
self.docketView.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch)
|
||||
self.docketView.resizeRowsToContents()
|
||||
return
|
||||
|
||||
def populateDocket(self, case_id:int) -> None:
|
||||
query = QSqlQuery()
|
||||
query.prepare("SELECT * FROM entries WHERE case_id=:cid ORDER BY entry_id")
|
||||
query.bindValue(":cid", case_id)
|
||||
if not query.exec():
|
||||
query_error(query)
|
||||
self.docketWidget.clearContents()
|
||||
row = 0
|
||||
while query.next():
|
||||
print(query.value(0), query.value(1), query.value(2), query.value(3))
|
||||
item = QTableWidgetItem()
|
||||
item.setData(Qt.ItemDataRole.DisplayRole, query.value(2))
|
||||
self.docketWidget.setItem(row,0, item)
|
||||
item = QTableWidgetItem()
|
||||
item.setData(Qt.ItemDataRole.DisplayRole, query.value(3))
|
||||
self.docketWidget.setItem(row, 1, item)
|
||||
row += 1
|
||||
self.docketWidget.setRowCount(row)
|
||||
|
||||
return
|
||||
|
||||
@Slot(QModelIndex)
|
||||
def rowClicked(self, index:QModelIndex) -> None:
|
||||
if not index.isValid():
|
||||
raise Exception("Bad index")
|
||||
docket = index.siblingAtColumn(1).data()
|
||||
print(docket)
|
||||
self.docketLabel.setText(docket)
|
||||
self.show_entries.emit(index.siblingAtColumn(0).data())
|
||||
self.populateDocket(index.siblingAtColumn(0).data())
|
||||
model = docketModel(index.siblingAtColumn(0).data())
|
||||
self.docketView.setModel(model)
|
||||
self.docketView.resizeColumnToContents(0)
|
||||
self.docketView.resizeRowsToContents()
|
||||
return
|
||||
|
||||
@Slot()
|
||||
def on_updateButton_clicked(self):
|
||||
text = self.docketInput.toPlainText()
|
||||
update_db(text)
|
||||
self.update()
|
||||
return
|
||||
|
||||
SQL_CMDS = [
|
||||
@@ -107,6 +85,7 @@ SQL_CMDS = [
|
||||
"docket_id TEXT, "
|
||||
"linked INTEGER, "
|
||||
"petitioners TEXT, respondents TEXT, date INTEGER, "
|
||||
"active INTEGER, "
|
||||
"FOREIGN KEY(linked) REFERENCES cases(case_id))",
|
||||
#
|
||||
"CREATE TABLE IF NOT EXISTS entries ("
|
||||
@@ -141,7 +120,9 @@ def schema_update(db: QSqlDatabase) -> None:
|
||||
table_name = matches.group(2)
|
||||
create_cmd = (
|
||||
matches.group(1)
|
||||
+ '"'
|
||||
+ matches.group(2)
|
||||
+ '"'
|
||||
+ matches.group(3)
|
||||
)
|
||||
else:
|
||||
@@ -173,7 +154,6 @@ def schema_update(db: QSqlDatabase) -> None:
|
||||
# Step 4 create new table
|
||||
new_table_name = table_name + "_new"
|
||||
sql = matches.group(1) + new_table_name + matches.group(3)
|
||||
print(sql)
|
||||
if not query.exec(sql):
|
||||
query_error(query)
|
||||
# step 5 transfer content
|
||||
@@ -228,7 +208,7 @@ def update_proceedings(case_id: int, bs: BeautifulSoup) -> None:
|
||||
if not query.next():
|
||||
query.prepare("INSERT INTO entries (case_id, date, text) VALUES (:cid,:date,:text)")
|
||||
query.bindValue(':cid', case_id)
|
||||
query.bindValue(':date', date.timestamp)
|
||||
query.bindValue(':date', date.timestamp())
|
||||
query.bindValue(':text', text)
|
||||
if not query.exec():
|
||||
query_error(query)
|
||||
@@ -238,7 +218,6 @@ def update_proceedings(case_id: int, bs: BeautifulSoup) -> None:
|
||||
tr = trs.pop(0)
|
||||
assert isinstance(tr, Tag)
|
||||
assert isinstance(tr.contents[1], Tag)
|
||||
print(tr.contents[1])
|
||||
for a in tr.contents[1]:
|
||||
assert isinstance(a, Tag)
|
||||
url = a.attrs['href']
|
||||
@@ -256,7 +235,6 @@ def update_proceedings(case_id: int, bs: BeautifulSoup) -> None:
|
||||
query.bindValue(":url", url)
|
||||
if not query.exec():
|
||||
query_error(query)
|
||||
break
|
||||
return
|
||||
|
||||
def update_db(case_id) -> int:
|
||||
@@ -315,8 +293,6 @@ def update_db(case_id) -> int:
|
||||
assert isinstance(tr, Tag) and isinstance(tr.contents[0], Tag)
|
||||
linked = tr.contents[0].string
|
||||
|
||||
print(docket_id, petitioners, respondent, date, linked)
|
||||
|
||||
#
|
||||
# See if this case already exists.
|
||||
#
|
||||
@@ -376,8 +352,8 @@ def main() -> int:
|
||||
db.setDatabaseName("scotus.db")
|
||||
db.open()
|
||||
schema_update(db)
|
||||
#update_db('24-203')
|
||||
#update_db('23A1058')
|
||||
update_db('24-203')
|
||||
update_db('23A1058')
|
||||
window = MainWindow()
|
||||
return app.exec()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user