Пример #1
0
def delete_order():
    order_id = request.args.get('orderId')

    # Check if the request parameter is valid
    validator = OrderIdValidator(order_id)
    if not validator.is_order_id_valid():
        return validator.get_error_json()

    try:
        order = Orders.select(Orders.c.orderId == order_id).execute().first()
        if not order:
            return jsonify({
                'status': 'error',
                'message': 'No orders with orderId: ' + order_id
            })
        order_items = BelongsTo.select(
            BelongsTo.c.orderId == order_id).execute().fetchall()
        Orders.delete().where(Orders.c.orderId == order_id).execute()
        if order_items:
            for item in order_items:
                OrderItems.delete().where(
                    OrderItems.c.refCode == item['refCode']).execute()
        return jsonify({'status': 'ok'})
    except exc.SQLAlchemyError as e:
        return jsonify({'status': 'error', 'message': e.message})
Пример #2
0
def create_dip():
    """
        Must receive JSON like this:
        {
            "orderId": "c1b1c16e-2c00-474f-b99b-42019b3eaeed"
        }
    """

    # TODO: refactor (code duplication)
    # Check the posted JSON
    mandatory_keys = ['orderId']
    if not request.json:
        abort(400)
    if not json_keys_valid(request.json, mandatory_keys):
        return jsonify({'success': False, 'message': 'Request JSON must contain the keys ' + ', '.join(mandatory_keys)}), 400
    order_id = request.json['orderId']
    validator = OrderIdValidator(order_id)
    if not validator.is_order_id_valid():
        return validator.get_error_json(), 400

    # Return error if the order does not exists or is not "processing"
    try:
        order = Orders.select(Orders.c.orderId == order_id).execute().first()
        if not order:
            return jsonify({'success': False, 'message': 'No orders with orderId: ' + order_id}), 404
        else:
            # Check if the orderStatus is "processing"
            order_dict = sql_query_to_dict(order)
            if not order_dict['orderStatus'] == 'processing':
                return jsonify({'success': False, 'message': 'The order must have status \'processing\' before DIP the creation can be initiated'}), 412
        
        # Login in to earkweb and get cookies
        try:
            earkweb_session = get_session(EARKWEB_LOGIN_URL)
        except Exception as e:
            return jsonify({'success': False, 'message': e.message}), 500
        
        # Get order details and construct payload
        process_id = get_order_value(order_id, 'processId')
        payload = {'process_id': process_id}
        
        resp = earkweb_session.post(CREATE_DIP, json = payload, headers = {'Referer':EARKWEB_LOGIN_URL})
        if resp.status_code == 201:
            # Put data into DB
            json = resp.json()
            Orders.update().where(Orders.c.orderId == order_id).values({'jobId': json['jobid'], 'orderStatus': 'packaging'}).execute()
            print 'Order status: packaging'
            return jsonify(resp.json()), 201
        else:
            return jsonify(resp.json()), resp.status_code
    
    except exc.SQLAlchemyError as e:
        return jsonify({'success': False, 'message': e.message}), 500
Пример #3
0
def get_order_data():
    order_id = request.args.get('orderId')

    # Check if the request parameter is valid
    validator = OrderIdValidator(order_id)
    if not validator.is_order_id_valid():
        return validator.get_error_json()

    try:
        order_dict = get_order_data_helper(order_id)
        return jsonify(order_dict)
    except exc.SQLAlchemyError as e:
        return jsonify({'status': 'error', 'message': e.message})
Пример #4
0
def get_order_data():
    order_id = request.args.get('orderId')
    
    # Check if the request parameter is valid
    validator = OrderIdValidator(order_id)
    if not validator.is_order_id_valid():
        return validator.get_error_json()
    
    try:
        order_dict = get_order_data_helper(order_id)
        return jsonify(order_dict)
    except exc.SQLAlchemyError as e:
        return jsonify({'status': 'error',
                        'message': e.message})
Пример #5
0
def delete_order():
    order_id = request.args.get('orderId')
    
    # Check if the request parameter is valid
    validator = OrderIdValidator(order_id)
    if not validator.is_order_id_valid():
        return validator.get_error_json()
    
    try:
        order = Orders.select(Orders.c.orderId == order_id).execute().first()
        if not order:
            return jsonify({'status':'error', 'message': 'No orders with orderId: ' + order_id})
        order_items = BelongsTo.select(BelongsTo.c.orderId == order_id).execute().fetchall()
        Orders.delete().where(Orders.c.orderId == order_id).execute()
        if order_items:
            for item in order_items:
                OrderItems.delete().where(OrderItems.c.refCode == item['refCode']).execute()
        return jsonify({'status': 'ok'})
    except exc.SQLAlchemyError as e:
        return jsonify({'status': 'error',
                        'message': e.message})
Пример #6
0
def submit_order():
    """
    Must receive JSON like this
    {
        "orderId": "0924e01714244d9693cc3b1774e6b88e"
    }
    where orderId is the unique id of the order
    """

    # TODO: refactor (code duplication)
    # Check the posted JSON
    mandatory_keys = ['orderId']
    if not request.json:
        abort(400)
    if not json_keys_valid(request.json, mandatory_keys):
        return jsonify({'success': False, 'message': 'Request JSON must contain the keys ' + ', '.join(mandatory_keys)}), 400
    order_id = request.json['orderId']
    validator = OrderIdValidator(order_id)
    if not validator.is_order_id_valid():
        return validator.get_error_json(), 400

    # Return error if the order does not exists
    try:
        order = Orders.select(Orders.c.orderId == order_id).execute().first()
        if not order:
            return jsonify({'success': False, 'message': 'No orders with orderId: ' + order_id})
        else:
            # Check that orderStatus is "new"
            order_status = sql_query_to_dict(order)['orderStatus']
            if order_status != 'new':
                return jsonify({'success': False, 'message': 'Can only submit order with status \'new\''}), 412
    
        # Login in to earkweb and get cookies
        # TODO: should be able to reuse a session
        try:
            earkweb_session = get_session(EARKWEB_LOGIN_URL)
        except Exception as e:
            return jsonify({'success': False, 'message': e.message}), 500
        
        # Get order details and construct payload
        package_ids = get_packageIds(order_id)
        order_title = get_order_value(order_id, 'title') + '_' + uuid.uuid4().hex
        payload = {'order_title': order_title, 'aip_identifiers': package_ids}
        print payload
        
        # Post the order    
        resp = earkweb_session.post(SUBMIT_ORDER_URL, json = payload, headers = {'Referer':EARKWEB_LOGIN_URL})
        if (resp.status_code != 200):
            return jsonify({'success': False,
                            'message': 'There was a problem submitting the order to ' + SUBMIT_ORDER_URL}), resp.status_code
        else:
            # Check the JSON response from earkweb
            json = resp.json() # dict
            if 'error' in json.keys():
                # Set order status to error in the DB 
                Orders.update().where(Orders.c.orderId == order_id).values({'orderStatus': 'error'}).execute()
                return jsonify({'success': False, 'message':json['error']}), 500
            else:
                # Update order status in the DB
                Orders.update().where(Orders.c.orderId == order_id).values({'processId': json['process_id']}).execute()
                
                # Initiate the first three earkweb AIP to DIP conversion tasks
                payload = {'process_id': json['process_id']}
                resp2 = earkweb_session.post(PREPARE_DIP, json = payload, headers = {'Referer':EARKWEB_LOGIN_URL})
                json2 = resp2.json()
                if resp2.status_code == 201:
                    # Update jobId in the DB
                    Orders.update().where(Orders.c.orderId == order_id).values({'jobId': json2['jobid'], 'orderStatus':'submitted'}).execute()
                    print 'Order status: submitted'
                    return jsonify({'success': True, 'message':'The order was successfully submitted'})
                else:
                    # Set order status to error in the DB 
                    Orders.update().where(Orders.c.orderId == order_id).values({'orderStatus': 'error'}).execute()
                    print 'Order status: error submitting'
                    return jsonify(resp.json()), resp.status_code

    except exc.SQLAlchemyError as e:
        return jsonify({'success': False, 'message': e.message})
Пример #7
0
def create_dip():
    """
        Must receive JSON like this:
        {
            "orderId": "c1b1c16e-2c00-474f-b99b-42019b3eaeed"
        }
    """

    # TODO: refactor (code duplication)
    # Check the posted JSON
    mandatory_keys = ['orderId']
    if not request.json:
        abort(400)
    if not json_keys_valid(request.json, mandatory_keys):
        return jsonify({
            'success':
            False,
            'message':
            'Request JSON must contain the keys ' + ', '.join(mandatory_keys)
        }), 400
    order_id = request.json['orderId']
    validator = OrderIdValidator(order_id)
    if not validator.is_order_id_valid():
        return validator.get_error_json(), 400

    # Return error if the order does not exists or is not "processing"
    try:
        order = Orders.select(Orders.c.orderId == order_id).execute().first()
        if not order:
            return jsonify({
                'success': False,
                'message': 'No orders with orderId: ' + order_id
            }), 404
        else:
            # Check if the orderStatus is "processing"
            order_dict = sql_query_to_dict(order)
            if not order_dict['orderStatus'] == 'processing':
                return jsonify({
                    'success':
                    False,
                    'message':
                    'The order must have status \'processing\' before DIP the creation can be initiated'
                }), 412

        # Login in to earkweb and get cookies
        try:
            earkweb_session = get_session(EARKWEB_LOGIN_URL)
        except Exception as e:
            return jsonify({'success': False, 'message': e.message}), 500

        # Get order details and construct payload
        process_id = get_order_value(order_id, 'processId')
        payload = {'process_id': process_id}

        resp = earkweb_session.post(CREATE_DIP,
                                    json=payload,
                                    headers={'Referer': EARKWEB_LOGIN_URL})
        if resp.status_code == 201:
            # Put data into DB
            json = resp.json()
            Orders.update().where(Orders.c.orderId == order_id).values({
                'jobId':
                json['jobid'],
                'orderStatus':
                'packaging'
            }).execute()
            print 'Order status: packaging'
            return jsonify(resp.json()), 201
        else:
            return jsonify(resp.json()), resp.status_code

    except exc.SQLAlchemyError as e:
        return jsonify({'success': False, 'message': e.message}), 500
Пример #8
0
def submit_order():
    """
    Must receive JSON like this
    {
        "orderId": "0924e01714244d9693cc3b1774e6b88e"
    }
    where orderId is the unique id of the order
    """

    # TODO: refactor (code duplication)
    # Check the posted JSON
    mandatory_keys = ['orderId']
    if not request.json:
        abort(400)
    if not json_keys_valid(request.json, mandatory_keys):
        return jsonify({
            'success':
            False,
            'message':
            'Request JSON must contain the keys ' + ', '.join(mandatory_keys)
        }), 400
    order_id = request.json['orderId']
    validator = OrderIdValidator(order_id)
    if not validator.is_order_id_valid():
        return validator.get_error_json(), 400

    # Return error if the order does not exists
    try:
        order = Orders.select(Orders.c.orderId == order_id).execute().first()
        if not order:
            return jsonify({
                'success': False,
                'message': 'No orders with orderId: ' + order_id
            })
        else:
            # Check that orderStatus is "new"
            order_status = sql_query_to_dict(order)['orderStatus']
            if order_status != 'new':
                return jsonify({
                    'success':
                    False,
                    'message':
                    'Can only submit order with status \'new\''
                }), 412

        # Login in to earkweb and get cookies
        # TODO: should be able to reuse a session
        try:
            earkweb_session = get_session(EARKWEB_LOGIN_URL)
        except Exception as e:
            return jsonify({'success': False, 'message': e.message}), 500

        # Get order details and construct payload
        package_ids = get_packageIds(order_id)
        order_title = get_order_value(order_id,
                                      'title') + '_' + uuid.uuid4().hex
        payload = {'order_title': order_title, 'aip_identifiers': package_ids}
        print payload

        # Post the order
        resp = earkweb_session.post(SUBMIT_ORDER_URL,
                                    json=payload,
                                    headers={'Referer': EARKWEB_LOGIN_URL})
        if (resp.status_code != 200):
            return jsonify({
                'success':
                False,
                'message':
                'There was a problem submitting the order to ' +
                SUBMIT_ORDER_URL
            }), resp.status_code
        else:
            # Check the JSON response from earkweb
            json = resp.json()  # dict
            if 'error' in json.keys():
                # Set order status to error in the DB
                Orders.update().where(Orders.c.orderId == order_id).values({
                    'orderStatus':
                    'error'
                }).execute()
                return jsonify({
                    'success': False,
                    'message': json['error']
                }), 500
            else:
                # Update order status in the DB
                Orders.update().where(Orders.c.orderId == order_id).values({
                    'processId':
                    json['process_id']
                }).execute()

                # Initiate the first three earkweb AIP to DIP conversion tasks
                payload = {'process_id': json['process_id']}
                resp2 = earkweb_session.post(
                    PREPARE_DIP,
                    json=payload,
                    headers={'Referer': EARKWEB_LOGIN_URL})
                json2 = resp2.json()
                if resp2.status_code == 201:
                    # Update jobId in the DB
                    Orders.update().where(Orders.c.orderId == order_id).values(
                        {
                            'jobId': json2['jobid'],
                            'orderStatus': 'submitted'
                        }).execute()
                    print 'Order status: submitted'
                    return jsonify({
                        'success':
                        True,
                        'message':
                        'The order was successfully submitted'
                    })
                else:
                    # Set order status to error in the DB
                    Orders.update().where(Orders.c.orderId == order_id).values(
                        {
                            'orderStatus': 'error'
                        }).execute()
                    print 'Order status: error submitting'
                    return jsonify(resp.json()), resp.status_code

    except exc.SQLAlchemyError as e:
        return jsonify({'success': False, 'message': e.message})