Esempio n. 1
0
def update_group(id):
    request_data = request.get_json()

    if not request_data:
        return bad_request('No input data provided.')

    try:
        data = GroupSchema().load(request_data)

        # check for existing group name
        group = Group.find_by_id(id)
        existing_group = Group.find_by_name(name=data.get('name'))

        if existing_group is not None:
            if existing_group.id != group.id:
                return bad_request(f'Group already exists.')

        group.name = data.get('name')
        group.description = data.get('description')
        group.save()

        return jsonify(GroupSchema().dump(group))

    # handle errors
    except ValidationError as err:
        return error_response(422, err.messages)
    except (exc.IntegrityError, ValueError):
        db.session.rollback()
        return server_error('Something went wrong, please try again.')
Esempio n. 2
0
def remove_group_members(grp_id):
    data = request.get_json()
    group = Group.find_by_id(grp_id)

    users = []
    for id in data.get('users'):
        user = User.find_by_id(id)
        users.append(user)

    group.remove_members(users)
    return jsonify(GroupSchema().dump(group))
Esempio n. 3
0
def remove_group_permissions(grp_id):
    data = request.get_json()
    group = Group.find_by_id(grp_id)

    perms = []
    for id in data.get('perms'):
        perm = Permission.find_by_id(id)
        perms.append(perm)

    group.remove_permissions(perms)
    return jsonify(GroupSchema().dump(group))
Esempio n. 4
0
def delete_group(id):
    try:
        group = Group.find_by_id(id)

        if not group:
            return not_found('Group does not exist.')

        group.delete()
        return {'message': 'Successfully deleted group.'}
    except Exception as error:
        return {'message': error}
Esempio n. 5
0
def get_group(id):
    """Get a single group"""
    group = Group.find_by_id(id)
    if group is None:
        return not_found('Group not found!')
    return jsonify(GroupSchema().dump(group))