Skip to content

Commit

Permalink
Allow news items to be linked to all versions of a project (#2186)
Browse files Browse the repository at this point in the history
Currently, news items can only be linked to a single version of a
published project, but in many or most cases we would actually like news
items to link to all versions of a published project.

This pull request adds a flag to the news model that allows a news item
to be linked to all versions of a project. Specifically, the changes
are:

- Add a `link_all_versions` flag to the News model
- Allow the flag to be set in the form that is used to create news items
- Adds a `get_all_news()` method to the `PublishedProject` model that
makes it straightforward to retrieve all news items for the project
(both news items that are directly linked, and news items that are
linked through the `link_all_versions` flag).
- Adds a migration that updates the News model. 

There may be better ways to implement this functionality, e.g. by:

- linking the News model to a CoreProject instead of PublishedProject
- setting a validator that requires linked CoreProjects to be associated
with a PublishedProject (so that news items can only be added for
published projects)
- adding `project_version` to the News item
- updating the form used to add news items so that the CoreProject and
version (or "all versions" can be set).

I would prefer to stick with the implementation in this pull request for
timeliness (so that @briangow can respond to a user request). We can
review and improve the approach in future.
  • Loading branch information
tompollard authored Jan 31, 2024
2 parents 9492804 + b0cb2ac commit 69a2259
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion physionet-django/console/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ class NewsForm(forms.ModelForm):

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


class FeaturedForm(forms.Form):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.1.10 on 2024-01-30 20:50

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("notification", "0009_alter_news_slug"),
]

operations = [
migrations.AddField(
model_name="news",
name="link_all_versions",
field=models.BooleanField(
default=False,
help_text="Check this to link the news item to all versions of the selected project",
),
),
]
13 changes: 11 additions & 2 deletions physionet-django/notification/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ class News(models.Model):
content = SafeHTMLField()
publish_datetime = models.DateTimeField(auto_now_add=True)
url = models.URLField(default='', blank=True)
project = models.ForeignKey('project.PublishedProject', null=True, blank=True,
on_delete=models.SET_NULL, related_name='news')
project = models.ForeignKey(
'project.PublishedProject',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='news'
)
link_all_versions = models.BooleanField(
default=False,
help_text='Check this to link the news item to all versions of the selected project'
)
guid = models.CharField(max_length=64, default=uuid.uuid4)
front_page_banner = models.BooleanField(default=False)
slug = models.SlugField(max_length=100, unique=True)
Expand Down
18 changes: 18 additions & 0 deletions physionet-django/project/modelcomponents/publishedproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from django.urls import reverse
from django.utils import timezone
from django.utils.text import slugify

from notification.models import News
from project.managers.publishedproject import PublishedProjectManager
from project.modelcomponents.access import DataAccessRequest, DataAccessRequestReviewer, DUASignature
from project.modelcomponents.fields import SafeHTMLField
Expand Down Expand Up @@ -377,3 +379,19 @@ def embargo_active(self):
return True
else:
return False

def get_all_news(self):
"""
Return all news items associated with this project. If any news item has
'link_all_versions' set to True, include those news too.
"""
# Fetch news items directly related to this PublishedProject
direct_news = self.news.all()

# Fetch news items related to other PublishedProjects where the
# link_all_versions flag is set to True
linked_news = News.objects.filter(
project__core_project=self.core_project,
link_all_versions=True).exclude(project=self)

return direct_news | linked_news
2 changes: 1 addition & 1 deletion physionet-django/project/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,7 @@ def published_project(request, project_slug, version, subdir=''):
topics = project.topics.all()
languages = project.programming_languages.all()
contact = project.contact
news = project.news.all().order_by('-publish_datetime')
news = project.get_all_news().order_by('-publish_datetime')
parent_projects = project.parent_projects.all()
# derived_projects = project.derived_publishedprojects.all()
data_access = DataAccess.objects.filter(project=project)
Expand Down

0 comments on commit 69a2259

Please sign in to comment.