News are now pinned for 1 month and events for 7 days.

This commit is contained in:
2024-08-21 16:59:27 +02:00
parent 4134ad67d5
commit f121239d31
3 changed files with 48 additions and 14 deletions

View File

@@ -1,3 +1,5 @@
import datetime
from django.db import models
from django.db.models import Case, Q, When
from django.utils import timezone
@@ -80,7 +82,32 @@ class ArticleManager(PublishedManager, models.Manager):
return self.published(public)
def pinned(self, public=True):
return self.published(public).filter(is_pinned=True).first()
# Get date for pinned news that is max 1 month old.
post_date = datetime.date.today()
__month = post_date.month
__year = post_date.year
if __month != 0:
__month -= 1
else:
# If the current month is January, you get the date from December of previous year.
__month = 12
__year -= 1
post_date = post_date.replace(year=__year, month=__month)
# Get date for event posts that is max 7 days old.
event_date = datetime.date.today() - datetime.timedelta(7)
return (
self.published(public)
.filter(is_pinned=True)
.filter(
(Q(post_type="N") & Q(public_date__gt=post_date))
| (Q(post_type="E") & Q(event_end__date__gt=event_date))
)
.first()
)
class NewsManager(PublishedManager, models.Manager):