Example #1
0
def menu_top_level_html_generate():
    settings = g.db.settings.find_one()
    sorted_menu = help.get_and_sort(settings.get('top_level_menu'), 'order')
    return render_template(
        'admin/_menu_parent.html',
        top_level_menu=sorted_menu
    )
Example #2
0
def demote_form_field():
    form_id = request.json.get('form_id')
    field_name = request.json.get('field_name')
    form = g.db.forms.find_one({'_id': ObjectId(form_id)})
    fields = help.get_and_sort(form.get('fields'), 'order')
    found = 0
    for item in fields:
        if item.get('name') == field_name:
            found = 1
            new_position = int(item.get('order')) + 1
            g.db.forms.update(
                {
                    '_id': ObjectId(form_id),
                    'fields.name': field_name
                }, {
                    '$inc': {
                        'fields.$.order': 1
                    }
                }
            )
        elif (found == 1) and (item.get('order') == new_position):
            g.db.forms.update(
                {
                    '_id': ObjectId(form_id),
                    'fields.name': item.get('name')
                }, {
                    '$inc': {
                        'fields.$.order': -1
                    }
                }
            )
    message = '%s field has been demoted' % field_name.title()
    return jsonify(message=message)
Example #3
0
def form_html_generate(form_id):
    custom_form = g.db.forms.find_one({'_id': ObjectId(form_id)})
    sorted_fields = help.get_and_sort(custom_form.get('fields'), 'order')
    return render_template(
        'admin/_form_fields.html',
        form_id=form_id,
        fields=sorted_fields
    )
Example #4
0
def menu_children_html_generate():
    settings = g.db.settings.find_one()
    sorted_menu = help.get_and_sort(
        settings.get('menu'),
        'parent_order',
        'order'
    )
    return render_template(
        'admin/_menu_children.html',
        menu_list=sorted_menu
    )
Example #5
0
def demote_top_level_menu_item():
    settings = g.db.settings.find_one()
    menu_item = request.json.get('name')
    sorted_menu = help.get_and_sort(settings.get('top_level_menu'), 'order')
    found, new_position = 0, None
    for item in sorted_menu:
        if item.get('name') == menu_item:
            found = 1
            new_position = item.get('order') + 1
            g.db.settings.update(
                {
                    'top_level_menu.name': menu_item
                }, {
                    '$inc': {
                        'top_level_menu.$.order': 1
                    }
                }
            )
        elif (found == 1) and (item.get('order') == new_position):
            g.db.settings.update(
                {
                    'top_level_menu.name': item.get('name')
                }, {
                    '$inc': {
                        'top_level_menu.$.order': -1
                    }
                }
            )
    help.change_top_level_order(
        settings,
        new_position,
        (new_position - 1),
        menu_item
    )
    settings = g.db.settings.find_one()
    new_sorted = help.get_and_sort(settings.get('top_level_menu'), 'order')
    message = 'Top Level Menu Item has been demoted'
    return jsonify(message=message)
Example #6
0
def demote_menu_item():
    settings = g.db.settings.find_one()
    menu_item = request.json.get('name')
    sorted_menu = help.get_and_sort(
        settings.get('menu'),
        'parent_order',
        'order'
    )
    found = 0
    parent = 'Not Found'
    for item in sorted_menu:
        if item.get('name') == menu_item:
            found = 1
            parent = item.get('parent')
            new_position = item.get('order') + 1
            g.db.settings.update(
                {
                    'menu.name': menu_item
                }, {
                    '$inc': {
                        'menu.$.order': 1
                    }
                }
            )
        elif (found == 1) and (item.get('parent') == parent) and \
                (item.get('order') == new_position):
            g.db.settings.update(
                {
                    'menu.name': item.get('name')
                }, {
                    '$inc': {
                        'menu.$.order': -1
                    }
                }
            )
    message = 'Menu Item has been demoted'
    return jsonify(message=message)
Example #7
0
def manage_form_fields(form_id, field_name=None):
    error = True
    custom_form = g.db.forms.find_one({'_id': ObjectId(form_id)})
    sorted_fields = help.get_and_sort(custom_form.get('fields'), 'order')
    title = "Manage Fields for %s" % custom_form.get('display_name').title()
    if field_name:
        fields = g.db.forms.find_one(
            {
                '_id': ObjectId(form_id),
                'fields': {
                    '$elemMatch': {
                        'name': field_name
                    }
                }
            }
        ).get('fields')

        edit_field = None
        for field in fields:
            if field.get('name') == field_name:
                edit_field = field
                break

        if edit_field:
            form = forms.BuildCustomForm(
                form_id=custom_form.get('_id'),
                name=field_name,
                label=edit_field.get('label'),
                field_type=edit_field.get('field_type'),
                default=edit_field.get('default'),
                default_value=edit_field.get('default_value'),
                style_id=edit_field.get('style_id'),
                description=edit_field.get('description'),
                required=edit_field.get('required'),
                active=edit_field.get('active'),
                field_choices=edit_field.get('field_choices'),
                order=edit_field.get('order')
            )
    else:
        form = forms.BuildCustomForm(form_id=custom_form.get('_id'))

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

        if field_name:
            if not sani_name == field_name:
                if g.db.forms.find(
                    {
                        '_id': ObjectId(form_id),
                        'fields': {
                            '$elemMatch': {
                                'name': sani_name
                            }
                        }
                    }
                ).count() > 0:
                    flash(
                        'Field name already exists, '
                        'please check the name and try again',
                        'error'
                    )
                    return render_template(
                        'admin/manage_form_fields.html',
                        form=form,
                        title=title,
                        fields=sorted_fields,
                        form_id=form_id,
                        error=error
                    )
        else:
            if g.db.forms.find(
                {
                    '_id': ObjectId(form_id),
                    'fields.name': sani_name
                }
            ).count() > 0:
                flash(
                    'Field name already exists, '
                    'please check the name and try again',
                    'error'
                )
                return render_template(
                    'admin/manage_form_fields.html',
                    form=form,
                    title=title,
                    fields=sorted_fields,
                    form_id=form_id,
                    error=error
                )

        if field_name:
            g.db.forms.update(
                {
                    '_id': ObjectId(form_id),
                    'fields.name': field_name
                },
                {
                    '$set': {
                        'fields.$.name': sani_name,
                        'fields.$.label': request.form.get('label'),
                        'fields.$.field_type': request.form.get('field_type'),
                        'fields.$.field_choices': request.form.get(
                            'field_choices'
                        ),
                        'fields.$.default': bool(request.form.get('default')),
                        'fields.$.default_value': request.form.get(
                            'default_value'
                        ),
                        'fields.$.style_id': request.form.get('style_id'),
                        'fields.$.required': bool(
                            request.form.get('required')
                        ),
                        'fields.$.active': bool(request.form.get('active')),
                        'fields.$.order': int(request.form.get('order')),
                        'fields.$.description': request.form.get('description')
                    }
                }
            )
            flash('Field successfully updated')
            return redirect(
                url_for(
                    'adminblueprint.manage_form_fields',
                    form_id=form_id
                )
            )
        else:
            g.db.forms.update(
                {
                    '_id': ObjectId(form_id)},
                {
                    "$push": {
                        'fields': {
                            'name': sani_name,
                            'label': request.form.get('label'),
                            'field_type': request.form.get('field_type'),
                            'field_choices': request.form.get('field_choices'),
                            'default': bool(request.form.get('default')),
                            'default_value': request.form.get('default_value'),
                            'style_id': request.form.get('style_id'),
                            'required': bool(request.form.get('required')),
                            'active': bool(request.form.get('active')),
                            'order': help.get_form_field_order(form_id),
                            'description': request.form.get('description')
                        }
                    }
                }
            )
            flash('Field successfully added to form')
            return redirect(
                url_for(
                    'adminblueprint.manage_form_fields',
                    form_id=form_id
                )
            )
    elif request.method == 'POST' and not form.validate_on_submit():
        flash(
            'Form validation error. Please check the form and try again',
            'error'
        )
        return render_template(
            'admin/manage_form_fields.html',
            form=form,
            title=title,
            fields=sorted_fields,
            form_id=form_id,
            error=error
        )
    else:
        if form_id and field_name:
            show_default_field, show_field_type = False, False
            if (
                field.get('field_type') == 'SelectField' or
                field.get('field_type') == 'RadioField' or
                field.get('field_type') == 'SelectMultipleField'
            ):
                show_field_type = True

            if field.get('default'):
                show_default_field = True

            return render_template(
                'admin/_edit_custom_fields.html',
                form=form,
                form_id=form_id,
                field_name=field_name,
                show_default_field=show_default_field,
                show_field_type=show_field_type
            )
        else:
            return render_template(
                'admin/manage_form_fields.html',
                form=form,
                title=title,
                fields=sorted_fields,
                form_id=form_id
            )
Example #8
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
            )