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

Display latest news from blog on homepage #5016

Merged
merged 4 commits into from
Sep 12, 2024
Merged
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
1 change: 1 addition & 0 deletions deploy/crontab-openprescribing
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
00 05 * * * hello /webapps/openprescribing/deploy/diskcache_garbage_collect.sh
30 05 * * * hello /webapps/openprescribing/deploy/clearsessions.sh
12 06 * * 1 hello /webapps/openprescribing/deploy/maillog_garbage_collect.sh
*/5 * * * * hello /webapps/openprescribing/deploy/refresh_news_feed_cache.sh
3 changes: 3 additions & 0 deletions deploy/refresh_news_feed_cache.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
. /webapps/openprescribing/.venv/bin/activate
python /webapps/openprescribing/openprescribing/manage.py refresh_news_feed_cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import datetime

import requests

from django.core.cache import cache
from django.core.management.base import BaseCommand


FEED_URL = "https://www.bennett.ox.ac.uk/blog/index.json"


class Command(BaseCommand):
help = f"Fetch and cache news items from:\n{FEED_URL}"

def handle(self, *args, **options):
response = requests.get(FEED_URL)
response.raise_for_status()
feed_data = response.json()
items = [
format_news_item(**item)
for item in feed_data["posts"]
if is_openprescribing_news_item(**item)
]
cache.set("news_feed", items, timeout=60 * 60 * 24 * 365)


def is_openprescribing_news_item(*, categories, tags, **kwargs):
if not categories or "OpenPrescribing" not in categories:
return False
if not tags or "news" not in tags:
return False
return True


def format_news_item(*, date, **kwargs):
return {
"date": datetime.datetime.fromisoformat(date),
**kwargs,
}
12 changes: 12 additions & 0 deletions openprescribing/frontend/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,21 @@ def first_or_none(lst):
return None


def home(request):
return render(
request,
"index.html",
{
"news_feed": cache.get("news_feed", [])[:3],
},
)


##################################################
# BNF sections
##################################################


def all_bnf(request):
sections = Section.objects.filter(is_current=True)
context = {"sections": sections, **get_org_context(request.GET)}
Expand Down
9 changes: 9 additions & 0 deletions openprescribing/media/css/homepage.less
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@
left: 45px;
color: #ccc;
}

.latest-news-item {
margin-bottom: 20px;
img {
display: block;
width: 200px;
border-radius: 6px;
}
}
2 changes: 1 addition & 1 deletion openprescribing/openprescribing/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def all_england_redirects(request, *args, **kwargs):


urlpatterns = [
path(r"", views.home, name="home"),
# Static pages.
path(r"", TemplateView.as_view(template_name="index.html"), name="home"),
path(r"api/", TemplateView.as_view(template_name="api.html"), name="api"),
path(r"about/", TemplateView.as_view(template_name="about.html"), name="about"),
path(r"faq/", TemplateView.as_view(template_name="faq.html"), name="faq"),
Expand Down
34 changes: 32 additions & 2 deletions openprescribing/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{% extends "base.html" %}
{% load template_extras %}
{% load static %}

{% block title %}Home{% endblock %}
{% block active_class %}home{% endblock %}

{% block content %}

<h1>Explore England's prescribing data</h1>

<div class="row">

<div class="col-md-9">

<h1>Explore England's prescribing data</h1>

<p>Every month, the NHS in England publishes <a href="https://digital.nhs.uk/practice-level-prescribing-summary">anonymised data</a> about the drugs prescribed by GPs. But the raw data files are large and unwieldy, with more than 700 million rows. We're making it easier for GPs, managers and everyone to explore - supporting safer, more efficient prescribing.</p>

<p>How to cite: If you use our data or graphs, please cite as <em>OpenPrescribing.net, Bennett Institute for Applied Data Science, University of Oxford, {% current_time "%Y" %}</em> so that others can find us and use our tools.</p>
Expand Down Expand Up @@ -54,7 +56,35 @@ <h3>Spot national trends</h3>
</div>

<div class="col-md-3">
{% include '_mailchimp_signup.html' %}
<h3>Latest News</h3>

{% if news_feed %}

{% for item in news_feed %}

<div class="latest-news-item">
<a href="{{ item.link }}"><img src="{{ item.image }}"></a>
<small class="text-muted">{{ item.date | date:"j M Y" }}</small>
<p><a href="{{ item.link }}">{{ item.title }}</a></p>
</div>

{% endfor %}

{% else %}

<div class="latest-news-item">
<a href="https://www.bennett.ox.ac.uk/openprescribing/blog/">
<img src="{% static 'img/footer-bennett.svg' %}">
</a>
<p style="margin-top: 5px">
<a href="https://www.bennett.ox.ac.uk/openprescribing/blog/">
Read the latest news from OpenPrescribing on the Bennett Institute blog
</a>
</p>
</div>

{% endif %}

</div>

</div>
Expand Down
Loading