Skip to content

Commit

Permalink
Merge branch 'dev' into refactor(payment)/event-payment
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsNyl authored Oct 13, 2023
2 parents 0721352 + 1c5952f commit 6cb6a19
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions app/communication/migrations/0008_alter_banner_options.py
Original file line number Diff line number Diff line change
@@ -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",)},
),
]
10 changes: 9 additions & 1 deletion app/communication/models/banner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
2 changes: 2 additions & 0 deletions app/communication/serializers/banner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ class Meta:
"updated_at",
"image",
"image_alt",
"is_visible",
"is_expired",
)
25 changes: 25 additions & 0 deletions app/communication/views/banner.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"],
Expand Down

0 comments on commit 6cb6a19

Please sign in to comment.