Ejemplo n.º 1
0
def put_job_without_id():
    """PUT Job without id

    Create a new Job entry without specifying the job_id. This is the
    preferred method for creating new jobs, as the job_id input has to match
    the MongoDB id pattern.

    Returns
    -------
    response : dict
        JSON response to this PUT Request

    """
    # get the data
    data = request.get_json()
    if data is None:
        data = dict()

    # if a user is logged in, bind the job to this user
    _filter = get_user_bound_filter(roles=['admin'])
    data.update(_filter)

    # create the Job
    try:
        job = Job(**data)
        job.create()
    except Exception as e:
        return jsonify({'status': 500, 'message': str(e)}), 500

    return jsonify(job.to_dict(stringify=True)), 201
Ejemplo n.º 2
0
    def put(self, job_id):
        """PUT request

        Handle a PUT request. A new job will be created using the passed
        data. If job_id is given, The function will try to create a Job of
        that given job_id.

        Parameters
        ----------
        job_id : string
            ObjectId of the Job to be created. Has to be a 12 byte name or a
            24 byte hex.

        Returns
        -------
        response : dict
            JSON response to this PUT Request

        """
        # check for existing job id
        if Job.id_exists(job_id):
            return {
                'status': 409,
                'message': 'A job of id %s already exists' % job_id
            }, 409

        # get the passed data
        data = request.get_json()
        if data is None:
            data = dict()

        # if a user is logged in, bind the job to this user
        _filter = get_user_bound_filter(roles=['admin'])
        data.update(_filter)

        # create the Job
        try:
            job = Job(_id=job_id, **data)
            job.create()
        except InvalidId as e:
            return {'status': 505, 'message': str(e)}, 505
        except Exception as e:
            return {'status': 500, 'message': str(e)}, 500

        # return
        return job.to_dict(stringify=True), 201