Skip to content

Commit

Permalink
Switch to just writing JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
willbarton committed May 6, 2024
1 parent 0bbd192 commit b710a17
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
5 changes: 5 additions & 0 deletions cfgov/archival/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from django.apps import AppConfig
from django.conf import settings

import fs


class ArchivalConfig(AppConfig):
name = "archival"
label = "archival"
filesystem_name = getattr(settings, "ARCHIVE_FILESYSTEM", "osfs:///")
filesystem = fs.open_fs(filesystem_name)
40 changes: 38 additions & 2 deletions cfgov/archival/wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
import json
from urllib.parse import unquote

from django.apps import apps
from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder
from django.utils.encoding import smart_str

from wagtail import hooks

from archival.views import PageArchiveView
from fs import path


def export_page(page):
page_data = {
"app_label": page.content_type.app_label,
"model": page.content_type.model,
"path": page.specific.url,
"data": page.serializable_data(),
}

page_json = json.dumps(
page_data, ensure_ascii=False, indent=4, cls=DjangoJSONEncoder
)
return page_json


def archive_page_data(page):
fs = apps.get_app_config("bakery").filesystem

page_path = unquote(page.specific.url[1:])
archive_path = path.join(settings.ARCHIVE_DIR, page_path)
fs.exists(archive_path) or fs.makedirs(archive_path)

page_json = export_page(page)
page_filename = f"{page.slug}.json"
target_path = smart_str(path.join(archive_path, page_filename))

fs.writetext(target_path, page_json, encoding="utf8")


@hooks.register("before_delete_page")
def archive_before_deletion(request, page):
"""Archive a page with wagtail-bakery before deleting it."""
PageArchiveView().build_archive(page)
archive_page_data(page)
9 changes: 1 addition & 8 deletions cfgov/cfgov/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,11 +754,4 @@
DRAFTAIL_ANCHORS_RENDERER = "wagtail_draftail_anchors.rich_text.render_span"


# Baking testing config
INSTALLED_APPS += (
"bakery",
"wagtailbakery",
"archival",
)
BUILD_DIR = "/srv/cfgov/archive/"
BAKERY_VIEWS = ("wagtailbakery.views.AllPagesView",)
ARCHIVE_DIR = "/srv/cfgov/archive/"

0 comments on commit b710a17

Please sign in to comment.