40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
# ITerativ GmbH
|
|
# http://www.iterativ.ch/
|
|
#
|
|
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
|
|
#
|
|
# Created on 2019-08-05
|
|
# @author: chrigu <christian.cueni@iterativ.ch>
|
|
from wagtail.core import hooks
|
|
|
|
from rooms.models import AdminGeneratedRoomSlug
|
|
|
|
|
|
@hooks.register('after_edit_page')
|
|
@hooks.register('after_create_page')
|
|
def do_after_page_edit(request, page):
|
|
blocks = get_room_blocks(page)
|
|
for block in blocks:
|
|
AdminGeneratedRoomSlug.objects.get_or_create(slug=block[1]['slug'])
|
|
|
|
|
|
def get_room_blocks(page):
|
|
top_level_admin_slug_blocks = get_block_from_stream_data(page.contents.stream_data, 'admin_room_slug')
|
|
content_list_admin_slug_blocks = get_admin_slugs_from_content_list(page.contents.stream_data)
|
|
return top_level_admin_slug_blocks + content_list_admin_slug_blocks
|
|
|
|
|
|
def get_block_from_stream_data(stream_data, block_name):
|
|
return [block for block in stream_data if block[0] in [block_name]]
|
|
|
|
|
|
def get_admin_slugs_from_content_list(stream_data):
|
|
admin_slug_blocks = []
|
|
content_list_items = get_block_from_stream_data(stream_data, 'content_list_item')
|
|
for content_list_item in content_list_items:
|
|
admin_slug_blocks = admin_slug_blocks + get_block_from_stream_data(content_list_item[1].stream_data,
|
|
'admin_room_slug')
|
|
return admin_slug_blocks
|