def api_post_project(): errors = OrderedDict() req = request.get_json() if ('resource' not in req): errors['message'] = "'project' missing in REST API request" return json_response(errors), 400 else: docRaw = req['resource'] docSchema = sProject() docParsed = docSchema.load(docRaw) if (len(docParsed.errors) > 0): errors['message'] = "Project is not as per Schema" errors['schema'] = docParsed.errors return json_response(errors), 400 else: project = docParsed.data projects = mongodb['projects'] try: projects.insert_one(project) except pymongo.errors.DuplicateKeyError: errors[ 'message'] = 'Insert Faileds as Project ID already exists' return json_response(errors), 400 except Exception as e: errors['message'] = str(e) return json_response(errors), 400 response = {} response['message'] = "Project added successfully" return json_response(response), 201
def api_get_user(user_id): errors = OrderedDict() users = mongodb['users'] docMongo = users.find_one({"_id": user_id}) if (docMongo== None): errors['message'] = "user with user ID as requested could not be found" return json_response(errors), 404 else: return json_response(docMongo)
def api_get_project(project_id): errors = OrderedDict() projects = mongodb['projects'] docMongo = projects.find_one({"_id": project_id}) if (docMongo == None): errors['operation'] = [ 'Project with Project ID as requested could not be found' ] return json_response(errors), 404 else: return json_response(docMongo)
def api_get_document(doc_id): errors = OrderedDict() documents = mongodb['documents'] docMongo = documents.find_one({"_id": doc_id}) if (docMongo == None): errors['operation'] = [ 'Document with Document ID as requested could not be found' ] return json_response(errors), 404 else: return json_response(docMongo)
def api_post_user(): errors = OrderedDict() req = request.get_json() if ('resource' not in req): errors['message'] = "'user' missing in REST API request" return json_response(errors), 400 else: docRaw = req['resource'] docSchema = sUserReg() docParsed = docSchema.load(docRaw) if (len(docParsed.errors) > 0): errors['message'] = "there are errors in your input" errors['schema'] = docParsed.errors return json_response(errors), 400 else: user = docParsed.data user['password_hash'] = generate_password_hash(user['password']) user['confirmed'] = False users = mongodb['users'] try: users.insert_one(user) except pymongo.errors.DuplicateKeyError: errors['message'] = 'Insert Faileds as user ID already exists' return json_response(errors), 400 except Exception as e: errors['message'] = str(e) return json_response(errors), 400 token = generate_confirmation_token(user["email"]) confirm_link = "https://www.clappets.com/htm/user/confirm/"+user['_id']+"/"+token+"/" html_template = """\ <html> <head></head> <body> <p>Hi!<br> We have received your user registration request. <br> Please click on the link to confirm your email id. <a href="{}">{}</a> </p> <p> This mail is auto generated. Please do not reply to this mail. </p> </body> </html> """ mailbody = html_template.format(confirm_link, confirm_link) # sendMail([user["email"]],'*****@*****.**','User Registration',mailbody) response = {} response["message"] = "User Registered Successfully." return json_response(response), 201
def api_delete_project(project_id): errors = OrderedDict() projects = mongodb["projects"] try: projects.delete_one({"_id": project_id}) except Exception as e: errors['operation'] = str(e) return json_response(errors) response = {} response['message'] = "Deletion Successful" return json_response(response)
def api_delete_document(doc_id): errors = OrderedDict() documents = mongodb["documents"] try: documents.delete_one({"_id": doc_id}) except Exception as e: errors['operation'] = str(e) return json_response(errors) response = {} response["message"] = "Deletion Successful" response["redirect_url"] = "/htm/document/" return json_response(response)
def api_delete_user(user_id): errors = OrderedDict() users = mongodb["users"] try: users.delete_one({"_id" : user_id}) except Exception as e: errors['message'] = str(e) return json_response(errors) response = {} response["message"] = "Deletion Successful" response["redirect_url"] = "/htm/user/" return json_response(response)
def api_forgotpasswd(): errors = OrderedDict() req = request.get_json() errors = {} if ('resource' not in req): errors['message'] = "'resource' missing in REST API request" return json_response(errors), 400 else: docRaw = req['resource'] docSchema = sUserForgot() docParsed = docSchema.load(docRaw) if (len(docParsed.errors) > 0): errors['message'] = "There are errors in input" errors['schema'] = docParsed.errors return json_response(errors), 400 else: user = docParsed.data user_id = user["_id"] users = mongodb['users'] user = users.find_one({"_id": user_id}) email = user["email"] new_password = pw_gen() user['password_hash'] = generate_password_hash(new_password) try: users.update({"_id" : user_id}, user) except Exception as e: errors['message'] = str(e) return json_response(errors), 400 html_template = """\ <html> <head></head> <body> <p>Hi!<br> We have received your new passoword request. <br> Your new passoword is {} <br> Please remember to reset your password after login. </p> <p> This mail is auto generated. Please do not reply to this mail. </p> </body> </html> """ mailbody = html_template.format(new_password) sendMail([user["email"]],'*****@*****.**','Password Reset',mailbody) response = {} response["message"] = "You will receive your new password on email. If you do not find the mail in your mail box\ please ensure to check your spam folder" return json_response(response), 201
def getAuthTokenAlt(): req = request.get_json() username = req["username"] password = req["password"] auth_response = {} if username in users: authenticated = check_password_hash(users.get(username), password) if authenticated: access_token = jwt.dumps({'username': username}) auth_response = {} auth_response["access_token"] = access_token.decode('utf-8') else: auth_response["message"] = "Invalid User Credentials" return json_response(auth_response), 401 else: auth_response["message"] = "Invalid User Credentials" return json_response(auth_response), 401 return json_response(auth_response)
def getAuthToken(): req = request.get_json() username = req["username"] password = req["password"] auth_response = {} users = mongodb['users'] user = users.find_one({"_id": username}) if (user == None): auth_response["message"] = "Invalid User Credentials" return json_response(auth_response), 401 else: authenticated = check_password_hash(user['password_hash'], password) if authenticated: access_token = jwt.dumps({'username': user['_id'] }) auth_response = {} auth_response["access_token"] = access_token.decode('utf-8') return json_response(auth_response) else: auth_response["message"] = "Invalid User Credentials" return json_response(auth_response), 401
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
def api_put_user(user_id): errors = OrderedDict() req = request.get_json() if ('resource' not in req): errors['message'] = "'user' missing in REST API request" return json_response(errors), 400 else: docRaw = req['resource'] docSchema = sUserAccountUpdate() docParsed = docSchema.load(docRaw) if (len(docParsed.errors) > 0): errors['message'] = "user is not as per Schema" errors['schema'] = docParsed.errors return json_response(errors), 400 else: user = docParsed.data user_id = user['_id'] update_doc = { "$set" : {"first_name" : user['first_name'], "last_name" : user['last_name'], "email" : user['email'], } } if (user['change_password']): user['password_hash'] = generate_password_hash(user['new_password']) update_doc['$set']['password_hash'] = user['password_hash'] users = mongodb['users'] try: users.update({"_id" : user_id}, update_doc) except Exception as e: errors['message'] = str(e) return json_response(errors), 400 return json_response({'message' : 'user Updated Sucessfully'}), 200
def api_put_project(project_id): errors = OrderedDict() req = request.get_json() if ('resource' not in req): errors['message'] = "'project' missing in REST API request" return json_response(errors), 400 else: docRaw = req['resource'] docSchema = sProject() docParsed = docSchema.load(docRaw) if (len(docParsed.errors) > 0): errors['message'] = "Project is not as per Schema" errors['schema'] = docParsed.errors return json_response(errors), 400 else: project = docParsed.data projects = mongodb['projects'] try: projects.update({"_id": project_id}, project) except Exception as e: errors['message'] = str(e) return json_response(errors), 400 return json_response({'message': 'Project Updated Sucessfully'}), 200
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)
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)
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
def api_document(project="", discipline="", docCategory="", docSubCategory="", docClass="", docInstance=""): #this_folderpath = os.path.dirname(os.path.abspath(__file__)) context = OrderedDict() context["title"] = "Open Document" documents = mongodb["documents"] pipeline = [ { "$match": { "$and": [{}] } }, # match all documents, the and operator has a list with only one element {} which means match all { "$group": { "_id": "$meta.projectID" } } # group results by projectID ] subfolder_names = list(documents.aggregate(pipeline)) if (project == ""): context["subtitle"] = "Project" subfolder_list = [] for folder in subfolder_names: entry = OrderedDict() entry["name"] = folder["_id"] entry["title"] = get_project_title(folder["_id"]) entry["url"] = "/" + "/".join( ["api", "document", "query", folder['_id']]) + "/" subfolder_list.append(entry) context['subfolder_list'] = subfolder_list return json_response(context) else: allowable_projects = [x['_id'] for x in subfolder_names] if project not in allowable_projects: return "Invalid Project" repository = get_repository(project) pipeline = [ { "$match": { "$and": [{ "meta.projectID": project }] } }, { "$group": { "_id": "$meta.discipline" } } # group results by discipline ] subfolder_names = list(documents.aggregate(pipeline)) if (discipline == ""): context["subtitle"] = "Discipline" subfolder_list = [] for folder in subfolder_names: entry = OrderedDict() entry["name"] = folder["_id"] entry["title"] = get_folder_title(repository, folder["_id"]) entry["url"] = "/" + "/".join( ["api", "document", "query", project, folder['_id']]) + "/" subfolder_list.append(entry) context['subfolder_list'] = subfolder_list return json_response(context) else: allowable_disciplines = [x['_id'] for x in subfolder_names] if discipline not in allowable_disciplines: return "Invalid Discipline Entered" pipeline = [{ "$match": { "$and": [{ "meta.projectID": project }, { "meta.discipline": discipline }] } }, { "$group": { "_id": "$meta.docCategory" } }] subfolder_names = list(documents.aggregate(pipeline)) if (docCategory == ""): subfolder_list = [] for folder in subfolder_names: entry = OrderedDict() entry["name"] = folder["_id"] entry["title"] = get_folder_title(repository, discipline, folder["_id"]) entry["url"] = "/" + "/".join([ "api", "document", "query", project, discipline, folder['_id'] ]) + "/" subfolder_list.append(entry) context['subfolder_list'] = subfolder_list return json_response(context) else: allowable_docCategories = [x['_id'] for x in subfolder_names] if docCategory not in allowable_docCategories: return "Invalid docCategory Entered" pipeline = [{ "$match": { "$and": [{ "meta.projectID": project }, { "meta.discipline": discipline }, { "meta.docCategory": docCategory }] } }, { "$group": { "_id": "$meta.docSubCategory" } }] subfolder_names = list(documents.aggregate(pipeline)) if (docSubCategory == ""): subfolder_list = [] for folder in subfolder_names: entry = OrderedDict() entry["name"] = folder["_id"] entry["title"] = get_folder_title(repository, discipline, docCategory, folder["_id"]) entry["url"] = "/" + "/".join([ "api", "document", "query", project, discipline, docCategory, folder['_id'] ]) + "/" subfolder_list.append(entry) context['subfolder_list'] = subfolder_list return json_response(context) else: allowable_docSubCategories = [x['_id'] for x in subfolder_names] if docSubCategory not in allowable_docSubCategories: return "Invalid docSubCategory Entered" pipeline = [{ "$match": { "$and": [{ "meta.projectID": project }, { "meta.discipline": discipline }, { "meta.docCategory": docCategory }, { "meta.docSubCategory": docSubCategory }] } }, { "$group": { "_id": "$meta.docClass" } }] subfolder_names = list(documents.aggregate(pipeline)) if (docClass == ""): subfolder_list = [] for folder in subfolder_names: entry = OrderedDict() entry["name"] = folder["_id"] entry["title"] = get_folder_title(repository, discipline, docCategory, docSubCategory, folder["_id"]) entry["url"] = "/" + "/".join([ "api", "document", "query", project, discipline, docCategory, docSubCategory, folder['_id'] ]) + "/" subfolder_list.append(entry) context['subfolder_list'] = subfolder_list return json_response(context) else: allowable_docClasses = [x['_id'] for x in subfolder_names] if docClass not in allowable_docClasses: return "Invalid docClass Entered" query = { "$and": [ { "meta.projectID": project }, { "meta.discipline": discipline }, { "meta.docCategory": docCategory }, { "meta.docSubCategory": docSubCategory }, { "meta.docClass": docClass }, ] } docInstance_list = list( documents.find(query, { "meta.docInstance": 1, "meta.docInstance_title": 1 })) if (docInstance == ""): subfolder_list = [] for doc in docInstance_list: entry = OrderedDict() entry["name"] = doc["meta"]["docInstance"] entry["title"] = doc["meta"]["docInstance_title"] entry["url"] = "/" + "/".join( ["htm", "document", "db", doc['_id']]) + "/" subfolder_list.append(entry) context['subfolder_list'] = subfolder_list return json_response(context) else: allowable_docInstances = [ x['meta']['docInstance'] for x in docInstance_list ] if docInstance not in allowable_docInstances: return "Invalid docInstance Entered" query = { "$and": [ { "meta.projectID": project }, { "meta.discipline": discipline }, { "meta.docCategory": docCategory }, { "meta.docSubCategory": docSubCategory }, { "meta.docClass": docClass }, { "meta.docInstance": docInstance }, ] } doc = documents.find_one(query) return json_response(doc)
def api_get_users(): users = mongodb['users'] user_list = list(users.find()) return json_response(user_list), 200
def api_template(repository="", discipline="", docCategory="", docSubCategory="", docClass=""): #this_folderpath = os.path.dirname(os.path.abspath(__file__)) context = OrderedDict() context["title"] = "New Document" #folderpath = os.path.join(this_folderpath) folderpath = os.path.join("") if (repository == ""): context["subtitle"] = "Repository" subfolder_list = get_subfolder_list(folderpath) context['subfolder_list'] = subfolder_list return json_response(context) else: allowed_repositories = get_subfolder_names(folderpath) if repository not in allowed_repositories: return "Invalid Repository" folderpath = os.path.join(repository) if (discipline == ""): context["subtitle"] = "Discipline" subfolder_list = get_subfolder_list(folderpath) context['subfolder_list'] = subfolder_list return json_response(context) else: allowed_disciplines = get_subfolder_names(folderpath) if discipline not in allowed_disciplines: return "Invalid Discipline" folderpath = os.path.join(repository, discipline) if (docCategory == ""): context["subtitle"] = "Doc Category" subfolder_list = get_subfolder_list(folderpath) context['subfolder_list'] = subfolder_list return json_response(context) else: allowed_docCategories = get_subfolder_names(folderpath) if docCategory not in allowed_docCategories: return "Invalid docCategory" folderpath = os.path.join(repository, discipline, docCategory) if (docSubCategory == ""): context["subtitle"] = "Doc SubCategory" subfolder_list = get_subfolder_list(folderpath) context['subfolder_list'] = subfolder_list return json_response(context) else: allowed_docSubCategories = get_subfolder_names(folderpath) if docSubCategory not in allowed_docSubCategories: return "Invalid docSubCategory" folderpath = os.path.join(repository, discipline, docCategory, docSubCategory) if (docClass == ""): context["subtitle"] = "Doc Class" subfolder_list = get_subfolder_list(folderpath) context['subfolder_list'] = subfolder_list return json_response(context) else: allowed_docClasses = get_subfolder_names(folderpath) if docClass not in allowed_docClasses: return "Invalid docClass" this_folderpath = os.path.dirname(os.path.abspath(__file__)) folderpath = os.path.join(repository, discipline, docCategory, docSubCategory, docClass) doc_json_path = os.path.join(this_folderpath, folderpath, "doc.json") try: doc_json = json.load(open(doc_json_path), object_pairs_hook=OrderedDict) except Exception as e: return "Error Occured" + str(e) return json_response(doc_json)
def api_get_documents(): documents = mongodb['documents'] document_list = list(documents.find()) return json_response(document_list), 200
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
def api_macro(): response = {} response["message"] = "You successfully ran the macros" return json_response(response)
def api_get_projects(): projects = mongodb['projects'] project_list = list(projects.find()) return json_response(project_list), 200