Skip to content

Commit

Permalink
feat: add praktisk info app
Browse files Browse the repository at this point in the history
  • Loading branch information
niccofyren committed Dec 3, 2023
1 parent f8f8032 commit 260109e
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 0 deletions.
Empty file added praktisk/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions praktisk/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
22 changes: 22 additions & 0 deletions praktisk/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from wagtail.api.v2.router import WagtailAPIRouter
from wagtail.api.v2.views import PagesAPIViewSet

from .models import InfoIndexPage, InfoPage

api_router = WagtailAPIRouter("praktiskapi")


class InfoPagesAPIViewSet(PagesAPIViewSet):
# This combined with get_queryset is just an awkward way of only allowing
# InfoIndexPage and InfoPage to be returned, and not any other page type.
# Guessing there is a better pattern for this
known_query_parameters = PagesAPIViewSet.known_query_parameters.difference(["type"])

meta_fields = PagesAPIViewSet.meta_fields + ["last_published_at"]

def get_queryset(self):
allowed_models = [InfoIndexPage, InfoPage]
return super().get_queryset().type(tuple(allowed_models))


api_router.register_endpoint("info", InfoPagesAPIViewSet)
6 changes: 6 additions & 0 deletions praktisk/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class PraktiskConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "praktisk"
55 changes: 55 additions & 0 deletions praktisk/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Generated by Django 4.2.7 on 2023-12-03 10:09

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

dependencies = [
("aktuelt", "0009_remove_newspage_date_newspage_custom_published_at_and_more"),
]

operations = [
migrations.CreateModel(
name="InfoIndexPage",
fields=[
(
"newsindexpage_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="aktuelt.newsindexpage",
),
),
],
options={
"abstract": False,
},
bases=("aktuelt.newsindexpage",),
),
migrations.CreateModel(
name="InfoPage",
fields=[
(
"newspage_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="aktuelt.newspage",
),
),
],
options={
"abstract": False,
},
bases=("aktuelt.newspage",),
),
]
Empty file added praktisk/migrations/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions praktisk/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from django.db import models
from rest_framework.serializers import ModelSerializer
from wagtail.api.v2.serializers import (
BaseSerializer,
ChildRelationField,
Field,
get_serializer_class,
)
from wagtail.api.v2.views import APIField, PageSerializer

from aktuelt.models import NewsIndexPage, NewsPage
from praktisk.serializers import ChildInfoIndexPagesSerializer, ChildInfoPageSerializer

# In info context we imagine structures like this
# - Practical info (top index/intro) - InfoIndexPage
# - Standalone info page 1 - InfoPage
# - Safety info (topic intro) - InfoPageIndex
# - Safety page 1 - InfoPage
# - Safety page 2 - InfoPage
# - ...
# - Ticket info (topic intro) - InfoPageIndex
# - Ticket page 1 - InfoPage
# - ...
#
# This is a bit different from the news context, where we mostly organize based
# on tags. This initial setup is an assumption, so feel free to adjust as needed.


# Extending the NewsPage models since they have a lot of similarities,
# feel free to split them up when/if conventient
class InfoPage(NewsPage):
page_description = "A regular info page"
parent_page_types = ["praktisk.InfoIndexPage"]
subpage_types = []


class InfoIndexPage(NewsIndexPage):
page_description = "Page to list all published info items"
parent_page_types = ["home.HomePage", "praktisk.InfoIndexPage"]
subpage_types = ["praktisk.InfoPage", "praktisk.InfoIndexPage"]

api_meta_fields = [
APIField("pages", ChildRelationField(serializer_class=ChildInfoPageSerializer)),
APIField("topics", serializer=ChildInfoIndexPagesSerializer()),
]

def pages(self):
return InfoPage.objects.live().descendant_of(self)

def topics(self):
return InfoIndexPage.objects.live().descendant_of(self)
33 changes: 33 additions & 0 deletions praktisk/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from wagtail.api.v2.serializers import Field, PageSerializer


# The more basic serializers as seen in aktuelt/serializers.py seem a lot
# more appropriate for current usage here. But hoping we can find a pattern where
# the Info(Index)Page is used to serialize. Ie. it uses and respects
# `.api_fields` and `.api_meta_fields` from the model similar to how it is done
# in PageAPIViewSet.
#
# Currently just left here for someone to play around with when/if they want
class ChildInfoPageSerializer(Field):
def __init__(self, *args, **kwargs):
kwargs.pop("context")
super().__init__(*args, **kwargs)

def to_representation(self, info_page):
return {
"id": info_page.pk,
"title": info_page.title,
"intro": info_page.intro,
}


class ChildInfoIndexPagesSerializer(Field):
def to_representation(self, pages):
return [
{
"id": index_page.pk,
"title": index_page.title,
"intro": index_page.intro,
}
for index_page in pages.all()
]
3 changes: 3 additions & 0 deletions praktisk/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions praktisk/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
1 change: 1 addition & 0 deletions tgno/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

INSTALLED_APPS = [
"aktuelt",
"praktisk",
"home",
"search",
"wagtail.contrib.forms",
Expand Down
2 changes: 2 additions & 0 deletions tgno/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from wagtail.documents import urls as wagtaildocs_urls

from aktuelt.api import api_router as aktuelt_api_router
from praktisk.api import api_router as praktisk_api_router
from search import views as search_views

from .api import api_router as base_api_router
Expand All @@ -17,6 +18,7 @@
path("search/", search_views.search, name="search"),
path("api/v2/", base_api_router.urls),
path("api/v2/", aktuelt_api_router.urls),
path("api/v2/", praktisk_api_router.urls),
re_path(r"^", include(wagtail_urls)),
]

Expand Down

0 comments on commit 260109e

Please sign in to comment.