Skip to content

Commit

Permalink
Merge pull request #2144 into dev
Browse files Browse the repository at this point in the history
This pull request switches from IDs to a unique, user-defined
slug. All existing news items will be assigned their id as the slug,
so paths will remain the same. For example
https://physionet.org/news/post/1 will continue to resolve after the
migration.
  • Loading branch information
Benjamin Moody committed Dec 6, 2023
2 parents 38e0408 + e4833e2 commit bcc014e
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 15 deletions.
2 changes: 1 addition & 1 deletion physionet-django/console/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ class NewsForm(forms.ModelForm):

class Meta:
model = News
fields = ('title', 'content', 'url', 'project', 'front_page_banner')
fields = ('slug', 'title', 'content', 'url', 'project', 'front_page_banner')


class FeaturedForm(forms.Form):
Expand Down
2 changes: 1 addition & 1 deletion physionet-django/console/templates/console/news_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<td>{{ news.publish_datetime }}</td>
<td>{% if news.url %}<a href='{{ news.url }}'>{{ news.url }}</a>{% else %}None{% endif %}</td>
<td>{% if news.front_page_banner %}<span class="fa fa-check"><span class="visually-hidden">&#10004;</span></span>{% endif %}</td>
<td><a href="{% url 'news_edit' news.id %}" class="btn btn-sm btn-primary" role="button">Edit</a></td>
<td><a href="{% url 'news_edit' news.slug %}" class="btn btn-sm btn-primary" role="button">Edit</a></td>
</tr>
{% endfor %}
3 changes: 2 additions & 1 deletion physionet-django/console/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
path('news/', views.news_console, name='news_console'),
path('news/add/', views.news_add, name='news_add'),
path('news/search/', views.news_search, name='news_search'),
path('news/edit/<news_id>/', views.news_edit, name='news_edit'),
path('news/edit/<news_slug>/', views.news_edit, name='news_edit'),

path('featured/', views.featured_content, name='featured_content'),
path('featured/add', views.add_featured, name='add_featured'),
Expand Down Expand Up @@ -169,6 +169,7 @@
'section_pk': 1,
'news_id': 1,
'username': 'rgmark',
'news_slug': 'cloud-migration',
}
TEST_CASES = {
'manage_published_project': {
Expand Down
4 changes: 2 additions & 2 deletions physionet-django/console/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1929,9 +1929,9 @@ def news_search(request):


@permission_required('notification.change_news', raise_exception=True)
def news_edit(request, news_id):
def news_edit(request, news_slug):
try:
news = News.objects.get(id=news_id)
news = News.objects.get(slug=news_slug)
except News.DoesNotExist:
raise Http404()
saved = False
Expand Down
9 changes: 6 additions & 3 deletions physionet-django/notification/fixtures/demo-notification.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"title": "New Platform Nearing Completion",
"content": "<p>We&#39;ve been working very hard on the PhysioNet rebuild. The staging site is available at <a href=\"https://staging.physionet.org/\">https://staging.physionet.org/</a>. We hope to release the beta version of the new site by November 2018.</p>",
"publish_datetime": "2018-07-05T19:47:15.285Z",
"url": "https://github.com/MIT-LCP/physionet-build/"
"url": "https://github.com/MIT-LCP/physionet-build/",
"slug": "new-platform"
}
},
{
Expand All @@ -16,7 +17,8 @@
"title": "Cloud Migration",
"content": "<p>PhysioNet may potentially partner with Google and host its data on Google Cloud Platform.</p>",
"publish_datetime": "2018-08-10T19:50:31.362Z",
"url": ""
"url": "",
"slug": "cloud-migration"
}
},
{
Expand All @@ -26,7 +28,8 @@
"title": "Preparing New Challenges",
"content": "<p>We are considering hosting challenges on Kaggle. This would free up users to use their own computing resources and choice of programming systems. Because submissions will not have their code run automatically, we will be vigilant in ensuring the quality of submissions. Unusable code will see their submissions penalized and/or disqualified.</p>",
"publish_datetime": "2018-08-17T19:53:33.258Z",
"url": "https://www.kaggle.com/"
"url": "https://www.kaggle.com/",
"slug": "new-challenges"
}
}
]
25 changes: 25 additions & 0 deletions physionet-django/notification/migrations/0008_news_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.1.10 on 2023-11-29 19:39

from django.db import migrations, models


def set_default_slugs(apps, schema_editor):
News = apps.get_model('notification', 'News')
for news in News.objects.all():
news.slug = str(news.id)
news.save()


class Migration(migrations.Migration):
dependencies = [
("notification", "0007_auto_20220221_0332"),
]

operations = [
migrations.AddField(
model_name="news",
name="slug",
field=models.SlugField(max_length=100, null=True),
),
migrations.RunPython(set_default_slugs, reverse_code=migrations.RunPython.noop),
]
18 changes: 18 additions & 0 deletions physionet-django/notification/migrations/0009_alter_news_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.10 on 2023-11-29 20:12

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("notification", "0008_news_slug"),
]

operations = [
migrations.AlterField(
model_name="news",
name="slug",
field=models.SlugField(max_length=100, unique=True),
preserve_default=False,
),
]
2 changes: 2 additions & 0 deletions physionet-django/notification/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class News(models.Model):
"""
Model to record news and announcements.
"""
title = models.CharField(max_length=100)
content = SafeHTMLField()
Expand All @@ -16,6 +17,7 @@ class News(models.Model):
on_delete=models.SET_NULL, related_name='news')
guid = models.CharField(max_length=64, default=uuid.uuid4)
front_page_banner = models.BooleanField(default=False)
slug = models.SlugField(max_length=100, unique=True)

class Meta:
default_permissions = ('change',)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h1>{{ year }} News</h1>

{% for news in news_pieces %}
<h2>
<a href="{% url 'news_by_id' news.id %}">{{ news.title }}</a>
<a href="{% url 'news_by_slug' news.slug %}">{{ news.title }}</a>
</h2>
{% include "notification/news_content.html" %}
<hr>
Expand Down
3 changes: 2 additions & 1 deletion physionet-django/notification/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
urlpatterns = [
path('news/', views.news, name='news'),
path('news/<int:year>/', views.news_year, name='news_year'),
path('news/post/<int:news_id>', views.news_by_id, name='news_by_id'),
path('news/post/<news_slug>', views.news_by_slug, name='news_by_slug'),
path('feed.xml', views.news_rss, name='news_rss'),
]

# Parameters for testing URLs (see physionet/test_urls.py)
TEST_DEFAULTS = {
'year': '2018',
'news_id': '1',
'news_slug': 'cloud-migration',
}
6 changes: 4 additions & 2 deletions physionet-django/notification/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ def news_year(request, year):
{'year': year, 'news_pieces': news_pieces,
'news_years': news_years})

def news_by_id(request, news_id, max_items=20):

def news_by_slug(request, news_slug, max_items=20):
"""
Get a specific news item
"""
try:
news = News.objects.get(id=news_id)
news = News.objects.get(slug=news_slug)
# The year range of all the PN news in existence.
minmax = News.objects.all().aggregate(min=Min('publish_datetime'),
max=Max('publish_datetime'))
Expand All @@ -60,6 +61,7 @@ def news_by_id(request, news_id, max_items=20):
except News.DoesNotExist:
raise Http404()


def news_rss(request, max_items=100):
news_pieces = News.objects.order_by('-publish_datetime')[:max_items]
feed_date = news_pieces[0].publish_datetime
Expand Down
3 changes: 2 additions & 1 deletion physionet-django/physionet/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@
'dua_slug': 'physionet-credentialed-health-data-dua',
'event_slug': 'iLII4L9jSDFh',
'license_slug': 'open-data-commons-attribution-license-v10',
'static_url': 'publish'
'static_url': 'publish',
'news_slug': 'cloud-migration',
}
TEST_CASES = {
'lightwave_server_compat': {
Expand Down
4 changes: 2 additions & 2 deletions physionet-django/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<ul class="list-unstyled front-page-banner">
{% for news in front_page_banner %}
<li>
<a href="{% url 'news_by_id' news.id %}">{{ news.title }}</a>
<a href="{% url 'news_by_slug' news.slug %}">{{ news.title }}</a>
</li>
{% endfor %}
</ul>
Expand Down Expand Up @@ -70,7 +70,7 @@ <h2>
<br>
{% for news in news_pieces %}
<h3>
<a href="{% url 'news_by_id' news.id %}">{{ news.title }}</a>
<a href="{% url 'news_by_slug' news.slug %}">{{ news.title }}</a>
</h3>
{% include "notification/news_content.html" %}
<hr>
Expand Down

0 comments on commit bcc014e

Please sign in to comment.