diff --git a/CHANGELOG.md b/CHANGELOG.md index eab0290e9..2288f591c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ --- ## Neste versjon - +- ✨ **Bannere** Filtrering på bannere - ✨ **Spørreskjemaer** NOK medlemmer kan lage spørreskjema. - ⚡ **Bruker** Nå kan ikke HS lenger endre eller slette brukere. - ⚡ **Mails** Nå logger vi på eposttjeneren kun en gang per batch med epost som sendes. diff --git a/app/communication/migrations/0008_alter_banner_options.py b/app/communication/migrations/0008_alter_banner_options.py new file mode 100644 index 000000000..4d494d66e --- /dev/null +++ b/app/communication/migrations/0008_alter_banner_options.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.5 on 2023-10-04 08:01 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("communication", "0007_remove_sent_mails"), + ] + + operations = [ + migrations.AlterModelOptions( + name="banner", + options={"ordering": ("-visible_from",)}, + ), + ] diff --git a/app/communication/models/banner.py b/app/communication/models/banner.py index 37380568a..f40fb1dde 100644 --- a/app/communication/models/banner.py +++ b/app/communication/models/banner.py @@ -44,7 +44,7 @@ def save(self, *args, **kwargs): super().save(*args, **kwargs) class Meta: - ordering = ("-updated_at",) + ordering = ("-visible_from",) @property def exists_overlapping_banners(self): @@ -57,6 +57,14 @@ def exists_overlapping_banners(self): .exists() ) + @property + def is_expired(self): + return self.visible_until < now() + + @property + def is_visible(self): + return self.visible_from <= now() <= self.visible_until + @classmethod def has_visible_permission(cls, request): return True diff --git a/app/communication/serializers/banner.py b/app/communication/serializers/banner.py index e78c2fa9c..66c8149b3 100644 --- a/app/communication/serializers/banner.py +++ b/app/communication/serializers/banner.py @@ -17,4 +17,6 @@ class Meta: "updated_at", "image", "image_alt", + "is_visible", + "is_expired", ) diff --git a/app/communication/views/banner.py b/app/communication/views/banner.py index dabed7a16..4dd0ddd3c 100644 --- a/app/communication/views/banner.py +++ b/app/communication/views/banner.py @@ -1,3 +1,6 @@ +from django_filters import BooleanFilter +from django_filters.rest_framework import DjangoFilterBackend +from django_filters.rest_framework.filterset import FilterSet from rest_framework import status from rest_framework.decorators import action from rest_framework.response import Response @@ -10,12 +13,34 @@ from app.util.utils import now +class BannerFilter(FilterSet): + is_visible = BooleanFilter(method="filter_is_visible") + is_expired = BooleanFilter(method="filter_is_expired") + + class Meta: + model = Banner + fields = ["is_visible", "is_expired"] + + def filter_is_visible(self, queryset, name, value): + if value: + return queryset.filter(visible_from__lte=now(), visible_until__gte=now()) + return queryset + + def filter_is_expired(self, queryset, name, value): + if value: + return queryset.filter(visible_until__lt=now()) + return queryset + + class BannerViewSet(BaseViewSet): serializer_class = BannerSerializer pagination_class = BasePagination queryset = Banner.objects.all() permission_classes = [BasicViewPermission] + filter_backends = [DjangoFilterBackend] + filterset_class = BannerFilter + @action( detail=False, methods=["get"],