Пример #1
0
def permissions_define(role):
    error = None
    title = "Manage Permissions for: %s" % help.unslug(role)
    form = forms.ManagePermissions()
    url_root = request.url_root[:-1]
    url_routes = current_app.url_map.iter_rules()
    form = help.generate_dynamic_form(url_routes, role)

    if request.method == 'POST' and form.validate_on_submit():
        set_perms = help.evaluate_permissions(request.form.iterlists())
        g.db.settings.update(
            {
                'roles.name': role
            }, {
                '$unset': {
                    'roles.$.perms': 1
                }
            }
        )
        g.db.settings.update(
            {
                'roles.name': role
            }, {
                '$set': {
                    'roles.$.perms': set_perms
                }
            }
        )
        flash('Permissions have been updated for %s role' % help.unslug(role))
        return redirect(url_for('adminblueprint.manage_roles'))
    elif request.method == 'POST' and not (form.validate_on_submit()):
        flash(
            'Form validation failed, please check the form and try again',
            'error'
        )
        return render_template(
            'admin/manage_permissions.html',
            title=title,
            form=form,
            error=error
        )
    else:
        return render_template(
            'admin/manage_permissions.html',
            title=title,
            form=form
        )
Пример #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
            )
Пример #3
0
def manage_forms(form_id=None):
    error, edit_form = True, None
    all_forms = g.db.forms.find()
    if form_id:
        edit_form = g.db.forms.find_one({'_id': ObjectId(form_id)})
        form = forms.BuildForm(
            name=edit_form.get('name'),
            submission_url=edit_form.get('submission_url'),
            active=edit_form.get('active'),
            system_form=edit_form.get('system_form')
        )
    else:
        form = forms.BuildForm()

    if request.method == 'POST' and form.validate_on_submit():
        sani_name = help.slug(request.form.get('name'))
        active, system_form = False, False

        active = bool(request.form.get('active'))
        system_form = bool(request.form.get('system_form'))

        if edit_form:
            if not edit_form.get('name') == sani_name:
                if g.db.forms.find_one({'name': sani_name}):
                    flash(
                        'Form name already exists, please check '
                        'the name and try again',
                        'error'
                    )
                    return render_template(
                        'admin/manage_forms.html',
                        form=form,
                        all_forms=all_forms,
                        error=error
                    )
            if not edit_form.get('submission_url') == \
                    request.form.get('submission_url'):
                if g.db.forms.find_one(
                    {
                        'submission_url': request.form.get(
                            'submission_url'
                        )
                    }
                ):
                    flash(
                        'Another form posts to the same URL. '
                        'Please check the URL and try again',
                        'error'
                    )
                    return render_template(
                        'admin/manage_forms.html',
                        form=form,
                        all_forms=all_forms,
                        error=error
                    )

            g.db.forms.update(
                {
                    '_id': ObjectId(form_id)
                }, {
                    '$set': {
                        'name': sani_name,
                        'display_name': help.unslug(sani_name),
                        'submission_url': request.form.get('submission_url'),
                        'active': active,
                        'system_form': system_form
                    }
                }
            )
        else:
            if g.db.forms.find_one({'name': sani_name}):
                flash(
                    'Form name already exists, please check'
                    ' the name and try again',
                    'error'
                )
                return render_template(
                    'admin/manage_forms.html',
                    form=form,
                    all_forms=all_forms,
                    error=error
                )
            elif g.db.forms.find_one(
                    {
                        'submission_url': request.form.get(
                            'submission_url'
                        )
                    }):
                flash(
                    'Another form posts to the same URL.'
                    ' Please check the URL and try again',
                    'error'
                )
                return render_template(
                    'admin/manage_forms.html',
                    form=form,
                    all_forms=all_forms,
                    error=error
                )

            g.db.forms.insert(
                {
                    'name': sani_name,
                    'display_name': help.unslug(sani_name),
                    'submission_url': request.form.get('submission_url'),
                    'active': active,
                    'system_form': system_form
                }
            )
        if edit_form:
            flash('Successfully updated Custom Form')
            return redirect(url_for('adminblueprint.manage_forms'))
        else:
            flash('Successfully added Custom Form')
            return redirect(url_for('adminblueprint.manage_forms'))
    elif request.method == 'POST' and not form.validate_on_submit():
        flash(
            'Form Validation failed. Please check the form and try again',
            'error'
        )
        return render_template(
            'admin/manage_forms.html',
            form=form,
            all_forms=all_forms,
            error=error
        )
    else:
        if form_id:
            return render_template(
                'admin/_edit_custom_forms.html',
                form=form,
                form_id=form_id
            )
        else:
            return render_template(
                'admin/manage_forms.html',
                form=form,
                all_forms=all_forms
            )