def itemById(itemId):
    if request.method == 'GET':
        try:
            item = itemService.getItemById(itemId)
            return makeJsonResponse(item)
        except DatabaseIndexError as e:
            return makeJsonResponse({"success": False, "message": str(e)}), 404
def updateRemoveAction(itemId, status):
    if request.method == 'PUT':
        try:
            itemService.updateRemoveAction(itemId, status)
            socketio.emit('delete_item', itemId)
            return makeJsonResponse({"success": True})
        except DatabaseIndexError as e:
            return makeJsonResponse({"success": False, "message": str(e)}), 404
def sellItem(itemId):
    if request.method == 'PUT':
        try:
            jsonreq = request.get_json(force=True)
            itemService.sellItem(itemId, jsonreq['site'])
            socketio.emit('delete_item', itemId)
            return makeJsonResponse({"success": True})
        except DatabaseIndexError as e:
            return makeJsonResponse({"success": False, "message": str(e)}), 404
def items():
    if request.method == 'POST':
        try:
            jsonreq = request.get_json(force=True)
            postedItem = itemAssembler(jsonreq)
            itemId = itemService.createItem(postedItem)
            mini = {
                'itemId': itemId,
                'title': postedItem.title,
                'upc': postedItem.upc
            }
            socketio.emit('new_item', makeJson(mini), broadcast=True)
            return makeJsonResponse({"success": True})
        except KeyError as e:
            return makeJsonResponse({
                "success":
                False,
                "message":
                f"Could not find {str(e)} key in received data"
            }), 400
        except IntegrityError as e:
            failedKey = str(e).split()[-1].split('.')
            return makeJsonResponse({
                "success":
                False,
                "message":
                f"'{failedKey[-1]}' must not be null"
            }), 400

    elif request.method == 'PUT':
        try:
            jsonreq = request.get_json(force=True)
            putItem = itemAssembler(jsonreq)
            itemService.editItem(putItem)
            mini = {
                'itemId': putItem.itemId,
                'title': putItem.title,
                'upc': putItem.upc
            }
            socketio.emit('update_item', makeJson(mini), broadcast=True)
            return makeJsonResponse({"success": True})
        except KeyError as e:
            return makeJsonResponse({
                "success":
                False,
                "message":
                f"Could not find {str(e)} key in received data"
            }), 400
        except IntegrityError as e:
            failedKey = str(e).split()[-1].split('.')
            return makeJsonResponse({
                "success":
                False,
                "message":
                f"'{failedKey[-1]}' must not be null"
            }), 400
        except DatabaseIndexError as e:
            return makeJsonResponse({"success": False, "message": str(e)}), 404
def savedData(dataType):
    if dataType not in savedDataTypes:
        return makeJsonResponse({
            "success": False,
            "message": f"'{dataType}' is not a valid type"
        }), 404
    if request.method == 'GET':
        dataList = savedDataService.getListOfData(dataType)
        return makeJsonResponse(dataList)
    elif request.method == 'POST':
        jsonreq = request.get_json(force=True)
        data = jsonreq['data']
        savedDataService.createData(dataType, data)
        dataPack = {'type': dataType, 'data': data}
        socketio.emit('new_data', makeJson(dataPack), broadcast=True)
        return makeJsonResponse({"success": True})
    elif request.method == 'DELETE':
        jsonreq = request.get_json(force=True)
        data = jsonreq['data']
        savedDataService.removeData(dataType, data)
        dataPack = {'type': dataType, 'data': data}
        socketio.emit('delete_data', makeJson(dataPack), broadcast=True)
        return makeJsonResponse({"success": True})
def test_make_json_response_from_object():
    with app.app_context():
        book = [
            Book(
                Item(5, "Cracking the Coding Interview", "9870984782857", 2015,
                     "A very good book for practicing algorithms", 2,
                     "2016-12-6", "Online", False, 2000, 1200,
                     [Site(1, 1234567890)], "A1", None, None, None),
                "Gayle Laakmann McDowell", 6, 15, 0)
        ]
        response = makeJsonResponse(book)
        assert response.status_code == 200
        assert response.is_json is True

        jsonResult = response.get_json()
        assert len(jsonResult) == 1
        jsonResult = jsonResult[0]
        assert jsonResult['item']['itemId'] == 5
        assert jsonResult['item']['title'] == "Cracking the Coding Interview"
        assert jsonResult['item']['upc'] == "9870984782857"
        assert jsonResult['item']['year'] == 2015
        assert jsonResult['item'][
            'description'] == "A very good book for practicing algorithms"
        assert jsonResult['item']['condition'] == 2
        assert jsonResult['item']['datePurchased'] == "2016-12-6"
        assert jsonResult['item']['locationPurchased'] == "Online"
        assert jsonResult['item']['consignment'] is False
        assert jsonResult['item']['amountPaid'] == 2000
        assert jsonResult['item']['sellPrice'] == 1200
        assert jsonResult['item']['siteListed'][0]['site'] == 1
        assert jsonResult['item']['siteListed'][0]['siteId'] == 1234567890
        assert jsonResult['item']['shelfLocation'] == "A1"
        assert jsonResult['item']['removalAction'] is None
        assert jsonResult['item']['dateRemoved'] is None
        assert jsonResult['author'] == "Gayle Laakmann McDowell"
        assert jsonResult['edition'] == 6
        assert jsonResult['printing'] == 15
        assert jsonResult['cover'] == 0
def itemsSellable():
    if request.method == 'GET':
        itemList = itemService.getSellableItems()
        return makeJsonResponse(itemList)
Beispiel #8
0
def issues():
    if request.method == 'POST':
        jsonreq = request.get_json(force=True)
        issueService.createNewIssue(jsonreq)
        return makeJsonResponse({"success": True})
def sitesById(itemId):
    if request.method == 'GET':
        sites = siteService.getSiteValuesById(itemId)
        return makeJsonResponse(sites)
Beispiel #10
0
def internalError(e):
	return makeJsonResponse({"success": False, "message": "something went wrong"}), 500
Beispiel #11
0
def notAllowed(e):
	return makeJsonResponse({"success": False, "message": "method not implemented"}), 405
Beispiel #12
0
def notFound(e):
	return makeJsonResponse({"success": False, "message": "not found"}), 404