import os import datetime import psycopg2 import orgHandler import dbHandler blogDir: str = "./blogs" debug: bool = True def debugPrint(msg: str) -> None: if debug: print("(BLOG HANDLER) PRINT: " + msg) # Returns new Blog ID def createBlogEntry(dbConnection: psycopg2.extensions.connection, userID: int, categoryID: int) -> int: datePosted = datetime.datetime.now() datePostedStr = datePosted.strftime("%G-%m-%d %X") debugPrint("Now creating new user with following attributes : userID = " + str(userID) + ", categoryID = " + str(categoryID) + ", datePosted = " + datePostedStr) newRow = dbHandler.insertRow(dbConnection, 'blogs', ['authorID', 'categoryID', 'datePosted' ], [userID, categoryID, datePostedStr]) return newRow[0] def uploadBlog(dbConnection: psycopg2.extensions.connection, userID: int, orgRawIn: str): try: orgParsedOut = orgHandler.orgToHTML(orgRawIn) if orgParsedOut: blogID = createBlogEntry(dbConnection, userID, 1) newBlogDir = blogDir + "/" + str(blogID) debugPrint("Attempting to create new blog directory " + newBlogDir + "...") os.mkdir(newBlogDir) debugPrint("Attempting to write new blog file " + newBlogDir + ".org...") orgFileData = open(newBlogDir + "/" + str(blogID) + ".org", 'w') orgFileData.write(orgRawIn) orgFileData.close() debugPrint("Attempting to write new blog file " + blogDir + "/" + str(blogID) + "/" + str(blogID) + ".html...") HTMLFileData = open(newBlogDir + "/" + str(blogID) + ".html", 'w') HTMLFileData.write(orgParsedOut) HTMLFileData.close() return blogID else: return False except Exception as error: debugPrint("Create blog failed! " + repr(error)) return False