Skip to content

Commit

Permalink
Adds support for updating DB records
Browse files Browse the repository at this point in the history
  • Loading branch information
captn3m0 committed Aug 13, 2024
1 parent 356676a commit c4c4012
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,16 @@ def get_cases(
elif filing_number and year:
ecourt.search_by_filing_number(filing_number, year)
elif act_type and status !="Both":
extra_fields = {
"status": status,
"act_type": act_type
}
cases = list(ecourt.ActType(act_type, status))
elif case_type and status!="Both":
extra_fields = {
"status": status,
"case_type": case_type
}
cases = list(ecourt.search_by_case_type(case_type, year, status))
else:
click.echo(
Expand All @@ -118,7 +126,7 @@ def get_cases(
idx = 0
for case in cases:
res = {}
fields = ['case_type', 'registration_number', 'cnr_number', 'name']
fields = ['registration_number', 'cnr_number', 'name']
for field in fields:
value = getattr(case, field)
if value:
Expand All @@ -131,7 +139,7 @@ def get_cases(
idx+=1

if save:
Storage().addCases(court, cases)
Storage().addCases(court, cases, extra_fields)


@ecourts.command()
Expand Down
6 changes: 6 additions & 0 deletions src/entities/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def __post_init__(self):
self.decision_date = parse_date(self.decision_date)
if isinstance(self.filing_date, str):
self.filing_date = parse_date(self.filing_date)
if self.orders == None:
self.orders = []
if self.hearings == None:
self.hearings = []
if self.objections == None:
self.objections = []
# The canonical representation of a CNR is without hyphens
self.cnr_number = self.cnr_number.replace("-", "")
if len(self.cnr_number) !=16:
Expand Down
21 changes: 16 additions & 5 deletions src/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,20 @@ def addCourts(self, records: list[Court]):
self.conn.commit()

#TODO: Move storage to under ecourts.storage so we get court information from there
def addCases(self, court: Court, records: list[Case]):
for record in records:
self.cursor.execute(
"INSERT OR IGNORE INTO cases VALUES (?, ?, ?)", (court.state_code, court.court_code or "1", json.dumps(record.json(), default=str))
)
def addCases(self, court: Court, cases: list[Case], extra_fields: dict={}):
for case in cases:
# search for the record using CNR
# if found, update the record
# if not found, insert the record
search_result = self.cursor.execute(
"SELECT * FROM cases WHERE json_extract(value, '$.cnr_number') = ?", (case.cnr_number,)
).fetchone()
if search_result:
self.cursor.execute(
"UPDATE cases SET value = ? WHERE json_extract(value, '$.cnr_number') = ?", (json.dumps(case.json() | extra_fields, default=str), case.cnr_number)
)
else:
self.cursor.execute(
"INSERT INTO cases VALUES (?, ?, ?)", (court.state_code, court.court_code or "1", json.dumps(case.json() | extra_fields, default=str))
)
self.conn.commit()

0 comments on commit c4c4012

Please sign in to comment.