Skip to content

Commit

Permalink
Merge branch 'main' into docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrharpo authored Nov 21, 2023
2 parents 65227bc + 4a12861 commit 1ed02c9
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 9 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
OV_DB_ENGINE=django.db.backends.postgresql
OV_DB_HOST=0.0.0.0
OV_DB_HOST=localhost
OV_DB_PORT=5432
OV_DB_NAME=postgres
OV_DB_USER=postgres
OV_DB_PASSWORD=""

OV_BASE_URL=http://localhost:3000
OV_ADMIN_BASE_URL=http://localhost:8000
OV_PREVIEW_URL=http://localhost:3000/preview
22 changes: 22 additions & 0 deletions client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<script>
function go() {
var querystring = window.location.search.replace(/^\?/, '');
var params = {};
querystring.replace(/([^=&]+)=([^&]*)/g, function(m, key, value) {
params[decodeURIComponent(key)] = decodeURIComponent(value);
});

var apiUrl = 'http://localhost:8000/api/v2/page_preview/1/?content_type=' + encodeURIComponent(params['content_type']) + '&token=' + encodeURIComponent(params['token']) + '&format=json';
fetch(apiUrl).then(function(response) {
response.text().then(function(text) {
document.body.innerText = text;
});
});
}
</script>
</head>
<body onload="go()"></body>
</html>
3 changes: 2 additions & 1 deletion exhibits/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from wagtail.images.api.fields import ImageRenditionField
from wagtail.models import Orderable, Page
from wagtail.search import index
from wagtail_headless_preview.models import HeadlessMixin

from authors.serializers import AuthorSerializer
from ov_wag.serializers import RichTextSerializer
Expand Down Expand Up @@ -65,7 +66,7 @@ class ExhibitPageApiSchema(BaseModel):
hero_thumb: ImageApiSchema


class ExhibitPage(Page):
class ExhibitPage(HeadlessMixin, Page):
body = RichTextField(blank=True)

cover_image = models.ForeignKey(
Expand Down
3 changes: 2 additions & 1 deletion home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from wagtail.api import APIField
from wagtail.fields import RichTextField
from wagtail.models import Page
from wagtail_headless_preview.models import HeadlessMixin


class HomePage(Page):
class HomePage(HeadlessMixin, Page):
body = RichTextField(blank=True)

content_panels: ClassVar[list[FieldPanel]] = [
Expand Down
3 changes: 2 additions & 1 deletion ov_collections/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
from wagtail.images.blocks import ImageChooserBlock
from wagtail.models import Page
from wagtail.search import index
from wagtail_headless_preview.models import HeadlessMixin

from .blocks import ContentBlock, ContentImageBlock


class Collection(Page):
class Collection(HeadlessMixin, Page):
introduction = RichTextField(blank=True)

content = StreamField(
Expand Down
45 changes: 41 additions & 4 deletions ov_wag/api.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@
from wagtail.api.v2.views import PagesAPIViewSet
from django.contrib.contenttypes.models import ContentType
from rest_framework.response import Response
from wagtail.api.v2.router import WagtailAPIRouter
from wagtail.images.api.v2.views import ImagesAPIViewSet
from wagtail.api.v2.views import PagesAPIViewSet
from wagtail.documents.api.v2.views import DocumentsAPIViewSet
from wagtail.images.api.v2.views import ImagesAPIViewSet
from wagtail_headless_preview.models import PagePreview

from authors.views import AuthorsAPIViewSet
from exhibits.views import ExhibitsAPIViewSet
from ov_collections.views import CollectionAPIViewSet

# Create the router. "wagtailapi" is the URL namespace
# Create the router. 'wagtailapi' is the URL namespace
api_router = WagtailAPIRouter('wagtailapi')

# Add the three endpoints using the "register_endpoint" method.

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


api_router.register_endpoint('page_preview', PagePreviewAPIViewSet)

# Add the three endpoints using the 'register_endpoint' method.
# The first parameter is the name of the endpoint (eg. pages, images). This
# is used in the URL of the endpoint
# The second parameter is the endpoint class that handles the requests
Expand Down
12 changes: 11 additions & 1 deletion ov_wag/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'wagtail.admin',
'wagtail',
'wagtail.api.v2',
'corsheaders',
'rest_framework',
'modelcluster',
'taggit',
Expand All @@ -56,9 +57,11 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'wagtail_headless_preview',
]

MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
Expand Down Expand Up @@ -125,6 +128,9 @@
},
]

# CORS to allow cross-origin requests from the client
CORS_ALLOW_ALL_ORIGINS = True
CORS_URLS_REGEX = r'^/api/v2/'

# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
Expand Down Expand Up @@ -166,7 +172,7 @@

# Wagtail settings

WAGTAIL_SITE_NAME = "ov-wag"
WAGTAIL_SITE_NAME = 'ov-wag'

# Search
# https://docs.wagtail.io/en/stable/topics/search/backends.html
Expand All @@ -182,3 +188,7 @@

WAGTAIL_BASE_URL = os.environ.get('OV_BASE_URL')
WAGTAILADMIN_BASE_URL = os.environ.get('OV_ADMIN_BASE_URL', '')

WAGTAIL_HEADLESS_PREVIEW = {
'CLIENT_URLS': {'default': os.environ.get('OV_PREVIEW_URL')},
}
27 changes: 27 additions & 0 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ dependencies = [
"psycopg2~=2.9",
"python-dotenv~=1.0",
"gunicorn~=21.2",
"wagtail-headless-preview>=0.7.0",
"django-cors-headers>=4.3.1",
]
requires-python = '>=3.9,<4.0'

Expand Down

0 comments on commit 1ed02c9

Please sign in to comment.