Exemplo n.º 1
0
def admin_new_group():
    """
    Renders the new group page
    """
    user_db = UserDB()
    role_db = RoleDB()

    if current_user.is_authenticated() and current_user.is_active():
        all_activities = set()
        for m in role_db.get_roles(current_user):
            acts = role_db.get_activities(m.role_id)
            for act in acts:
                if acts[act]:
                    all_activities.add(act)

        if 'new_group' in all_activities:
            group = user_db.create_temp_empty_group()
            if 'cancel' in request.form:
                return redirect(url_for('admin_group.admin_group_page'))
            elif 'submit' in request.form:
                gname = request.form.get('group_name', None)
                group.name = gname
                group.description = request.form.get('description', None)
                if gname is not None and gname != '':
                    g = user_db.get_group(gname)
                    if g is None:
                        try:
                            group = user_db.add_group(group.name,
                                   group.description)
                            flash(_('Group "{0}" added.'.format(gname)))
                            return redirect(url_for(
                               'admin_group.admin_group_page'))
                        except DuplicateGroup:
                            flash(_(
                                'Group name "{0}" is already in use!'.format(
                                gname)),
                                'error')
                else:
                    flash(_("Group name cannot be empty!"), 'error')

            return render_template('admin_group.html', group=group,
                    state=get_state(), title=_('Edit Group'),
                    cancel_button=_('Cancel'), submit_button=_('Submit'),
                    can_edit_groups=True)
        else:
            return _not_auth()
    else:
        return _not_auth()