Skip to content

Commit

Permalink
Banner filtering (#687)
Browse files Browse the repository at this point in the history
* feat: banner search

* chore: update changelog

* chore: refactor based on comments and callums PR 🙂

* chore: format

* chore: refactor comments

* Added migration

* Format

---------

Co-authored-by: Thomas <[email protected]>
  • Loading branch information
martcl and thomsen85 authored Oct 9, 2023
1 parent c38511c commit 99c9cd0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 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
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(is_visible=True)
return queryset

def filter_is_expired(self, queryset, name, value):
if value:
return queryset.filter(is_expired=True)
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
5 changes: 1 addition & 4 deletions app/group/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ class MembershipAdmin(admin.ModelAdmin):
"user",
)

search_fields = [
"user__first_name",
"user__last_name"
]
search_fields = ["user__first_name", "user__last_name"]


@admin.register(models.MembershipHistory)
Expand Down

0 comments on commit 99c9cd0

Please sign in to comment.