mirror of
https://github.com/jointakahe/takahe.git
synced 2024-12-27 15:40:29 +00:00
8b3106b852
Fixes #377
45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
from django.db import models
|
|
from django.utils import timezone
|
|
|
|
from users.models import Announcement, User
|
|
|
|
|
|
class AnnouncementService:
|
|
"""
|
|
Handles viewing and dismissing announcements
|
|
"""
|
|
|
|
def __init__(self, user: User):
|
|
self.user = user
|
|
|
|
@classmethod
|
|
def visible_queryset(cls) -> models.QuerySet[Announcement]:
|
|
"""
|
|
Common visibility query
|
|
"""
|
|
now = timezone.now()
|
|
return Announcement.objects.filter(
|
|
models.Q(start__lte=now) | models.Q(start__isnull=True),
|
|
models.Q(end__gte=now) | models.Q(end__isnull=True),
|
|
published=True,
|
|
).order_by("-start", "-created")
|
|
|
|
@classmethod
|
|
def visible_anonymous(cls) -> models.QuerySet[Announcement]:
|
|
"""
|
|
Returns all announcements marked as being showable to all visitors
|
|
"""
|
|
return cls.visible_queryset().filter(include_unauthenticated=True)
|
|
|
|
def visible(self) -> models.QuerySet[Announcement]:
|
|
"""
|
|
Returns all announcements that are currently valid and should be shown
|
|
to a given user.
|
|
"""
|
|
return self.visible_queryset().exclude(seen=self.user)
|
|
|
|
def mark_seen(self, announcement: Announcement):
|
|
"""
|
|
Marks an announcement as seen by the user
|
|
"""
|
|
announcement.seen.add(self.user)
|