Exemple #1
0
def run_tests():

    THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
    input_filename = os.path.join(THIS_FOLDER, 'doc.json')

    # load the input json to a python dictionary named input_json and print
    doc0 = json.load(open(input_filename), object_pairs_hook=OrderedDict)
    print("Check for valid Json File")
    print(json.dumps(doc0, indent=4))

    # check if json confirms to basic Schema for project document
    #validate the input using the schema
    print("Testing the data with the sDocPrj schema")
    common_schema = sDocPrj()
    parsed_data = common_schema.load(doc0)
    print("Parsed Data from sDocPrj: ")
    print("=========================")
    print(json.dumps(parsed_data.data, indent=4))
    print("Errors from sDocPrj:")
    print("========")
    print(json.dumps(parsed_data.errors, indent=4))
    doc1 = parsed_data.data

    schemaModuleFile = os.path.join(THIS_FOLDER, "schema.py")
    docSchema = load_schema(schemaModuleFile)
    parsed_data = docSchema.load(doc1)
    print("Parsed Data from docSchema: ")
    print("===========================")
    print(json.dumps(parsed_data.data, indent=4))
    print("Errors from docSchema:")
    print("========")
    print(json.dumps(parsed_data.errors, indent=4))
    doc2 = parsed_data.data
    '''
Exemple #2
0
def api_put_document(doc_id):
    errors = OrderedDict()
    req = request.get_json()

    #perform a series of checks an return error responses
    #check if the request body contains 'doc'
    if ('resource' not in req):
        errors['message'] = "'resource' missing in request"
        return json_response(errors), 400

    #check if the raw document conforms to the generic document schema for the project (basically meta check)
    docRaw = req['resource']
    basicSchema = sDocPrj()
    docParsed = basicSchema.load(docRaw)
    if (len(docParsed.errors) > 0):
        errors["message"] = "The Document Meta Information has errors"
        errors["schema"] = docParsed.errors
        return json_response(errors), 400

    #check if the raw document conforms to the specific document schema for the class
    try:
        projectID = docRaw['meta']['projectID']
        repository = get_repository(projectID)
        discipline = docRaw['meta']['discipline']
        docCategory = docRaw['meta']['docCategory']
        docSubCategory = docRaw['meta']['docSubCategory']
        docClass = docRaw['meta']['docClass']

        # get the absolute folder path in folderpath
        this_folderpath = os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(repository, discipline, docCategory,
                            docSubCategory, docClass)
        docClassFolder = os.path.join(this_folderpath, path)
        schemaModuleFile = os.path.join(docClassFolder, "schema.py")
        docSchema = load_schema(schemaModuleFile)
    except FileNotFoundError as e:
        errors["message"] = "Schema File could not be found"
        errors["operation"] = str(e)
        return json_response(errors), 400

    docParsed = docSchema.load(docRaw)
    if (len(docParsed.errors) > 0):
        errors['message'] = "Document Contains Errors"
        errors["schema"] = docParsed.errors
        return json_response(errors), 400

    doc = docParsed.data
    try:
        documents = mongodb['documents']
        documents.update({"_id": doc["_id"]}, doc)
    except Exception as e:
        errors['message'] = ['Database Connection Error.']
        errors['operation'] = str(e)
        return json_response(errors), 400

    return json_response({
        'message': 'Document Updated Sucessfully',
        '_id': doc['_id']
    }), 201
Exemple #3
0
def run_tests():

    THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
    input_filename = os.path.join(THIS_FOLDER, 'doc.json')

    # load the input json to a python dictionary named input_json and print
    doc0 = json.load(open(input_filename), object_pairs_hook=OrderedDict)
    print ("Check for valid Json File")
    print(json.dumps(doc0, indent=4))

    # check if json confirms to basic Schema for project document
    #validate the input using the schema
    print("Testing the data with the sDocPrj schema")
    common_schema = sDocPrj()
    parsed_data = common_schema.load(doc0)
    print("Parsed Data from sDocPrj: ")
    print("=========================")
    print(json.dumps(parsed_data.data, indent=4))
    print("Errors from sDocPrj:")
    print("========")
    print(json.dumps(parsed_data.errors, indent=4))
    doc1 = parsed_data.data


    schemaModuleFile = os.path.join(THIS_FOLDER, "schema.py")
    docSchema = load_schema(schemaModuleFile)
    parsed_data = docSchema.load(doc1)
    print("Parsed Data from docSchema: ")
    print("===========================")
    print(json.dumps(parsed_data.data, indent=4))
    print("Errors from docSchema:")
    print("========")
    print(json.dumps(parsed_data.errors, indent=4))
    doc2 = parsed_data.data


    macroModuleFile = os.path.join(THIS_FOLDER, "macro.py")
    calculate = load_function(macroModuleFile, 'calculate')
    doc3 = deepcopy(doc2)
    calculate(doc3)
    print("Calculation Done ")
    print(json.dumps(doc3, indent=4))



    data={
        "doc" : doc0
    }

    data_json = json.dumps(data)
    headers = {'Content-type': 'application/json'}
    url = "http://127.0.0.1:5000/api/document/calculate/"
    response = requests.post(url, data=data_json, headers=headers)
    print("Checking REST API")
    print("REST API Response Code : " + str(response.status_code))
    print(response.text)
Exemple #4
0
def api_calculate():
    errors = {}
    req = request.get_json()
    if ('doc' not in req):
        errors['message'] = "'doc' missing in request"
        return json_response(errors), 400

    #check if the raw document conforms to the generic document schema for the project (basically meta check)
    docRaw = req['doc']
    basicSchema = sDocPrj()
    docParsed = basicSchema.load(docRaw)
    if (len(docParsed.errors) > 0):
        errors["message"] = "The Document Meta Information has errors"
        errors["schema"] = docParsed.errors
        return json_response(errors), 400

    #check if the raw document conforms to the specific document schema for the class
    projectID = docRaw['meta']['projectID']
    repository = get_repository(projectID)
    discipline = docRaw['meta']['discipline']
    docCategory = docRaw['meta']['docCategory']
    docSubCategory = docRaw['meta']['docSubCategory']
    docClass = docRaw['meta']['docClass']

    # get the absolute folder path in folderpath
    this_folderpath = os.path.dirname(os.path.abspath(__file__))
    path = os.path.join(repository, discipline, docCategory, docSubCategory,
                        docClass)
    docClassFolder = os.path.join(this_folderpath, path)
    schemaModuleFile = os.path.join(docClassFolder, "schema.py")
    macroModuleFile = os.path.join(docClassFolder, "macro.py")

    try:
        docSchema = load_schema(schemaModuleFile)
    except FileNotFoundError as e:
        errors["message"] = "Schema File could not be found"
        errors["operation"] = str(e)
        return json_response(errors), 400

    try:
        calculate = load_function(macroModuleFile, 'calculate')
    except FileNotFoundError as e:
        errors["message"] = "Calculation Function could not be loaded"
        errors["operation"] = str(e)
        return json_response(errors), 400

    docParsed = docSchema.load(docRaw)
    if (len(docParsed.errors) > 0):
        errors['message'] = "Document Contains Errors"
        errors["schema"] = docParsed.errors
        return json_response(errors), 400

    doc = docParsed.data
    calculate(doc)
    return json_response(doc)
Exemple #5
0
def api_post_document():
    errors = OrderedDict()
    req = request.get_json()

    #perform a series of checks an return error responses
    #check if the request body contains 'doc'
    if ('resource' not in req):
        errors['message'] = "'resource' missing in request"
        return json_response(errors), 400

    #check if the raw document conforms to the generic document schema for the project (basically meta check)
    docRaw = req['resource']
    basicSchema = sDocPrj()
    docParsed = basicSchema.load(docRaw)
    if (len(docParsed.errors) > 0):
        errors["message"] = "The Document Meta Information has errors"
        errors["schema"] = docParsed.errors
        return json_response(errors), 400

    #check if the raw document conforms to the specific document schema for the class
    try:
        projectID = docRaw['meta']['projectID']
        repository = get_repository(projectID)
        discipline = docRaw['meta']['discipline']
        docCategory = docRaw['meta']['docCategory']
        docSubCategory = docRaw['meta']['docSubCategory']
        docClass = docRaw['meta']['docClass']

        # get the absolute folder path in folderpath
        this_folderpath = os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(repository, discipline, docCategory,
                            docSubCategory, docClass)
        docClassFolder = os.path.join(this_folderpath, path)
        schemaModuleFile = os.path.join(docClassFolder, "schema.py")
        docSchema = load_schema(schemaModuleFile)
    except FileNotFoundError as e:
        errors["message"] = "Schema File could not be found"
        errors["operation"] = str(e)
        return json_response(errors), 400

    docParsed = docSchema.load(docRaw)
    if (len(docParsed.errors) > 0):
        errors['message'] = "Document Contains Errors"
        errors["schema"] = docParsed.errors
        return json_response(errors), 400

    doc = docParsed.data
    docInstance = doc["meta"]["docInstance"]
    doc["_id"] = "-".join([
        projectID, discipline, docCategory, docSubCategory, docClass,
        docInstance
    ])
    try:
        documents = mongodb['documents']
        documents.insert_one(doc)
    except pymongo.errors.DuplicateKeyError as e:
        errors['message'] = [
            'Insert Failed as Document ID already exists. Change docInstance to make it unique'
        ]
        errors['operation'] = str(e)
        return json_response(errors), 400
    except Exception as e:
        errors['message'] = ['Database Connection Error.']
        errors['operation'] = str(e)
        return json_response(errors), 400

    response = {}
    response["message"] = "Document Added Successfully"
    response["_id"] = doc["_id"]
    response["redirect_url"] = "/htm/document/db/" + doc["_id"] + "/"
    return json_response(response), 201