def init_md_code_examples(): """Converts markdown code-example items to DB.""" kb_dir = os.path.join(current_app.root_path, 'markdown/code_examples/web/') code_langs = [ 'asp', 'java', 'php', 'flask', 'django', 'go', 'ruby', 'nodejs-express' ] try: for lang in code_langs: for filename in os.listdir(kb_dir + lang): if filename.endswith(".md"): name_raw = filename.split("-") title = name_raw[3].replace("_", " ") file = os.path.join(kb_dir + lang, filename) data = open(file, 'r') file_content = data.read() data.close() content_escaped = file_content.translate( str.maketrans({ "'": r"''", "-": r"", "#": r"" })) try: item = CodeItem(content_escaped, title, lang) item.checklist_category_id = 1 db.session.add(item) db.session.commit() except IntegrityError as e: print(e) pass print('Initialized the markdown code-examples.') return True except: raise
def create_code_item(data, category_id): log("User requested creating a new code item", "LOW", "PASS") result = CodeItem(data.get('content'), data.get('title'), data.get('code_lang')) result.checklist_category_id = category_id try: db.session.add(result) db.session.commit() except: db.session.rollback() raise return {'message': 'Code example item successfully created'}
def create_code_item(data): log("User requested creating a new code item", "LOW", "PASS") val_alpha_num_special(data.get('title')) val_alpha_num(data.get('code_lang')) title = data.get('title') content = data.get('content') code_lang = data.get('code_lang') result = CodeItem(content, title, code_lang) try: db.session.add(result) db.session.commit() except: db.session.rollback() raise return {'message': 'Code example item successfully created'}
def init_md_code_examples(): """Converts markdown code-example items to DB.""" kb_dir = os.path.join(current_app.root_path, 'markdown/code_examples/') code_langs = [ 'asp', 'java', 'php', 'flask', 'django', 'go', 'ruby', 'nodejs-express' ] try: for lang in code_langs: for filename in os.listdir(kb_dir + lang): if filename.endswith(".md"): name_raw = filename.split("-") title = name_raw[3].replace("_", " ") file = os.path.join(kb_dir + lang, filename) data = open(file, 'r') file_content = data.read() data.close() content_escaped = file_content.translate( str.maketrans({ "'": r"''", "-": r"", "#": r"" })) #query = "INSERT OR REPLACE INTO code_items (content, title, code_lang) VALUES ('"+content_escaped+"', '"+title+"', '"+lang+"', 1) \n" #with open(os.path.join(app.root_path, 'db.sqlite_schema'), 'a') as myfile: # myfile.write(query) try: item = CodeItem(content_escaped, title, lang) db.session.add(item) db.session.commit() except IntegrityError as e: print(e) pass print('Initialized the markdown code-examples.') return True except: raise
def load_test_data(): print("Loading test data") try: for i in range(1, 5): db.session.add( KBItem("test title kb item {}".format(i), "test content kb item {}".format(i))) db.session.commit() for lang in ["php", "asp"]: for i in [1, 2]: db.session.add( CodeItem(lang, "test php code item 1", "test php content code item {}".format(i))) for i in [1, 2]: db.session.add(Question(i, "test-question-sprint")) db.session.add(ChecklistType("empty-checklist-for-testing", "TBD")) db.session.add(ChecklistType("filled-checklist-for-testing", "TBD")) checklist_kb = ChecklistKB('1.0', 'test content checklist item 1', False, 123) checklist_kb.question_id = 2 checklist_kb.kb_id = 2 checklist_kb.checklist_type = 1 db.session.add(checklist_kb) checklist_kb = ChecklistKB('1.1', 'test content checklist item 1', False, 123) checklist_kb.question_id = 2 checklist_kb.kb_id = 2 checklist_kb.checklist_type = 1 db.session.add(checklist_kb) checklist_kb = ChecklistKB('1.2', 'test content checklist item 2', True, 124) checklist_kb.question_id = 0 checklist_kb.kb_id = 1 checklist_kb.checklist_type = 1 db.session.add(checklist_kb) checklist_kb = ChecklistKB('1.3', 'test content checklist item 3', True, 125) checklist_kb.question_id = 0 checklist_kb.kb_id = 1 checklist_kb.checklist_type = 1 db.session.add(checklist_kb) checklist_kb = ChecklistKB('1.4', 'test content checklist item 4', False, 126) checklist_kb.question_id = 2 checklist_kb.kb_id = 2 checklist_kb.checklist_type = 1 db.session.add(checklist_kb) db.session.commit() except: db.session.rollback() raise