'POST /api/groups
Creates a new group.'

@web.expose_api
def create(self, trans, payload, **kwd):

    log.info(('groups    payload%s\n' % payload))
    if (not trans.user_is_admin()):
        trans.response.status = 403
        return 'You    are    not    authorized    to    create    a    new    group.'
    name = payload.get('name', None)
    if (not name):
        trans.response.status = 400
        return 'Enter    a    valid    name'
    if trans.sa_session.query(trans.app.model.Group).filter((trans.app.model.Group.table.c.name == name)).first():
        trans.response.status = 400
        return 'A    group    with    that    name    already    exists'
    group = trans.app.model.Group(name=name)
    trans.sa_session.add(group)
    user_ids = payload.get('user_ids', [])
    for i in user_ids:
        log.info(('user_id:    %s\n' % i))
        log.info(('%s    %s\n' % (i, trans.security.decode_id(i))))
    users = [trans.sa_session.query(trans.model.User).get(trans.security.decode_id(i)) for i in user_ids]
    role_ids = payload.get('role_ids', [])
    roles = [trans.sa_session.query(trans.model.Role).get(trans.security.decode_id(i)) for i in role_ids]
    trans.app.security_agent.set_entity_group_associations(groups=[group], roles=roles, users=users)
    '\n                                #    Create    the    UserGroupAssociations\n                                for    user    in    users:\n                                                trans.app.security_agent.associate_user_group(    user,    group    )\n                                #    Create    the    GroupRoleAssociations\n                                for    role    in    roles:\n                                                trans.app.security_agent.associate_group_role(    group,    role    )\n                                '
    trans.sa_session.flush()
    encoded_id = trans.security.encode_id(group.id)
    item = group.to_dict(view='element', value_mapper={'id': trans.security.encode_id})
    item['url'] = url_for('group', id=encoded_id)
    return [item]
