Beispiel #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
    '''
Beispiel #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
Beispiel #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)
Beispiel #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)
Beispiel #5
0
def htm_Doc():
    if (request.method == 'GET'):
        this_folderpath = os.path.dirname(os.path.abspath(__file__))
        folderpath = this_folderpath
        doc_json_path = os.path.join(folderpath, "base.json")
        try:
            doc_json = json.load(open(doc_json_path),
                                 object_pairs_hook=OrderedDict)
            doc = json.dumps(doc_json, indent=4)
        except Exception as e:
            return "Error Occured" + str(e)
        template = "/document/document.html"

        return render_template(template, doc=doc, authenticated=False)
    else:
        doc_file = request.files['doc']
        docRaw = json.loads(doc_file.read().decode('utf-8'))
        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']
            path = os.path.join(repository, discipline, docCategory,
                                docSubCategory, docClass, "doc.html")
            template = "/".join(path.split(os.sep))
        except:
            pass

        doc = json.dumps(docRaw, indent=4)
        return render_template(template, doc=doc, authenticated=False)
Beispiel #6
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
Beispiel #7
0
def pdf_report():
    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

    doc = docParsed.data
    #check if the raw document conforms to the specific document schema for the class
    projectID = doc['meta']['projectID']
    repository = get_repository(projectID)
    discipline = doc['meta']['discipline']
    docCategory = doc['meta']['docCategory']
    docSubCategory = doc['meta']['docSubCategory']
    docClass = doc['meta']['docClass']
    title = doc["meta"]["docInstance_title"]
    rev = doc['meta']['rev']
    date = doc['meta']['date']
    doc_no = doc['meta']['doc_no']

    path = os.path.join(repository, discipline, docCategory, docSubCategory, docClass, "doc.html")
    template = "/".join(path.split(os.sep))
    doc = json.dumps(doc, indent=4)
    print(doc)
    main_content = render_template(template, doc=doc)

    options = {
        'page-size' : 'A4',
        'margin-top':'25mm',
        'margin-bottom':'19mm',
        'margin-left':'19mm',
        'margin-right':'19mm',
        'encoding':'UTF-8',
        'print-media-type' : None,
#            'header-left' : 'My Static Header',
        'header-line' : None,
        'header-font-size' : '8',
        'header-font-name' : 'Calibri',
        'header-spacing' : '5',
        'footer-left' : "www.codecalculation.com",
        'footer-line' : None,
        'footer-font-size' : '8',
        'footer-font-name' : 'Calibri',
        'footer-spacing' : '5',
        'disable-smart-shrinking' : None,
        'no-stop-slow-scripts' : None,
        'javascript-delay': 300,
        'enable-javascript': None,
        'debug-javascript': None,
        '--encoding': "utf-8"
    }

    this_folderpath = os.path.dirname(os.path.abspath(__file__))
#    wkhtmltopdf_path = r'/home/appadmin/wkhtmltox/bin/wkhtmltopdf'
    wkhtmltopdf_path = r'/home/sandeep/www/wkhtmltox/bin/wkhtmltopdf'
    config = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_path)
    this_folderpath = os.path.dirname(os.path.abspath(__file__))
    css_path = os.path.join(this_folderpath, 'print.css')

    context_header = {}
    context_header['title'] = title
    context_header['rev'] = rev
    context_header['doc_no'] = doc_no

    context_header['date'] = change_date_format(date)
    add_pdf_header(options, context_header=context_header)

    try:
        pdf = pdfkit.from_string(main_content, False, configuration=config, options=options, css=css_path)
    except Exception as e:
        print(str(e))
        return (str(e))
    finally:
        os.remove(options['header-html'])

    response = build_response(pdf)
    return response