Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audience and event relations to SplashEvent #2385

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions apps/splash/admin.py
Original file line number Diff line number Diff line change
@@ -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",)
18 changes: 16 additions & 2 deletions apps/splash/api/serializers.py
Original file line number Diff line number Diff line change
@@ -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")
7 changes: 5 additions & 2 deletions apps/splash/api/urls.py
Original file line number Diff line number Diff line change
@@ -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"
)
13 changes: 10 additions & 3 deletions apps/splash/api/views.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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,)
6 changes: 5 additions & 1 deletion apps/splash/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
}
91 changes: 91 additions & 0 deletions apps/splash/migrations/0010_auto_20191102_2058.py
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
41 changes: 37 additions & 4 deletions apps/splash/models.py
Original file line number Diff line number Diff line change
@@ -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")
35 changes: 31 additions & 4 deletions apps/splash/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -29,15 +29,15 @@ 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)

self.assertEqual(response.status_code, status.HTTP_200_OK)

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)

Expand All @@ -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()

Expand All @@ -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"))