Server Messages unpublish

* changes unpublish_on to optional value
* simplifies fetching of server message news
pull/319/head
mpeltriaux 2 years ago
parent e4a2a0f64e
commit 16e72c3372

@ -38,13 +38,7 @@ def home_view(request: HttpRequest):
user_teams = user.shared_teams
# Fetch the four newest active and published ServerMessages
msgs = ServerMessage.objects.filter(
is_active=True,
publish_on__lte=now,
unpublish_on__gte=now,
).order_by(
"-publish_on"
)[:3]
msgs = ServerMessage.get_current_news()[:3]
# First fetch all valid objects (undeleted, only newest versions)
interventions = Intervention.objects.filter(

@ -0,0 +1,18 @@
# Generated by Django 3.1.3 on 2023-03-24 06:20
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('news', '0002_auto_20220114_0936'),
]
operations = [
migrations.AlterField(
model_name='servermessage',
name='unpublish_on',
field=models.DateTimeField(blank=True, null=True),
),
]

@ -1,4 +1,5 @@
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from konova.models import BaseResource
@ -22,7 +23,7 @@ class ServerMessage(BaseResource):
body = models.TextField()
is_active = models.BooleanField(default=True)
publish_on = models.DateTimeField()
unpublish_on = models.DateTimeField()
unpublish_on = models.DateTimeField(null=True, blank=True)
importance = models.CharField(max_length=100, choices=ServerMessageImportance.choices)
def save(self, user=None, *args, **kwargs):
@ -34,3 +35,24 @@ class ServerMessage(BaseResource):
self.modified = UserActionLogEntry.get_edited_action(user)
super().save(*args, **kwargs)
@staticmethod
def get_current_news():
""" Getter for the most current news
Meaning the ones that are
* activated
* publish_on has passed
* unpublish_on (if set) has not been passed, yet
"""
now = timezone.now()
news = ServerMessage.objects.filter(
is_active=True,
publish_on__lte=now,
).exclude(
unpublish_on__lte=now,
).order_by(
"-publish_on"
)
return news

@ -20,14 +20,7 @@ def index_view(request: HttpRequest):
"""
template = "news/index.html"
now = timezone.now()
news = ServerMessage.objects.filter(
is_active=True,
publish_on__lte=now,
unpublish_on__gte=now
).order_by(
"-publish_on"
)
news = ServerMessage.get_current_news()
context = {
"news": news,

Loading…
Cancel
Save