-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0bbd192
commit b710a17
Showing
3 changed files
with
44 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters