128 lines
5.4 KiB
Python
128 lines
5.4 KiB
Python
import os
|
|
import datetime
|
|
|
|
import psycopg2
|
|
from orgparse import loads
|
|
|
|
import userHandler
|
|
import orgHandler
|
|
import dbHandler
|
|
|
|
blogDir: str = "./blogs"
|
|
debug: bool = True
|
|
|
|
def debugPrint(msg: str) -> None:
|
|
if debug:
|
|
print("(BLOG HANDLER) PRINT: " + msg)
|
|
|
|
def checkIDExistence(dbConnection: psycopg2.extensions.connection, blogID: int) -> bool:
|
|
return dbHandler.checkFieldValueExistence(dbConnection, "blogs", "id", blogID)
|
|
|
|
def getBlogCardRange(dbConnection: psycopg2.extensions.connection, rangeStart: int, rangeEnd: int, latestRecords = True) -> dict:
|
|
records = dbHandler.getRowRangeByID(dbConnection, "blogs", rangeStart, rangeEnd, latestRecords)
|
|
recordsDictionary = {}
|
|
for index, record in enumerate(records):
|
|
datePostedStr = record[3].strftime("%G-%m-%d %X")
|
|
# 0 index reserved for success/message status
|
|
recordsDictionary[index+1] = {
|
|
"blogID": record[0],
|
|
"authorInfo": {
|
|
"ID": record[1],
|
|
"username": userHandler.getUserInfoByID(dbConnection, record[1], "username"),
|
|
"firstName": userHandler.getUserInfoByID(dbConnection, record[1], "firstname"),
|
|
"lastName": userHandler.getUserInfoByID(dbConnection, record[1], "lastname")
|
|
},
|
|
"categoryID": record[2],
|
|
"datePosted": datePostedStr,
|
|
"title": record[4],
|
|
"description": record[5]
|
|
}
|
|
return recordsDictionary
|
|
|
|
|
|
def getBlogTitle(blogID: int) -> str:
|
|
debugPrint("Attempting to retrieve blog title of blog ID " + str(blogID))
|
|
try:
|
|
fileData = open(blogDir + "/" + str(blogID) + "/" + str(blogID) + ".org", 'r')
|
|
title = orgHandler.checkAndRetrieveMetadata(fileData, "TITLE")
|
|
if title:
|
|
return title
|
|
except Exception as error:
|
|
debugPrint("Error getting blog title! " + repr(error))
|
|
|
|
def getBlogDescription(blogID: int) -> str:
|
|
debugPrint("Attempting to retrieve blog description of blog ID " + str(blogID))
|
|
try:
|
|
fileData = open(blogDir + "/" + str(blogID) + "/" + str(blogID) + ".org", 'r')
|
|
orgRoot = loads(fileData.read())
|
|
|
|
fileData.readline()
|
|
shortDescription = orgHandler.checkAndRetrieveMetadata(fileData, "DESCRIPTION")
|
|
if not shortDescription:
|
|
debugPrint("No valid description found, will generate a placeholder from the text itself...")
|
|
firstText = orgRoot[1].body
|
|
shortDescription = (firstText[:60] + "...") if len(firstText) > 60 else firstText
|
|
return shortDescription
|
|
except Exception as error:
|
|
debugPrint("Error getting blog description! Unexpected error: " + repr(error) + ".")
|
|
|
|
def getBlogContentsHTML(blogID: int) -> str:
|
|
try:
|
|
debugPrint("Attempting to retrieve blog contents of blogID " + str(blogID) + "...")
|
|
FullBlogDir = blogDir + "/" + str(blogID)
|
|
HTMLFileData = open(FullBlogDir + "/" + str(blogID) + ".html", 'r')
|
|
HTMLFileContents = HTMLFileData.read()
|
|
HTMLFileData.close()
|
|
return HTMLFileContents
|
|
except Exception as error:
|
|
msg = "Retrieve blog contents of blogID " + str(blogID) + " failed! Unexpected error: " + repr(error) + "."
|
|
debugPrint(msg)
|
|
return 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 generateBlogTitle(dbConnection: psycopg2.extensions.connection, blogID: int) -> tuple:
|
|
return dbHandler.changeFieldValueByID(dbConnection, "blogs", blogID, "title", getBlogTitle(blogID))
|
|
|
|
def generateBlogDescription(dbConnection: psycopg2.extensions.connection, blogID: int) -> tuple:
|
|
return dbHandler.changeFieldValueByID(dbConnection, "blogs", blogID, "description", getBlogDescription(blogID))
|
|
|
|
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()
|
|
|
|
generateBlogTitle(dbConnection, blogID)
|
|
generateBlogDescription(dbConnection, blogID)
|
|
|
|
return blogID
|
|
else:
|
|
return False
|
|
except Exception as error:
|
|
debugPrint("Create blog failed! Unexpected error: " + repr(error) + ".")
|
|
return False
|