Exemple #1
0
def remove_menu_item(menu_item):
    menu = g.db.settings.find_one()
    current_menu = None
    for item in menu.get('menu'):
        if item.get('name') == menu_item:
            current_menu = item
            break
    if current_menu:
        g.db.settings.update(
            {
                '_id': menu.get('_id')
            }, {
                '$pull': {
                    'menu': {
                        'name': menu_item
                    }
                }
            }
        )
        help.check_top_level_to_remove(current_menu)
        flash('Menu Item %s was successfully removed' % menu_item)
    return redirect(url_for('adminblueprint.menu_settings'))
Exemple #2
0
def menu_settings(edit_menu_name=None):
    error = True
    settings = check_and_initialize()
    menu_list = help.get_and_sort(
        settings.get('menu'),
        'parent_order',
        'order'
    )
    top_level_menu = help.get_and_sort(
        settings.get('top_level_menu'),
        'order'
    )

    if edit_menu_name:
        menus = settings.get('menu')
        menu_edit = None
        for item in menus:
            if item.get('name') == edit_menu_name:
                menu_edit = item
                break

        if menu_edit:
            title = "Edit Menu Settings for %s" % \
                help.unslug(edit_menu_name)
            menu_form = help.deploy_custom_form(
                'menu_items_form',
                parent_menu=menu_edit.get('parent'),
                menu_display_name=menu_edit.get('display_name'),
                menu_item_url=menu_edit.get('url'),
                menu_permissions=menu_edit.get('view_permissions'),
                menu_item_status=menu_edit.get('active'),
                db_name=menu_edit.get('name'),
                action='edit'
            )
        else:
            title = "Application Menu Settings"
            menu_form = help.deploy_custom_form('menu_items_form')
            edit_menu_name = None
    else:
        title = "Application Menu Settings"
        menu_form = help.deploy_custom_form('menu_items_form')

    parent_menus = help.generate_parent_menu(settings.get('menu'))
    menu_form.parent_menu.choices = [
        (parent, parent) for parent in parent_menus
    ]

    active_roles = help.generate_active_roles(settings.get('roles'))
    menu_form.menu_permissions.choices = [
        (help.slug(role), role) for role in active_roles
    ]
    if request.method == 'POST' and menu_form.validate_on_submit():
        db_name = help.slug(
            str(request.form.get('db_name'))
        )
        existing_name = g.db.settings.find_one(
            {
                'menu.name': db_name
            }
        )
        if existing_name:
            if not (edit_menu_name and (menu_edit.get('name') == db_name)):
                flash(
                    'Name already exists, please choose another name',
                    'error'
                )
                return render_template(
                    'admin/manage_menu.html',
                    title=title,
                    menu_form=menu_form,
                    menu_list=menu_list,
                    top_level_menu=top_level_menu,
                    error=error
                )

        existing_url = g.db.settings.find_one(
            {
                'menu.url': request.form.get('menu_item_url')
            }
        )
        if existing_url:
            if not (edit_menu_name and
                    menu_edit.get('url') == request.form.get('menu_item_url')):
                flash(
                    'URL is already being used, '
                    'please check the URL and try again',
                    'error'
                )
                return render_template(
                    'admin/manage_menu.html',
                    title=title,
                    menu_form=menu_form,
                    menu_list=menu_list,
                    top_level_menu=top_level_menu,
                    error=error
                )

        if request.form.get('parent_menu') == "Add New Parent":
            if request.form.get('new_parent'):
                existing_parent = g.db.settings.find_one(
                    {
                        'top_level_menu.slug': help.slug(
                            request.form.get('new_parent')
                        )
                    }
                )
                if existing_parent:
                    flash(
                        'Parent is already in use, '
                        'please check the value and try again',
                        'error'
                    )
                    return render_template(
                        'admin/manage_menu.html',
                        title=title,
                        menu_form=menu_form,
                        menu_list=menu_list,
                        top_level_menu=top_level_menu,
                        error=error
                    )
                parent_menu = help.normalize(request.form.get('new_parent'))
            else:
                flash(
                    'New Parent cannot be blank when adding a new Parent Item',
                    'error'
                )
                return render_template(
                    'admin/manage_menu.html',
                    title=title,
                    menu_form=menu_form,
                    menu_list=menu_list,
                    top_level_menu=top_level_menu,
                    error=error
                )
        else:
            parent_menu = help.normalize(request.form.get('parent_menu'))

        status = False
        if request.form.get('menu_item_status'):
            status = True

        if edit_menu_name:
            g.db.settings.update(
                {
                    'menu.name': edit_menu_name
                }, {
                    '$set': {
                        'menu.$.name': db_name,
                        'menu.$.display_name': help.normalize(
                            request.form.get('menu_display_name')
                        ),
                        'menu.$.url': request.form.get('menu_item_url'),
                        'menu.$.view_permissions': request.form.get(
                            'menu_permissions'
                        ),
                        'menu.$.active': status,
                        'menu.$.parent': help.slug(parent_menu),
                        'menu.$.parent_order': help.get_parent_order(
                            parent_menu,
                            settings,
                            request.form.get('menu_display_name')
                        )
                    }
                }
            )
            if (
                (
                    menu_edit.get('display_name') != help.normalize(
                        request.form.get('menu_display_name')
                    )
                ) or (
                    menu_edit.get('parent') != help.slug(parent_menu)
                )
            ):
                help.check_top_level_to_remove(menu_edit)
            flash('Menu Item was edited successfully')
        else:
            g.db.settings.update(
                {
                    '_id': settings.get('_id')
                }, {
                    '$push': {
                        'menu': {
                            'name': db_name,
                            'display_name': help.normalize(
                                request.form.get('menu_display_name')
                            ),
                            'url': request.form.get('menu_item_url'),
                            'view_permissions': request.form.get(
                                'menu_permissions'
                            ),
                            'active': status,
                            'parent': help.slug(parent_menu),
                            'order': help.get_next_order_number(
                                menu_list, parent_menu
                            ),
                            'parent_order': help.get_parent_order(
                                parent_menu,
                                settings,
                                request.form.get('menu_display_name')
                            )
                        }
                    }
                }
            )
            flash('Menu Item successfully Added')

        return redirect(url_for('adminblueprint.menu_settings'))
    elif request.method == 'POST' and not (menu_form.validate_on_submit()):
        flash(
            'Form validation failed. Please check the form and try again',
            'error'
        )
        return render_template(
            'admin/manage_menu.html',
            title=title,
            menu_form=menu_form,
            menu_list=menu_list,
            top_level_menu=top_level_menu,
            error=error
        )
    else:
        if edit_menu_name:
            return render_template(
                'admin/_edit_settings_menu.html',
                menu_form=menu_form,
                name=menu_edit.get('name')
            )
        else:
            return render_template(
                'admin/manage_menu.html',
                title=title,
                menu_form=menu_form,
                menu_list=menu_list,
                top_level_menu=top_level_menu
            )