diff --git a/.gitignore b/.gitignore index 72cf1943..52a1ecc4 100644 --- a/.gitignore +++ b/.gitignore @@ -54,4 +54,6 @@ pmg/static/stylesheets/ bin/*.csv src/ -.vscode \ No newline at end of file +.vscode + +pmg/static/bill-tracker.json \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 87f6f5e7..997fa597 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/pmg/api/v2.py b/pmg/api/v2.py index 1913ca0a..6e00d8ce 100644 --- a/pmg/api/v2.py +++ b/pmg/api/v2.py @@ -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) diff --git a/pmg/bill_tracker.py b/pmg/bill_tracker.py new file mode 100644 index 00000000..bf03d908 --- /dev/null +++ b/pmg/bill_tracker.py @@ -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) diff --git a/pmg/tasks.py b/pmg/tasks.py index 419f5f72..5d789cec 100644 --- a/pmg/tasks.py +++ b/pmg/tasks.py @@ -4,6 +4,7 @@ from flask_script import Command from pmg import db import logging +import datetime log = logging.getLogger(__name__) @@ -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 @@ -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",