diff --git a/apps/splash/admin.py b/apps/splash/admin.py index 83e79c115..a8c88ee0c 100644 --- a/apps/splash/admin.py +++ b/apps/splash/admin.py @@ -1,13 +1,17 @@ from django.contrib import admin from reversion.admin import VersionAdmin -from apps.splash.models import SplashEvent +from apps.splash.models import AudienceGroup, SplashEvent +@admin.register(SplashEvent) class SplashEventAdmin(VersionAdmin): model = SplashEvent ordering = ["-start_time"] list_display = ["title", "start_time", "end_time"] -admin.site.register(SplashEvent, SplashEventAdmin) +@admin.register(AudienceGroup) +class AudienceGroupAdmin(VersionAdmin): + model = AudienceGroup + list_display = ("name",) diff --git a/apps/splash/api/serializers.py b/apps/splash/api/serializers.py index 1c44648c4..d8fe48e40 100644 --- a/apps/splash/api/serializers.py +++ b/apps/splash/api/serializers.py @@ -1,9 +1,23 @@ from rest_framework.serializers import ModelSerializer -from apps.splash.models import SplashEvent +from apps.splash.models import AudienceGroup, SplashEvent class SplashEventSerializer(ModelSerializer): class Meta: model = SplashEvent - fields = ("id", "title", "content", "start_time", "end_time") + fields = ( + "id", + "title", + "content", + "start_time", + "end_time", + "target_audience", + "event", + ) + + +class AudienceGroupSerializer(ModelSerializer): + class Meta: + model = AudienceGroup + fields = ("id", "name") diff --git a/apps/splash/api/urls.py b/apps/splash/api/urls.py index d4da0fe98..68ad45b1d 100644 --- a/apps/splash/api/urls.py +++ b/apps/splash/api/urls.py @@ -1,7 +1,10 @@ from apps.api.utils import SharedAPIRootRouter -from apps.splash.api.views import SplashEventViewSet +from apps.splash.api.views import AudienceGroupViewSet, SplashEventViewSet urlpatterns = [] router = SharedAPIRootRouter() -router.register("splash-events", SplashEventViewSet) +router.register("splash/events", SplashEventViewSet, basename="splash_events") +router.register( + "splash/audience-groups", AudienceGroupViewSet, basename="splash_audience" +) diff --git a/apps/splash/api/views.py b/apps/splash/api/views.py index 6b1756d36..8317ea41c 100644 --- a/apps/splash/api/views.py +++ b/apps/splash/api/views.py @@ -1,9 +1,10 @@ from rest_framework.pagination import PageNumberPagination +from rest_framework.permissions import AllowAny from rest_framework.viewsets import ReadOnlyModelViewSet -from apps.splash.api.serializers import SplashEventSerializer +from apps.splash.api.serializers import AudienceGroupSerializer, SplashEventSerializer from apps.splash.filters import SplashEventFilter -from apps.splash.models import SplashEvent +from apps.splash.models import AudienceGroup, SplashEvent class HundredItemsPaginator(PageNumberPagination): @@ -15,5 +16,11 @@ class SplashEventViewSet(ReadOnlyModelViewSet): serializer_class = SplashEventSerializer pagination_class = HundredItemsPaginator filterset_class = SplashEventFilter - filterset_fields = ("start_time", "end_time") + filterset_fields = ("start_time", "end_time", "target_audience") ordering_fields = ("id", "start_time", "end_time") + + +class AudienceGroupViewSet(ReadOnlyModelViewSet): + queryset = AudienceGroup.objects.all() + serializer_class = AudienceGroupSerializer + permission_classes = (AllowAny,) diff --git a/apps/splash/filters.py b/apps/splash/filters.py index 7b3edeac4..67f9a3cfb 100644 --- a/apps/splash/filters.py +++ b/apps/splash/filters.py @@ -6,4 +6,8 @@ class SplashEventFilter(django_filters.FilterSet): class Meta: model = SplashEvent - fields = {"start_time": ["gte", "lte"], "end_time": ["gte", "lte"]} + fields = { + "start_time": ["gte", "lte"], + "end_time": ["gte", "lte"], + "target_audience": ["exact"], + } diff --git a/apps/splash/migrations/0010_auto_20191102_2058.py b/apps/splash/migrations/0010_auto_20191102_2058.py new file mode 100644 index 000000000..6bced52d8 --- /dev/null +++ b/apps/splash/migrations/0010_auto_20191102_2058.py @@ -0,0 +1,91 @@ +# Generated by Django 2.1.11 on 2019-11-02 19:58 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ("events", "0024_auto_20191010_2259"), + ("splash", "0009_auto_20190506_1719"), + ] + + operations = [ + migrations.CreateModel( + name="AudienceGroup", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "name", + models.CharField(default="", max_length=64, verbose_name="Navn"), + ), + ], + options={ + "verbose_name": "Deltakergruppe", + "verbose_name_plural": "Deltakergrupper", + "ordering": ("name",), + }, + ), + migrations.AlterModelOptions( + name="splashevent", + options={ + "default_permissions": ("add", "change", "delete"), + "ordering": ("start_time",), + "verbose_name": "Fadderukearrangement", + "verbose_name_plural": "Fadderukearrangementer", + }, + ), + migrations.AddField( + model_name="splashevent", + name="event", + field=models.OneToOneField( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + related_name="splash_event", + to="events.Event", + verbose_name="Arrangement", + ), + ), + migrations.AlterField( + model_name="splashevent", + name="content", + field=models.TextField(verbose_name="Innhold"), + ), + migrations.AlterField( + model_name="splashevent", + name="end_time", + field=models.DateTimeField(verbose_name="Sluttid"), + ), + migrations.AlterField( + model_name="splashevent", + name="start_time", + field=models.DateTimeField(verbose_name="Starttid"), + ), + migrations.AlterField( + model_name="splashevent", + name="title", + field=models.CharField(max_length=100, verbose_name="Tittel"), + ), + migrations.AddField( + model_name="splashevent", + name="target_audience", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + related_name="splash_events", + to="splash.AudienceGroup", + verbose_name="Deltakergruppe", + ), + ), + ] diff --git a/apps/splash/models.py b/apps/splash/models.py index 75ac98f11..0678271af 100644 --- a/apps/splash/models.py +++ b/apps/splash/models.py @@ -1,15 +1,48 @@ from django.db import models +from django.utils.translation import ugettext as _ + +from apps.events.models import Event + + +class AudienceGroup(models.Model): + name = models.CharField(_("Navn"), default="", max_length=64) + + def __str__(self): + return self.name + + class Meta: + verbose_name = _("Deltakergruppe") + verbose_name_plural = _("Deltakergrupper") + ordering = ("name",) class SplashEvent(models.Model): - title = models.CharField("title", max_length=100) - content = models.TextField("content") - start_time = models.DateTimeField() - end_time = models.DateTimeField() + title = models.CharField(_("Tittel"), max_length=100) + content = models.TextField(_("Innhold")) + start_time = models.DateTimeField(_("Starttid")) + end_time = models.DateTimeField(_("Sluttid")) + target_audience = models.ForeignKey( + to=AudienceGroup, + related_name="splash_events", + verbose_name=_("Deltakergruppe"), + on_delete=models.SET_NULL, + null=True, + blank=True, + ) + event = models.OneToOneField( + to=Event, + related_name="splash_event", + verbose_name=_("Arrangement"), + on_delete=models.SET_NULL, + null=True, + blank=True, + ) def __str__(self): return self.title class Meta: + verbose_name = _("Fadderukearrangement") + verbose_name_plural = _("Fadderukearrangementer") ordering = ("start_time",) default_permissions = ("add", "change", "delete") diff --git a/apps/splash/tests.py b/apps/splash/tests.py index 057f87234..b6adb0618 100644 --- a/apps/splash/tests.py +++ b/apps/splash/tests.py @@ -7,7 +7,7 @@ from rest_framework import status from rest_framework.test import APITestCase -from apps.splash.models import SplashEvent +from apps.splash.models import AudienceGroup, SplashEvent class SplashModelsTestCase(TestCase): @@ -29,7 +29,7 @@ def test_calendar_export(self): class SplashAPIURLTestCase(APITestCase): def test_splash_events_list_no_events(self): - url = reverse("splashevent-list") + url = reverse("splash_events-list") response = self.client.get(url) @@ -37,7 +37,7 @@ def test_splash_events_list_no_events(self): def test_splash_events_list_with_events_exist(self): G(SplashEvent) - url = reverse("splashevent-list") + url = reverse("splash_events-list") response = self.client.get(url) @@ -50,7 +50,7 @@ def test_splash_events_list_filter_future(self): G(SplashEvent, start_time=last_week, end_time=last_week) next_week_event = G(SplashEvent, start_time=next_week, end_time=next_week) - url = reverse("splashevent-list") + url = reverse("splash_events-list") url += "?start_time__gte=%s" % datetime.datetime.now() @@ -61,3 +61,30 @@ def test_splash_events_list_filter_future(self): self.assertEqual( next_week_event.title, response.data.get("results")[0].get("title") ) + + def test_audience_group_filter(self): + url = reverse("splash_events-list") + + audience = G(AudienceGroup) + G(SplashEvent) + event_with_audience = G(SplashEvent, target_audience=audience) + + response = self.client.get(url, {"target_audience": audience.id}) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(1, response.data.get("count")) + self.assertEqual( + event_with_audience.id, response.data.get("results")[0].get("id") + ) + + def test_get_audience_groups(self): + url = reverse("splash_audience-list") + + G(AudienceGroup) + G(AudienceGroup) + G(AudienceGroup) + + response = self.client.get(url) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(3, response.data.get("count"))