Files
kursy-mirror/home/wagtail_hooks.py

80 lines
2.2 KiB
Python

from wagtail.admin.menu import MenuItem
import wagtail.admin.rich_text.editors.draftail.features as draftail_features
from wagtail.admin.rich_text.converters.html_to_contentstate import BlockElementHandler
from wagtail import hooks
from django.urls import path, reverse
from . import views
@hooks.register("register_rich_text_features")
def register_code_block_feature(features):
"""
Registering the `code-block` feature, which uses the `CODE-BLOCK` Draft.js block type,
and is stored as HTML with a <pre><code> tag.
"""
feature_name = "code-block"
type_ = "CODE-BLOCK"
control = {
"type": type_,
"label": "</>",
"description": "Code block",
"element": "pre",
}
features.register_editor_plugin(
"draftail", feature_name, draftail_features.BlockFeature(control)
)
features.register_converter_rule(
"contentstate",
feature_name,
{
"from_database_format": {
"pre": BlockElementHandler(type_),
"pre > code": BlockElementHandler(type_),
},
"to_database_format": {
"block_map": {type_: {"element": "pre", "props": {}}}
},
},
)
# Optional: add to default features
features.default_features.append(feature_name)
@hooks.register("register_admin_urls")
def register_admin_urls():
return [
path("chat/", views.admin_chat_dashboard, name="admin_chat_dashboard"),
path(
"chat/user/<int:user_id>/",
views.admin_chat,
name="admin_chat",
),
path("purchase/", views.admin_purchase_dashboard, name="admin_purchase_dashboard"),
path("purchase/add/", views.admin_purchase, name="admin_purchase_add"),
path(
"purchase/<int:product_id>/",
views.admin_purchase,
name="admin_purchase",
),
]
@hooks.register("register_admin_menu_item")
def register_admin_chat_menu_item():
return MenuItem("Chat", reverse("admin_chat_dashboard"), icon_name="mail")
@hooks.register("register_admin_menu_item")
def register_admin_purchase_menu_item():
return MenuItem("Products", reverse("admin_purchase_dashboard"), icon_name="tag")