Skip to content

Commit

Permalink
Merge pull request #555 from OpenUpSA/feature/api-bill-tracker
Browse files Browse the repository at this point in the history
Feature/api bill tracker
  • Loading branch information
paulmwatson authored Jul 30, 2024
2 parents 069e6f7 + 64bc78e commit a1c46f9
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ pmg/static/stylesheets/

bin/*.csv
src/
.vscode
.vscode

pmg/static/bill-tracker.json
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
- ES_SERVER=http://elastic:9200
- GOOGLE_ANALYTICS_ID=G-XN8MJJNSEE
- GOOGLE_TAG_MANAGER_ID=GTM-MMCNPLT6
- RUN_PERIODIC_TASKS=true
ports:
- "5000:5000"
command: python app.py runserver
Expand Down
10 changes: 10 additions & 0 deletions pmg/api/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,13 @@ def daily_schedules(id=None):
return api_get_item(id, DailySchedule, DailyScheduleSchema)
else:
return api_list_items(DailySchedule.list(), DailyScheduleSchema)


@api.route("/bill-tracker")
def bill_tracker():
# Return background-worker produced static JSON file from pmg/static/bill-tracker.json
try:
with open("pmg/static/bill-tracker.json", "r") as f:
return f.read()
except FileNotFoundError:
return abort(404)
60 changes: 60 additions & 0 deletions pmg/bill_tracker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import json
import datetime
from pmg.models.resources import Bill


# Simple export of bill data to pmg/static/bill-tracker.json run by APScheduler every morning at 1am
def produce_bill_tracker_json():
bills = []
for bill in Bill.query.order_by(Bill.year).limit(1000000000).all():
billDict = {
"id": bill.id,
"title": bill.title,
"type": bill.type.name,
"status": "draft",
"year": bill.year,
"introduced_by": bill.introduced_by,
"date_of_introduction": bill.date_of_introduction,
"events": [],
"versions": [],
}

if bill.status:
billDict["status"] = bill.status.name

# Order bill.events by event.date
events = sorted(bill.events, key=lambda x: x.date)
for event in events:
bill_event = {
"id": event.id,
"title": event.title,
"date": event.date,
"type": event.type,
"public_participation": event.public_participation,
}
if event.member:
bill_event["member"] = event.member.name

if event.type in ["bill-signed", "bill-act-commenced", "bill-enacted"]:
bill_event["house"] = "President"
elif event.house:
bill_event["house"] = event.house.name_short
elif event.committee:
bill_event["house"] = event.committee.house.name_short

if event.committee:
bill_event["committee"] = event.committee.name

billDict["events"].append(bill_event)

for version in bill.versions:
bill_version = {
"id": version.id,
"date": version.date,
"title": version.title,
"enacted": version.enacted,
}
billDict["versions"].append(bill_version)
bills.append(billDict)
with open("pmg/static/bill-tracker.json", "w") as f:
json.dump(bills, f, indent=4, ensure_ascii=False, default=str)
18 changes: 18 additions & 0 deletions pmg/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from flask_script import Command
from pmg import db
import logging
import datetime

log = logging.getLogger(__name__)

Expand All @@ -24,6 +25,14 @@ def sync_soundcloud():
SoundcloudTrack.sync()


def produce_bill_tracker_json():
from pmg import app
from pmg.bill_tracker import produce_bill_tracker_json

with app.app_context():
produce_bill_tracker_json()


def schedule(scheduler):
jobs = [
# Schedule background task for sending saved search alerts every
Expand All @@ -36,6 +45,15 @@ def schedule(scheduler):
coalesce=True,
hour=3,
),
scheduler.add_job(
"pmg.tasks:produce_bill_tracker_json",
"cron",
id="produce-bill-tracker-json",
replace_existing=True,
coalesce=True,
hour=1,
next_run_time=datetime.datetime.now(),
),
scheduler.add_job(
sync_soundcloud,
"cron",
Expand Down

0 comments on commit a1c46f9

Please sign in to comment.