예제 #1
0
def location(id):

    #print("id",id)
    #print("args",request.args)

    if request.method == 'GET':
        query = request.args
        data = mongo.db.locations.find_one({"_id": ObjectId(id)})
        #print("data",data)
        #print("len",len(data))
        return jsonify(data), 200

    if request.method == 'DELETE':
        db_response = mongo.db.locations.delete_one({"_id": ObjectId(id)})
        if db_response.deleted_count == 1:
            response = {'message': 'record deleted'}
        else:
            response = {'message': 'no record found'}
        return jsonify(response), 200

    if request.method == 'PUT':
        #data = request.get_json()
        data = validate_location(request.get_json())
        if data['ok']:
            data = data['data']

            check = checkSimpleForeign("address", data['address'])
            if check != True:
                return check

            check = checkSimpleForeign("accounts", data['accountId'])
            if check != True:
                return check

            data["updatedAT"] = datetime.datetime.utcnow()
            data["updatedBy"] = request.tokenUserId
            db_response = mongo.db.locations.update_one({"_id": ObjectId(id)},
                                                        {'$set': data})
            #print("response",db_response.matched_count)
            if db_response.matched_count > 0:
                return jsonify({'message': 'record updated'}), 200
            else:
                return jsonify({'message': 'error on record updated'}), 400
        else:
            return jsonify({
                'message':
                'Bad request parameters: {}'.format(data['message'])
            }), 400
예제 #2
0
def productTypes():
    if request.method == 'GET':
        query = request.args
        data = json.loads(
            dumps(
                mongo.db.productTypes.aggregate([{
                    '$addFields': {
                        "_id": {
                            '$toString': '$_id'
                        }
                    }
                }])))  #print("data",data)
        #print("len",len(data))
        return jsonify(data), 200
    if request.method == 'POST':
        data = validate_productType(request.get_json())
        if data['ok']:
            data = data['data']

            check = checkSimpleForeign("accounts", data['accountId'])
            if check != True:
                return check

            data["createdAT"] = datetime.datetime.utcnow()
            data["createdBy"] = request.tokenUserId
            mongo.db.productTypes.insert_one(data)
            return jsonify({'message':
                            'productType created successfully!'}), 200
        else:
            return jsonify({
                'message':
                'Bad request parameters: {}'.format(data['message'])
            }), 400