Ejemplo n.º 1
0
def get_project_activities(project_id: str, user: str,
                           **filters) -> Union[int, None, List[Activity]]:
    """
    Return activities of all users in a project
    :param project_id: an id of the project
    :param user: a reference to the user asking, should have a manager role
    :param filters: filters for find_activities method
    :return:
    """
    if not (project_id or user):
        return None

    project = Project.objects(id=project_id).first()
    if not project:
        return None

    if user not in project.managers:
        return None

    if not project.users:
        return []
    else:
        return find_activities(user_ids=project.users, **filters)
Ejemplo n.º 2
0
def activity_find():
    """
    Find activities
    ---
    get:
        summary: Find activities.
        description: Find activities of current user.
        parameters:
            -   name: offset
                in: query
                required: true
                type: integer
                description: a number of activities to skip
            -   name: amount_to_return
                in: query
                required: true
                type: integer
                description: amount of activities to return, max is 1000
            -   name: filters
                in: query
                required: false
                type: string
                description: filters for activity, example {"activity_type": "os"}
            -   name: start_time
                in: query
                required: false
                type: string
                description: minimum start time of an activity
            -   name: end_time
                in: query
                required: false
                type: string
                description: maximum end time of an activity
        responses:
            404:
                description: Activities were not found
            400:
                description: Wrong format
            200:
                description: A list of activities was returned
    """
    data = flask.request.args
    offset: int = int(data.get(OFFSET_KEY, 0))
    amount_to_return: int = min(int(data.get(AMOUNT_TO_RETURN_KEY, 100)), 1000)
    filters = data.get(FILTERS_KEY, {})
    start_time = data.get(START_TIME_KEY, None)
    end_time = data.get(END_TIME_KEY, None)

    if not isinstance(filters, dict):
        try:
            filters = json.loads(filters)
        except Exception:
            return make_response(jsonify({MESSAGE_KEY: 'Wrong format'}),
                                 HTTPStatus.BAD_REQUEST)

    activities = find_activities([current_user.id],
                                 offset=offset,
                                 items_to_return=amount_to_return,
                                 filters=filters,
                                 start_time=start_time,
                                 end_time=end_time)
    if activities is None:
        return make_response(
            jsonify({MESSAGE_KEY: 'Failed to fetch activities'}),
            HTTPStatus.INTERNAL_SERVER_ERROR)
    if activities == -1:
        return make_response(
            jsonify({MESSAGE_KEY: 'Wrong format for filters'}),
            HTTPStatus.BAD_REQUEST)

    if not activities:
        return make_response(
            jsonify({MESSAGE_KEY:
                     'Activities of current user were not found'}),
            HTTPStatus.NOT_FOUND)
    activities_list = [{k: str(v)
                        for k, v in activity.to_mongo().items()}
                       for activity in activities]

    return make_response(
        jsonify({
            MESSAGE_KEY: 'Success',
            ACTIVITIES_KEY: activities_list
        }), HTTPStatus.OK)