Skip to content

Commit

Permalink
Merge branch 'develop' into etna-rosetta
Browse files Browse the repository at this point in the history
  • Loading branch information
TNA-Allan committed Jan 11, 2024
2 parents abcc099 + bbed3ee commit 62ef818
Show file tree
Hide file tree
Showing 22 changed files with 489 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd-feature.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Deploy feature branch

on:
workflow_call:
workflow_dispatch:
# push:
# branches:
# - feature/*
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ config/settings/local.py
.npm
.nvm

media
media/images
media/original_images
coverage.xml
2 changes: 1 addition & 1 deletion bash/run-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
mkdir -p /app/templates/static/assets
cp -R /app/node_modules/@nationalarchives/frontend/nationalarchives/assets/* /app/templates/static/assets
poetry run python /app/manage.py migrate
# poetry run python /app/manage.py createsuperuser --no-input
poetry run python /app/manage.py createsuperuser --no-input
poetry run python /app/manage.py runserver 0.0.0.0:8080
14 changes: 13 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
WAGTAILAPI_BASE_URL = os.getenv("WAGTAILAPI_BASE_URL", "")
WAGTAIL_HEADLESS_PREVIEW = {
"CLIENT_URLS": {}, # defaults to an empty dict. You must at the very least define the default client URL.
"SERVE_BASE_URL": None, # can be used for HeadlessServeMixin
"REDIRECT_ON_PREVIEW": False, # set to True to redirect to the preview instead of using the Wagtail default mechanism
"ENFORCE_TRAILING_SLASH": True, # set to False in order to disable the trailing slash enforcement
}

DEBUG = strtobool(os.getenv("DEBUG", "False"))

Expand Down Expand Up @@ -82,6 +89,9 @@
"allauth",
"allauth.account",
"birdbath",
"wagtail.api.v2",
"rest_framework",
"wagtail_headless_preview",
]

SITE_ID = 1
Expand Down Expand Up @@ -274,7 +284,9 @@

# Base URL to use when referring to full URLs within the Wagtail admin backend -
# e.g. in notification emails. Don't include '/admin' or a trailing slash
WAGTAILADMIN_BASE_URL = os.getenv("WAGTAILADMIN_BASE_URL", "https://www.example.com")
WAGTAILADMIN_BASE_URL = os.getenv(
"WAGTAILADMIN_BASE_URL", "https://nationalarchives.gov.uk"
)

# For search results within Wagtail itself
WAGTAILSEARCH_BACKENDS = {
Expand Down
10 changes: 10 additions & 0 deletions config/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
os.getenv("DEBUG_TOOLBAR_ENABLED", "True") # noqa: F405
)

WAGTAILADMIN_BASE_URL = os.getenv("WAGTAILADMIN_BASE_URL", "http://localhost:8000")
WAGTAIL_HEADLESS_PREVIEW = {
"CLIENT_URLS": {
"default": "http://localhost:65535/preview",
},
"SERVE_BASE_URL": "http://localhost:65535",
"REDIRECT_ON_PREVIEW": False,
"ENFORCE_TRAILING_SLASH": True,
}

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "@6gce61jt^(pyj5+l**&*_#zyxfj5v1*71cs5yoetg-!fsz826"

Expand Down
4 changes: 2 additions & 2 deletions config/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# TODO: Temporary until the static files can be served via S3 or a CDN
DEBUG = True

# Ensure that certificates are always checked by the Client API in production
CLIENT_VERIFY_CERTIFICATES = True
# TODO: Ensure that certificates are always checked by the Client API in production
# CLIENT_VERIFY_CERTIFICATES = True

# TODO: Generate SECRET_KEY
SECRET_KEY = os.getenv("SECRET_KEY", "")
Expand Down
2 changes: 2 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.utils.urlpatterns import decorate_urlpatterns

from etna.api.urls import api_router
from etna.core.cache_control import (
apply_default_cache_control,
apply_default_vary_headers,
Expand Down Expand Up @@ -36,6 +37,7 @@ def trigger_error(request):

# Private URLs that are not meant to be cached.
private_urls = [
path("api/v2/", api_router.urls),
path("django-admin/", admin.site.urls),
path("admin/", include(wagtailadmin_urls)),
path("accounts/", include("allauth.urls")),
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ services:
- DATABASE_USER=postgres
- DJANGO_SUPERUSER_PASSWORD=admin
- DJANGO_SUPERUSER_USERNAME=admin
- DJANGO_SUPERUSER_EMAIL=admin@tna-development
- DJANGO_SUPERUSER_EMAIL=admin@tna.dev
- FEATURE_BETA_BANNER_ENABLED=False
- KEEP_ALIVE=30
- WAGTAILAPI_BASE_URL=http://host.docker.internal:8000

cli:
container_name: cli
Expand Down
3 changes: 3 additions & 0 deletions etna/alerts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db import models

from wagtail.admin.panels import FieldPanel
from wagtail.api import APIField
from wagtail.fields import RichTextField
from wagtail.snippets.models import register_snippet

Expand Down Expand Up @@ -75,5 +76,7 @@ class AlertMixin(models.Model):
FieldPanel("alert"),
]

api_fields = [APIField("alert")]

class Meta:
abstract = True
80 changes: 80 additions & 0 deletions etna/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from django.contrib.contenttypes.models import ContentType

# from wagtail.api import APIField
from wagtail.api.v2.router import WagtailAPIRouter
from wagtail.api.v2.views import PagesAPIViewSet

# from wagtail.documents.api.v2.views import DocumentsAPIViewSet
from wagtail.images.api.v2.views import ImagesAPIViewSet

from rest_framework.response import Response
from wagtail_headless_preview.models import PagePreview
from wagtailmedia.api.views import MediaAPIViewSet

# from wagtail.images.api.fields import ImageRenditionField


class PagePreviewAPIViewSet(PagesAPIViewSet):
known_query_parameters = PagesAPIViewSet.known_query_parameters.union(
["content_type", "token"]
)

def listing_view(self, request):
page = self.get_object()
serializer = self.get_serializer(page)
return Response(serializer.data)

def detail_view(self, request, pk):
page = self.get_object()
serializer = self.get_serializer(page)
return Response(serializer.data)

def get_object(self):
app_label, model = self.request.GET["content_type"].split(".")
content_type = ContentType.objects.get(app_label=app_label, model=model)

page_preview = PagePreview.objects.get(
content_type=content_type, token=self.request.GET["token"]
)
page = page_preview.as_page()
if not page.pk:
# fake primary key to stop API URL routing from complaining
page.pk = 0

return page


class CustomImagesAPIViewSet(ImagesAPIViewSet):
body_fields = ImagesAPIViewSet.body_fields + [
# "collection",
"title",
"file",
"copyright",
"is_sensitive",
"custom_sensitive_image_warning",
"tags",
# "focal_point_x",
# "focal_point_y",
# "focal_point_width",
# "focal_point_height",
"transcription_heading",
"transcription",
"translation_heading",
"translation",
"record",
"record_dates",
"description",
# APIField(
# "image_small_jpg",
# serializer=ImageRenditionField("fill-128x128", source="file"),
# ),
]


api_router = WagtailAPIRouter("wagtailapi")

api_router.register_endpoint("pages", PagesAPIViewSet)
api_router.register_endpoint("images", CustomImagesAPIViewSet)
api_router.register_endpoint("media", MediaAPIViewSet)
# api_router.register_endpoint("documents", DocumentsAPIViewSet)
api_router.register_endpoint("page_preview", PagePreviewAPIViewSet)
84 changes: 83 additions & 1 deletion etna/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
MultiFieldPanel,
PageChooserPanel,
)
from wagtail.api import APIField
from wagtail.fields import RichTextField, StreamField
from wagtail.images import get_image_model_string
from wagtail.images.api.fields import ImageRenditionField
from wagtail.models import Orderable, Page
from wagtail.search import index
from wagtail.snippets.models import register_snippet

from rest_framework import serializers
from taggit.models import ItemBase, TagBase

from etna.authors.models import AuthorPageMixin
from etna.authors.models import AuthorPageMixin, AuthorTag
from etna.collections.models import TopicalPageMixin
from etna.core.models import (
BasePageWithIntro,
Expand Down Expand Up @@ -108,6 +111,8 @@ class Meta:
index.SearchField("article_tag_names", boost=2),
]

api_fields = [APIField("article_tag_names")]


class ArticleIndexPage(BasePageWithIntro):
"""ArticleIndexPage
Expand All @@ -134,6 +139,11 @@ class ArticleIndexPage(BasePageWithIntro):
use_json_field=True,
)

api_fields = BasePageWithIntro.api_fields + [
APIField("featured_article"),
APIField("featured_pages"),
]

# DataLayerMixin overrides
gtm_content_group = "Explore the collection"

Expand Down Expand Up @@ -171,6 +181,24 @@ def get_context(self, request):
]


# TODO: Make better
class PageSerializer(serializers.ModelSerializer):
class Meta:
model = Page
fields = (
"id",
"title",
"url_path",
)


# TODO: Make better
class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = AuthorTag
fields = ("author",)


class ArticlePage(
TopicalPageMixin,
RequiredHeroImageMixin,
Expand Down Expand Up @@ -237,6 +265,22 @@ class Meta:
]
)

verbose_name_public = Meta.verbose_name_public
api_fields = (
BasePageWithIntro.api_fields
+ RequiredHeroImageMixin.api_fields
+ ArticleTagMixin.api_fields
+ [
APIField("verbose_name_public"),
APIField("similar_items", serializer=PageSerializer(many=True)),
APIField("latest_items", serializer=PageSerializer(many=True)),
APIField("body"),
# TODO
# APIField("topics"),
# APIField("time_periods"),
]
)

def get_datalayer_data(self, request: HttpRequest) -> Dict[str, Any]:
data = super().get_datalayer_data(request)
data.update(
Expand Down Expand Up @@ -382,6 +426,17 @@ class Meta:
index.SearchField("author_names", boost=1),
]
)
api_fields = (
BasePageWithIntro.api_fields
+ ArticleTagMixin.api_fields
+ HeroImageMixin.api_fields
+ ContentWarningMixin.api_fields
+ [
APIField("type_label"),
APIField("body"),
APIField("authors", serializer=AuthorSerializer(many=True)),
]
)

def save(self, *args, **kwargs):
"""
Expand Down Expand Up @@ -615,6 +670,33 @@ class Meta:
]
)

api_fields = (
BasePageWithIntro.api_fields
+ ArticleTagMixin.api_fields
+ NewLabelMixin.api_fields
+ ContentWarningMixin.api_fields
+ [
APIField("type_label"),
APIField("about"),
APIField("date_text"),
APIField("about"),
APIField("record"),
APIField("gallery_heading"),
APIField("image_library_link"),
# APIField("intro_image"),
APIField("featured_article"),
APIField("promoted_links"),
# APIField("promote_panels"),
APIField(
"intro_image_jpg",
serializer=ImageRenditionField("fill-512x512", source="intro_image"),
),
# APIField("content_panels"),
# APIField("gallery_items"),
# APIField("gallery_text"),
]
)

@cached_property
def gallery_items(self):
"""
Expand Down
Loading

0 comments on commit 62ef818

Please sign in to comment.