예제 #1
0
파일: views.py 프로젝트: dev-ace/pitchfork
def manage_admins():
    error = True
    settings = check_and_initialize()
    title = "Manage Administrators"
    form = help.deploy_custom_form('add_administrator')
    if request.method == 'POST' and form.validate_on_submit():
        found = g.db.settings.find_one(
            {
                'administrators.admin': request.form.get(
                    'administrator'
                )
            }
        )
        if found:
            flash('User is already in the Administrators List', 'error')
            form.administrator.errors.append('Duplicate user')
            return render_template(
                'admin/manage_admins.html',
                error=error,
                title=title,
                form=form,
                settings=settings
            )

        g.db.settings.update(
            {
                '_id': settings.get('_id')
            }, {
                '$push': {
                    'administrators': {
                        'admin': request.form.get('administrator'),
                        'admin_name': request.form.get('full_name').strip()
                    }
                }
            }
        )
        flash('User has been added as an Administrator')
        return redirect(url_for('adminblueprint.manage_admins'))
    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_admins.html',
                error=error,
                title=title,
                form=form,
                settings=settings
            )

    else:
        return render_template(
            'admin/manage_admins.html',
            title=title,
            form=form,
            settings=settings
        )
예제 #2
0
def manage_roles(edit_role_name=None):
    settings = check_and_initialize()
    form = helpers.deploy_custom_form('manage_roles')
    if request.method == 'POST' and form.validate_on_submit():
        role_name = helpers.slug(request.form.get('display_name'))
        existing_role = g.db.settings.find_one(
            {
                'roles.name': role_name
            }
        )
        if existing_role:
            flash(
                'Role already exists, please check the name and try again',
                'error'
            )
            form.display_name.errors.append('Duplicate role')
            return render_template(
                'admin/manage_roles.html',
                form=form,
                roles=settings.get('roles')
            )
        else:
            g.db.settings.update(
                {
                    '_id': settings.get('_id')
                }, {
                    '$push': {
                        'roles': {
                            'name': role_name,
                            'display_name': helpers.normalize(
                                request.form.get('display_name')
                            ),
                            'active': bool(request.form.get('status'))
                        }
                    }
                }
            )
            flash('Role successfully Added', 'success')
            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_roles.html',
            form=form,
            roles=settings.get('roles')
        )
    else:
        return render_template(
            'admin/manage_roles.html',
            form=form,
            roles=settings.get('roles')
        )
예제 #3
0
파일: views.py 프로젝트: dev-ace/pitchfork
def general_settings():
    error = True
    settings = check_and_initialize()
    title = "Application Settings"
    form = help.deploy_custom_form(
        'application_settings',
        application_title=settings.get('application_title'),
        application_email=settings.get('application_email'),
        application_footer=settings.get('application_footer'),
        application_well=settings.get('application_well')
    )
    if request.method == 'POST':
        g.db.settings.update(
            {
                '_id': settings.get('_id')
            }, {
                '$set': {
                    'application_title': request.form.get(
                        'application_title'
                    ),
                    'application_email': request.form.get(
                        'application_email'
                    ),
                    'application_footer': request.form.get(
                        'application_footer'
                    ),
                    'application_well': request.form.get(
                        'application_well'
                    )
                }
            }
        )
        flash('General Settings have been updated')
        return redirect(url_for('adminblueprint.general_settings'))
    else:
        return render_template(
            'admin/manage_settings.html',
            title=title,
            form=form,
            settings=settings
        )
예제 #4
0
파일: views.py 프로젝트: dev-ace/pitchfork
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
            )
예제 #5
0
def utility_processor():
    settings = g.db.settings.find_one()
    if (settings is None):
        settings = check_and_initialize()

    def app_title():
        return settings.get('application_title')

    def app_well():
        return settings.get('application_well')

    def app_email():
        return settings.get('application_email')

    def app_footer():
        return settings.get('application_footer')

    def check_app_email():
        if settings.get('application_email') and session.get('username'):
            return True
        return False

    def check_if_admin():
        return permissions.is_admin()

    def get_menu():
        menu_items = settings.get('menu')
        active_menu = []
        for menu in menu_items:
            if menu.get('active'):
                if menu.get('view_permissions') == "all":
                    active_menu.append(menu)
                elif (
                    menu.get('view_permissions') == "logged_in" and
                    not (session.get('username') is None)
                ):
                    active_menu.append(menu)
                elif (
                    menu.get('view_permissions') == "administrators" and
                    permissions.is_admin()
                ):
                    active_menu.append(menu)
        return sorted(active_menu, key=itemgetter('parent_order', 'order'))

    def get_parent_name(parent_slug):
        parent_slug = re.sub('\s+', '_', parent_slug.lower())
        top_menu = g.db.settings.find_one(
            {
                'top_level_menu.slug': parent_slug
            }, {
                'top_level_menu.$': 1, '_id': 0
            }
        )
        if top_menu:
            return top_menu.get('top_level_menu')[0].get('name')

    return dict(
        app_title=app_title(),
        app_footer=app_footer(),
        app_email=app_email(),
        app_well=app_well(),
        check_app_email=check_app_email(),
        get_menu=get_menu(),
        check_if_admin=check_if_admin(),
        get_parent_name=get_parent_name
    )