~homeworkprod/byceps

8ea8fd1e70d1222c92cad3f3bbf534d8d956fc43 — Jochen Kupperschmidt a month ago a0cc457
Add admin UI to create site navigation menus
A byceps/blueprints/admin/site/navigation/forms.py => byceps/blueprints/admin/site/navigation/forms.py +25 -0
@@ 0,0 1,25 @@
"""
byceps.blueprints.admin.site.navigation.forms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2014-2022 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from flask_babel import lazy_gettext
from wtforms import BooleanField, StringField
from wtforms.validators import InputRequired

from .....util.l10n import LocalizedForm


class _BaseForm(LocalizedForm):
    name = StringField(lazy_gettext('Name'), validators=[InputRequired()])
    language_code = StringField(
        lazy_gettext('Language code'), validators=[InputRequired()]
    )
    hidden = BooleanField(lazy_gettext('hidden'))


class MenuCreateForm(_BaseForm):
    pass

M byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/index_for_site.html => byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/index_for_site.html +11 -1
@@ 1,11 1,21 @@
{% extends 'layout/admin/site/navigation.html' %}
{% from 'macros/icons.html' import render_icon %}
{% from 'macros/misc.html' import render_tag %}
{% set page_title = _('Menus') %}

{% block body %}

  <div class="row row--space-between">
    <h1>{{ page_title }}</h1>
    <div>
      <h1>{{ page_title }}</h1>
    </div>
    <div>
      <div class="button-row button-row--right">
        {%- if has_current_user_permission('site_navigation.administrate') %}
        <a class="button" href="{{ url_for('.menu_create_form', site_id=site.id) }}">{{ render_icon('add') }} <span>{{ _('Create menu') }}</span></a>
        {%- endif %}
      </div>
    </div>
  </div>

  <div class="box">

A byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/menu_create_form.html => byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/menu_create_form.html +24 -0
@@ 0,0 1,24 @@
{% extends 'layout/admin/site/navigation.html' %}
{% from 'macros/admin.html' import render_backlink %}
{% from 'macros/forms.html' import form_buttons, form_field, form_field_check %}
{% set page_title = _('Create menu') %}

{% block before_body %}
{{ render_backlink(url_for('.index_for_site', site_id=site.id), _('Menus')) }}
{%- endblock %}

{% block body %}

  <h1>{{ page_title }}</h1>

  <form action="{{ url_for('.menu_create', site_id=site.id) }}" method="post">
    <div class="box">
      {{ form_field(form.name, maxlength=40, autofocus='autofocus') }}
      {{ form_field(form.language_code) }}
      {{ form_field_check(form.hidden) }}
    </div>

    {{ form_buttons(_('Create')) }}
  </form>

{%- endblock %}

M byceps/blueprints/admin/site/navigation/views.py => byceps/blueprints/admin/site/navigation/views.py +47 -2
@@ 6,7 6,8 @@ byceps.blueprints.admin.site.navigation.views
:License: Revised BSD (see `LICENSE` file for details)
"""

from flask import abort
from flask import abort, request
from flask_babel import gettext

from .....services.brand import service as brand_service
from .....services.site import service as site_service


@@ 14,8 15,11 @@ from .....services.site.transfer.models import Site, SiteID
from .....services.site_navigation import service as navigation_service
from .....services.site_navigation.transfer.models import MenuAggregate, MenuID
from .....util.framework.blueprint import create_blueprint
from .....util.framework.flash import flash_error, flash_success
from .....util.framework.templating import templated
from .....util.views import permission_required
from .....util.views import permission_required, redirect_to

from .forms import MenuCreateForm


blueprint = create_blueprint('site_navigation_admin', __name__)


@@ 57,6 61,47 @@ def view(menu_id):
    }


@blueprint.get('/for_site/<site_id>/create')
@permission_required('site_navigation.administrate')
@templated
def menu_create_form(site_id, erroneous_form=None):
    """Show form to create a menu."""
    site = _get_site_or_404(site_id)

    brand = brand_service.get_brand(site.brand_id)

    form = erroneous_form if erroneous_form else MenuCreateForm()

    return {
        'site': site,
        'brand': brand,
        'form': form,
    }


@blueprint.post('/for_site/<site_id>')
@permission_required('site_navigation.administrate')
def menu_create(site_id):
    """Create a menu."""
    site = _get_site_or_404(site_id)

    form = MenuCreateForm(request.form)

    if not form.validate():
        return menu_create_form(site_id, form)

    name = form.name.data.strip()
    language_code = form.language_code.data.strip()
    hidden = form.hidden.data

    menu = navigation_service.create_menu(
        site.id, name, language_code, hidden=hidden
    )

    flash_success(gettext('Menu "%(name)s" has been created.', name=menu.name))
    return redirect_to('.view', menu_id=menu.id)


def _get_site_or_404(site_id: SiteID) -> Site:
    site = site_service.find_site(site_id)


M byceps/permissions/site.py => byceps/permissions/site.py +8 -0
@@ 19,3 19,11 @@ register_permissions(
        ('view', lazy_gettext('View sites')),
    ],
)


register_permissions(
    'site_navigation',
    [
        ('administrate', lazy_gettext('Administrate site navigation')),
    ],
)

M byceps/translations/de/LC_MESSAGES/messages.po => byceps/translations/de/LC_MESSAGES/messages.po +26 -4
@@ 537,6 537,7 @@ msgstr "API-Tokens"
#: byceps/blueprints/admin/shop/article/templates/admin/shop/article/create_number_sequence_form.html:20
#: byceps/blueprints/admin/shop/order/templates/admin/shop/order/create_number_sequence_form.html:19
#: byceps/blueprints/admin/shop/storefront/templates/admin/shop/storefront/create_form.html:27
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/menu_create_form.html:21
#: byceps/blueprints/admin/site/templates/admin/site/create_form.html:39
#: byceps/blueprints/admin/snippet/mountpoint/templates/admin/snippet/mountpoint/create_form.html:29
#: byceps/blueprints/admin/snippet/templates/admin/snippet/create_document_form.html:22


@@ 1073,6 1074,10 @@ msgid "Position"
msgstr "Position"

#: byceps/blueprints/admin/board/templates/admin/board/board_view.html:49
#: byceps/blueprints/admin/site/navigation/forms.py:21
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/index_for_site.html:38
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/view.html:12
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/view.html:49
#: byceps/blueprints/site/board/templates/macros/board.html:5
#: byceps/blueprints/site/board/templates/site/board/category_view.html:35
#: byceps/blueprints/site/board/templates/site/board/topic_index.html:38


@@ 1285,7 1290,8 @@ msgstr "Marke erstellen"
#: byceps/blueprints/admin/party/templates/admin/party/view.html:94
#: byceps/blueprints/admin/shop/order/templates/admin/shop/order/_view_orderer.html:11
#: byceps/blueprints/admin/shop/shop/templates/admin/shop/shop/view.html:40
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/index_for_site.html:16
#: byceps/blueprints/admin/site/navigation/forms.py:17
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/index_for_site.html:26
#: byceps/blueprints/admin/site/templates/admin/site/view.html:125
#: byceps/blueprints/admin/snippet/forms.py:17
#: byceps/blueprints/admin/snippet/templates/admin/snippet/index_for_scope.html:35


@@ 3140,6 3146,7 @@ msgid "Remove person from team?"
msgstr "Person aus Team entfernen?"

#: byceps/blueprints/admin/page/forms.py:21
#: byceps/blueprints/admin/site/navigation/forms.py:19
msgid "Language code"
msgstr "Sprachcode"



@@ 3197,7 3204,7 @@ msgstr "Es sind keine Versionen vorhanden."

#: byceps/blueprints/admin/page/templates/admin/page/index_for_site.html:28
#: byceps/blueprints/admin/page/templates/admin/page/view_version.html:56
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/index_for_site.html:17
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/index_for_site.html:27
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/view.html:20
#: byceps/blueprints/site/user/settings/templates/site/user/settings/_account.html:60
msgid "Language"


@@ 4456,11 4463,22 @@ msgstr ""
"Newskanal \"%(news_channel_id)s\" has been removed from site "
"\"%(site_title)s\"."

#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/index_for_site.html:3
#: byceps/blueprints/admin/site/navigation/views.py:101
#, python-format
msgid "Menu \"%(name)s\" has been created."
msgstr "Das Menü \"%(name)s\" wurde angelegt."

#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/index_for_site.html:4
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/menu_create_form.html:7
msgid "Menus"
msgstr "Menüs"

#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/index_for_site.html:36
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/index_for_site.html:15
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/menu_create_form.html:4
msgid "Create menu"
msgstr "Menü erstellen"

#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/index_for_site.html:46
#: byceps/blueprints/admin/site/navigation/templates/admin/site/navigation/view.html:57
msgid "No menus exist."
msgstr "Es sind keine Menüs vorhanden."


@@ 7474,6 7492,10 @@ msgstr "Sites bearbeiten"
msgid "View sites"
msgstr "Sites anzeigen"

#: byceps/permissions/site.py:27
msgid "Administrate site navigation"
msgstr "Site-Navigation administrieren"

#: byceps/permissions/snippet.py:17
msgid "Create snippets"
msgstr "Snippets erstellen"

M scripts/data/roles.toml => scripts/data/roles.toml +1 -0
@@ 227,6 227,7 @@ assigned_permissions = [
    'site.create',
    'site.update',
    'site.view',
    'site_navigation.administrate',
]

[[roles]]