Exemple #1
0
def activity_delete():
    """
    Delete an activity
    ---
    delete:
        summary: Delete an activity.
        description: Delete a specific activity from current user's history.
        parameters:
            -   name: activity_id
                in: formData
                required: true
                type: integer
                description: an id of the activity
        responses:
            400:
                description: Parameters are not correct
            404:
                description: Activity with this id was not found
            200:
                description: Activity was deleted
    """
    data = flask.request.json if flask.request.json else flask.request.form
    activity_id: str = data.get(ACTIVITY_ID_KEY)

    if not activity_id:
        return make_response((jsonify({MESSAGE_KEY: 'Empty data'},
                                      HTTPStatus.BAD_REQUEST)))

    result = delete_activity(activity_id)
    if result == 0:
        return make_response(
            jsonify({MESSAGE_KEY: 'Activity with this id was not found'}),
            HTTPStatus.NOT_FOUND)
    if not result:
        return make_response(
            jsonify({MESSAGE_KEY: 'Failed to delete activity'}),
            HTTPStatus.INTERNAL_SERVER_ERROR)

    return make_response(jsonify({MESSAGE_KEY: 'Success'}), HTTPStatus.OK)
Exemple #2
0
def activity_add():
    """
    Add an activity
    ---
    post:
        summary: Add an activity.
        description: Add an activity or multiple activities to the current user.
        parameters:
            -   name: activity
                in: formData
                required: true
                description: json containing all specified parameters
                type: string
            -   name: activities
                in: formData
                required: false
                description: List containing activity_data
                type: array
                items:
                    type: string
            -   name: start_time
                in: formData
                required: true
                type: string
                description: a start time of the activity
            -   name: end_time
                in: formData
                required: true
                type: string
                description: an end time of the activity
            -   name: executable_name
                in: formData
                required: true
                type: string
                description: a name of the current executable
            -   name: browser_url
                in: formData
                required: false
                type: string
                description: a url opened during the activity
            -   name: browser_title
                in: formData
                required: false
                type: string
                description: a title of the browsing window
            -   name: ip_address
                in: formData
                required: true
                type: string
                description: an ip address of the user
            -   name: mac_address
                in: formData
                required: true
                type: string
                description: an mac address of the user
            -   name: idle_activity
                in: formData
                required: false
                type: boolean
                description: if activity is an idle one
            -   name: activity_type
                in: formData
                required: false
                type: string
                description: a type of activity collected (os, eclipse tab and etc)
        responses:
            400:
                description: Parameters are not correct
            201:
                description: Activity was added
    """
    data = flask.request.json if flask.request.json else flask.request.form
    activity_data = data.get(ACTIVITY_KEY)
    if not isinstance(activity_data, dict):
        try:
            activity_data = json.loads(activity_data)
        except Exception:
            return make_response(jsonify({MESSAGE_KEY: 'Wrong format'}),
                                 HTTPStatus.BAD_REQUEST)

    if ACTIVITIES_KEY in activity_data:
        #  Add multiple activities
        activities = [(activity, current_user.to_dbref())
                      for activity in activity_data.get(ACTIVITIES_KEY, [])]
        all_result = execute_function_in_parallel(add_activity, activities)
        result = 1
        for part_result in all_result:
            if not part_result:
                result = part_result
        if result:
            result = all_result
        else:
            # Delete those activities that were added
            for part_result in all_result:
                if part_result:
                    delete_activity(part_result)
    else:
        result = add_activity(activity_data, current_user.to_dbref())

    if not result:
        return make_response(
            jsonify({MESSAGE_KEY: 'Failed to create activity'}),
            HTTPStatus.INTERNAL_SERVER_ERROR)

    return make_response(
        jsonify({
            MESSAGE_KEY: 'Success',
            ACTIVITY_ID_KEY: result
        }), HTTPStatus.CREATED)