Пример #1
0
def admin_get_user(id_, token_info=None, user=None):
    """Return the current user"""
    check_user_is_admin(user)
    requested_user = User.query.get(id_)
    if not requested_user:
        abort(404)
    return _hide_sensitive_fields(requested_user.to_dict(with_projects=True))
Пример #2
0
def admin_get_user_list(filter_=None,
                        page=1,
                        page_size=25,
                        token_info=None,
                        user=None):
    """
    Return a list of users (only superadmins can run this function)
    """
    check_user_is_admin(user)
    query = User.query

    if filter_:
        for filter_string in filter_:
            filter_clause = convert_filter(filter_string, User)
            if filter_clause is not None:
                query = query.filter(filter_clause)

    offset = (page * page_size) - page_size
    total_items = query.count()
    total_pages = (total_items //
                   page_size) + (1 if total_items % page_size > 0 else 0)
    users = query.order_by(
        User.email.asc()).offset(offset).limit(page_size).all()
    return {
        "users": [
            _hide_sensitive_fields(user.to_dict(with_projects=True))
            for user in users
        ],
        "pagination": {
            "page": page,
            "pageSize": page_size,
            "totalItems": total_items,
            "totalPages": total_pages,
        },
    }
Пример #3
0
def admin_delete_user(id_, token_info=None, user=None):
    """Delete a single user"""
    check_user_is_admin(user)
    requested_user = User.query.get(id_)
    if not requested_user:
        abort(404)
    session.delete(requested_user)
    session.commit()
    return "OK", 200
Пример #4
0
def admin_add_user(new_user=None, token_info=None, user=None):
    """Create a new user in the system"""
    check_user_is_admin(user)
    if not connexion.request.is_json:
        return "Bad request, JSON required", 400
    new_user = User.from_dict(**connexion.request.get_json())
    session.add(new_user)
    session.commit()
    return _hide_sensitive_fields(new_user.to_dict()), 201
Пример #5
0
def admin_delete_project(id_, token_info=None, user=None):
    """Delete a single project"""
    check_user_is_admin(user)
    project = Project.query.get(id_)
    if not project:
        abort(404)
    session.delete(project)
    session.commit()
    return "OK", 200
Пример #6
0
def admin_get_project(id_, token_info=None, user=None):
    """Get a single project by ID

    :param id: ID of test project
    :type id: str

    :rtype: Project
    """
    check_user_is_admin(user)
    project = Project.query.get(id_)
    if not project:
        project = Project.query.filter(Project.name == id_).first()
    if not project:
        abort(404)
    return project.to_dict(with_owner=True)
Пример #7
0
def admin_get_project_list(filter_=None,
                           owner_id=None,
                           group_id=None,
                           page=1,
                           page_size=25,
                           token_info=None,
                           user=None):
    """Get a list of projects

    :param owner_id: Filter projects by owner ID
    :type owner_id: str
    :param group_id: Filter projects by group ID
    :type group_id: str
    :param limit: Limit the projects
    :type limit: int
    :param offset: Offset the projects
    :type offset: int

    :rtype: List[Project]
    """
    check_user_is_admin(user)
    query = Project.query

    if filter_:
        for filter_string in filter_:
            filter_clause = convert_filter(filter_string, Project)
            if filter_clause is not None:
                query = query.filter(filter_clause)
    if owner_id:
        query = query.filter(Project.owner_id == owner_id)
    if group_id:
        query = query.filter(Project.group_id == group_id)

    offset = (page * page_size) - page_size
    total_items = query.count()
    total_pages = (total_items //
                   page_size) + (1 if total_items % page_size > 0 else 0)
    projects = query.offset(offset).limit(page_size).all()
    return {
        "projects": [project.to_dict(with_owner=True) for project in projects],
        "pagination": {
            "page": page,
            "pageSize": page_size,
            "totalItems": total_items,
            "totalPages": total_pages,
        },
    }
Пример #8
0
def admin_update_user(id_, user_info=None, token_info=None, user=None):
    """Update a single user in the system"""
    check_user_is_admin(user)
    if not connexion.request.is_json:
        return "Bad request, JSON required", 400
    user_dict = connexion.request.get_json()
    projects = user_dict.pop("projects", [])
    requested_user = User.query.get(id_)
    if not requested_user:
        abort(404)
    requested_user.update(user_dict)
    requested_user.projects = [
        Project.query.get(project["id"]) for project in projects
    ]
    session.add(requested_user)
    session.commit()
    return _hide_sensitive_fields(requested_user.to_dict())
Пример #9
0
def admin_update_project(id_, project=None, token_info=None, user=None):
    """Update a project

    :param id: ID of test project
    :type id: str
    :param body: Project
    :type body: dict | bytes

    :rtype: Project
    """
    check_user_is_admin(user)
    if not connexion.request.is_json:
        return "Bad request, JSON required", 400
    if not is_uuid(id_):
        id_ = convert_objectid_to_uuid(id_)
    project = Project.query.get(id_)

    if not project:
        abort(404)

    # Grab the fields from the request
    project_dict = connexion.request.get_json()

    # If the "owner" field is set, ignore it
    project_dict.pop("owner", None)

    # handle updating users separately
    for username in project_dict.pop("users", []):
        user_to_add = User.query.filter_by(email=username).first()
        if user_to_add and user_to_add not in project.users:
            project.users.append(user_to_add)

    # Make sure the project owner is in the list of users
    if project_dict.get("owner_id"):
        owner = User.query.get(project_dict["owner_id"])
        if owner and owner not in project.users:
            project.users.append(owner)

    # update the rest of the project info
    project.update(project_dict)
    session.add(project)
    session.commit()
    return project.to_dict()
Пример #10
0
def admin_add_project(project=None, token_info=None, user=None):
    """Create a project

    :param body: Project
    :type body: dict | bytes

    :rtype: Project
    """
    check_user_is_admin(user)
    if not connexion.request.is_json:
        return "Bad request, JSON required", 400
    project = Project.from_dict(**connexion.request.get_json())
    user = User.query.get(user)
    if user:
        project.owner = user
        project.users.append(user)
    session.add(project)
    session.commit()
    return project.to_dict(), 201