Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eas 265 alert atom feed #183

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repos:
- id: check-yaml
- id: debug-statements
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
rev: 7.0.0
hooks:
- id: flake8
name: flake8 (python)
Expand Down
3 changes: 3 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ class Config(object):
ENCRYPTION_DANGEROUS_SALT = os.environ.get("ENCRYPTION_DANGEROUS_SALT")

ADMIN_BASE_URL = "http://localhost:6012"
ADMIN_EXTERNAL_URL = "http://localhost:6012"
API_HOST_NAME = "http://localhost:6011"
API_RATE_LIMIT_ENABLED = True
GOVUK_EXTERNAL_URL = "http://127.0.0.1:6017"

CBC_ACCOUNT_NUMBER = os.getenv("CBC_ACCOUNT_NUMBER")
CBC_PROXY_ENABLED = True
Expand Down Expand Up @@ -315,6 +317,7 @@ class Hosted(Config):
REDIS_URL = f"redis://api.{TENANT}ecs.local:6379/0"
API_HOST_NAME = f"http://api.{TENANT}ecs.local:6011"
TEMPLATE_PREVIEW_API_HOST = f"http://api.{TENANT}ecs.local:6013"
# GOVUK_EXTERNAL_URL = f"https://www.{SUBDOMAIN}.publishing.service.gov.uk/alerts"
if os.getenv("MASTER_USERNAME"):
print("Using master credentials for db connection")
filtered_password = os.environ.get("MASTER_PASSWORD").replace("%", "%%")
Expand Down
8 changes: 5 additions & 3 deletions app/dao/broadcast_message_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def dao_get_broadcast_provider_messages_by_broadcast_message_id(broadcast_messag
)


def dao_get_all_broadcast_messages():
def dao_get_all_broadcast_messages(
status_filter=BroadcastStatusType.LIVE_STATUSES, get_from=datetime(2021, 5, 25, 0, 0, 0)
):
return (
db.session.query(
BroadcastMessage.id,
Expand All @@ -77,9 +79,9 @@ def dao_get_all_broadcast_messages():
)
.join(ServiceBroadcastSettings, ServiceBroadcastSettings.service_id == BroadcastMessage.service_id)
.filter(
BroadcastMessage.starts_at >= datetime(2021, 5, 25, 0, 0, 0),
BroadcastMessage.starts_at >= get_from,
BroadcastMessage.stubbed == False, # noqa
BroadcastMessage.status.in_(BroadcastStatusType.LIVE_STATUSES),
BroadcastMessage.status.in_(status_filter),
)
.order_by(desc(BroadcastMessage.starts_at))
.all()
Expand Down
58 changes: 51 additions & 7 deletions app/govuk_alerts/rest.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from flask import Blueprint, jsonify
import time
from datetime import datetime, timedelta

from feedwerk.atom import AtomFeed
from flask import Blueprint, current_app, jsonify

from app.dao.broadcast_message_dao import dao_get_all_broadcast_messages
from app.errors import register_errors
from app.utils import get_dt_string_or_none

govuk_alerts_blueprint = Blueprint(
"govuk-alerts",
__name__,
url_prefix="/govuk-alerts",
)
govuk_alerts_blueprint = Blueprint("govuk-alerts", __name__)

register_errors(govuk_alerts_blueprint)


@govuk_alerts_blueprint.route("")
@govuk_alerts_blueprint.route("/govuk-alerts")
def get_broadcasts():
broadcasts = dao_get_all_broadcast_messages()
broadcasts_dict = {
Expand All @@ -34,3 +34,47 @@ def get_broadcasts():
]
}
return jsonify(broadcasts_dict), 200


@govuk_alerts_blueprint.route("/govuk-atom")
def get_atom_feed():
broadcasts = dao_get_all_broadcast_messages(get_from=datetime.now() - timedelta(days=30))

url = current_app.config["GOVUK_EXTERNAL_URL"]
feed_url = current_app.config["GOVUK_EXTERNAL_URL"] + "/alerts.atom"

feed = AtomFeed(title="Emergency Alerts", feed_url=feed_url, url=url)

for broadcast in broadcasts:
feed.add(
broadcast.reference,
broadcast.content,
content_type="html",
author="GOV.UK",
url=url + "/alerts/current-alerts",
published=broadcast.starts_at,
updated=broadcast.starts_at,
)

# add dummy posts, for testing
feed.add(
str(time.time()),
"Alert message text",
content_type="html",
author="GOV.UK",
url=url + "/alerts/23-feb-2024",
published=datetime.now() - timedelta(minutes=1),
updated=datetime.now() - timedelta(minutes=6),
)

feed.add(
str(time.time()),
"Alert message goes here",
content_type="html",
author="GOV.UK",
url=url + "/alerts/15-feb-2024",
published=datetime.now() - timedelta(minutes=10),
updated=datetime.now() - timedelta(minutes=12),
)

return feed.get_response(), 200
6 changes: 3 additions & 3 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SQLAlchemy==1.4.41
cachetools==5.2.0
beautifulsoup4==4.11.1
lxml==4.9.1

feedwerk==1.2.0
notifications-python-client==8.0.0

# gds-metrics requires prometheseus 0.2.0, override that requirement as 0.7.1 brings significant performance gains
Expand All @@ -31,8 +31,8 @@ git+https://github.com/alphagov/gds_metrics_python.git@6f1840a57b6fb1ee40b7e84f2

# Package for testing
black==22.8.0
flake8==5.0.4
flake8-bugbear==22.9.23
flake8==7.0.0
flake8-bugbear==22.10.27
flaky==3.7.0
isort==5.10.1
moto==4.0.11
Expand Down
12 changes: 8 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ eventlet==0.33.1
# via gunicorn
execnet==1.9.0
# via pytest-xdist
flake8==5.0.4
feedwerk==1.2.0
# via -r requirements.in
flake8==7.0.0
# via
# -r requirements.in
# flake8-bugbear
flake8-bugbear==22.9.23
flake8-bugbear==22.10.27
# via -r requirements.in
flaky==3.7.0
# via -r requirements.in
Expand Down Expand Up @@ -149,6 +151,7 @@ mako==1.2.3
# via alembic
markupsafe==2.1.1
# via
# feedwerk
# jinja2
# mako
# moto
Expand Down Expand Up @@ -191,13 +194,13 @@ py==1.11.0
# via
# pytest
# pytest-forked
pycodestyle==2.9.1
pycodestyle==2.11.1
# via flake8
pycparser==2.21
# via cffi
pycurl==7.44.1
# via kombu
pyflakes==2.5.0
pyflakes==3.2.0
# via flake8
pyjwt==2.5.0
# via
Expand Down Expand Up @@ -297,6 +300,7 @@ webcolors==1.12
# via jsonschema
werkzeug==2.2.2
# via
# feedwerk
# flask
# moto
xmltodict==0.13.0
Expand Down