initial commit
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
*.db
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,62 @@
|
|||||||
|
import sqlite3, datetime
|
||||||
|
|
||||||
|
def initBlog(db_file):
|
||||||
|
db = sqlite3.connect(db_file)
|
||||||
|
db.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS posts (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
title TEXT,
|
||||||
|
body TEXT NOT NULL,
|
||||||
|
timestamp INTEGER
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
return db
|
||||||
|
|
||||||
|
def newPost(db, title, body):
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
time = int(now.timestamp() * 1000)
|
||||||
|
db.execute(
|
||||||
|
"INSERT INTO posts (title, body, timestamp) VALUES (?, ?, ?)",
|
||||||
|
(title, body, time)
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
def getPost(db, index: int):
|
||||||
|
db.row_factory = sqlite3.Row
|
||||||
|
cur = db.cursor()
|
||||||
|
cur.execute("SELECT * FROM posts WHERE id = (?)", (index,))
|
||||||
|
return cur.fetchone()
|
||||||
|
|
||||||
|
|
||||||
|
def getAllPosts(db):
|
||||||
|
db.row_factory = sqlite3.Row
|
||||||
|
return db.execute("SELECT * FROM posts ORDER BY timestamp DESC").fetchall()
|
||||||
|
|
||||||
|
def deletePost(db, index: int):
|
||||||
|
db.execute(
|
||||||
|
"DELETE FROM posts WHERE id = ?",
|
||||||
|
(index,)
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
def updateTitle(db, index: int, title):
|
||||||
|
db.execute(
|
||||||
|
"UPDATE posts SET title = (?) WHERE id = (?)",
|
||||||
|
(title, index,)
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
def updateBody(db, index: int, body):
|
||||||
|
db.execute(
|
||||||
|
"UPDATE posts SET body = (?) WHERE id = (?)",
|
||||||
|
(body, index,)
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
#if __name__ == "__main__":
|
||||||
|
#db = initBlog("blog.db")
|
||||||
|
|
||||||
|
#db.close()
|
||||||
|
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
from blahblah import bbdb
|
||||||
|
import tempfile
|
||||||
|
import subprocess
|
||||||
|
import os
|
||||||
|
|
||||||
|
def mainMenu():
|
||||||
|
print("""
|
||||||
|
_ _ _ _ _ _
|
||||||
|
| |__ | | __ _| |__ | |__ | | __ _| |__
|
||||||
|
| '_ \\| |/ _` | '_ \\| '_ \\| |/ _` | '_ \\
|
||||||
|
| |_) | | (_| | | | | |_) | | (_| | | | |
|
||||||
|
|_.__/|_|\\__,_|_| |_|_.__/|_|\\__,_|_| |_|
|
||||||
|
|
||||||
|
1. [N]ew Post
|
||||||
|
2. [E]dit Post
|
||||||
|
3. [D]elete Post
|
||||||
|
4. [L]ist Posts
|
||||||
|
|
||||||
|
5. [G]enerate HTML
|
||||||
|
|
||||||
|
6. [Q]uit
|
||||||
|
""")
|
||||||
|
choice = input("Choose an option: ")
|
||||||
|
return choice
|
||||||
|
|
||||||
|
def writeNew(db):
|
||||||
|
print("""
|
||||||
|
|
||||||
|
Write New Post
|
||||||
|
|
||||||
|
1. [N]ew Micropost
|
||||||
|
2. [L]oad Post From File
|
||||||
|
""")
|
||||||
|
|
||||||
|
choice = input("Choose an option: ")
|
||||||
|
|
||||||
|
if choice == "n" or choice == "N" or choice == "1":
|
||||||
|
post = input("Enter your post: ")
|
||||||
|
print('You entered: "' + post + '"')
|
||||||
|
confirmPost = input("Publish this micropost (Y/N)? ")
|
||||||
|
if confirmPost == 'y' or confirmPost == 'Y':
|
||||||
|
bbdb.newPost(db, None, post)
|
||||||
|
|
||||||
|
def editPost(db):
|
||||||
|
try:
|
||||||
|
postID = int(input("Enter the numerical ID of the post to edit or [Q]uit: "))
|
||||||
|
except ValueError:
|
||||||
|
print("Cancelled edit.")
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
post = bbdb.getPost(db, postID)
|
||||||
|
except:
|
||||||
|
print("Error finding post.")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
print(post["id"], post["title"])
|
||||||
|
print(post["body"])
|
||||||
|
|
||||||
|
print("""
|
||||||
|
|
||||||
|
1. Edit [T]itle
|
||||||
|
2. Edit [B]ody
|
||||||
|
|
||||||
|
""")
|
||||||
|
|
||||||
|
repeat = True
|
||||||
|
|
||||||
|
while (repeat):
|
||||||
|
choice = input("Choose which field to edit or [Q]uit: ")
|
||||||
|
if choice == 't' or choice == 'T' or choice == "1":
|
||||||
|
newTitle = input("Enter a new title: ")
|
||||||
|
print('Your new title is "' + newTitle + '"')
|
||||||
|
confirmEdit = input("Publish this edited title [Y/N]? ")
|
||||||
|
if confirmEdit == 'y' or confirmEdit == 'Y':
|
||||||
|
repeat = False
|
||||||
|
try:
|
||||||
|
bbdb.updateTitle(db, postID, newTitle)
|
||||||
|
except:
|
||||||
|
print("Error updating title.")
|
||||||
|
else:
|
||||||
|
print("Updated title.")
|
||||||
|
elif choice == 'b' or choice == 'B' or choice == "2":
|
||||||
|
editor = os.environ.get("EDITOR", os.environ.get("VISUAL", "vi"))
|
||||||
|
|
||||||
|
with tempfile.NamedTemporaryFile(suffix=".tmp", mode="w+", delete=False) as tf:
|
||||||
|
tf.write(post["body"])
|
||||||
|
path = tf.name
|
||||||
|
|
||||||
|
try:
|
||||||
|
subprocess.call([editor, path])
|
||||||
|
with open(path, "r") as f:
|
||||||
|
newBody = f.read()
|
||||||
|
finally:
|
||||||
|
os.unlink(path)
|
||||||
|
|
||||||
|
print('Your new body text is:\n\n' + newBody + "\n")
|
||||||
|
confirmEdit = input("Publish this edited post [Y/N]? ")
|
||||||
|
if confirmEdit == 'y' or confirmEdit == 'Y':
|
||||||
|
repeat = False
|
||||||
|
try:
|
||||||
|
bbdb.updateBody(db, postID, newBody)
|
||||||
|
except:
|
||||||
|
print("Error updating post body.")
|
||||||
|
else:
|
||||||
|
print("Updated post body.")
|
||||||
|
|
||||||
|
elif choice == 'q' or choice == 'Q':
|
||||||
|
repeat = False
|
||||||
|
|
||||||
|
|
||||||
|
def listPosts(db):
|
||||||
|
for p in bbdb.getAllPosts(db):
|
||||||
|
print("***")
|
||||||
|
print(p["id"], p["title"])
|
||||||
|
print(p["body"])
|
||||||
|
print("\n")
|
||||||
|
|
||||||
|
def confirmDelete(db):
|
||||||
|
try:
|
||||||
|
post = int(input("Enter the ID of the post to PERMANENTLY DELETE or [Q]uit: "))
|
||||||
|
except ValueError:
|
||||||
|
print("Cancelled delete.")
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
bbdb.deletePost(db, post)
|
||||||
|
except:
|
||||||
|
print(post)
|
||||||
|
print("Error deleting post")
|
||||||
|
else:
|
||||||
|
print("Deleted post ", post)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
db = bbdb.initBlog("blog.db")
|
||||||
|
|
||||||
|
running = True;
|
||||||
|
foo = 0;
|
||||||
|
|
||||||
|
while(running):
|
||||||
|
choice = mainMenu()
|
||||||
|
|
||||||
|
if choice == 'q' or choice == 'Q' or choice == "6":
|
||||||
|
running = False
|
||||||
|
elif choice == 'n' or choice == 'N' or choice == "1":
|
||||||
|
writeNew(db)
|
||||||
|
elif choice == 'e' or choice == 'E' or choice == "2":
|
||||||
|
editPost(db)
|
||||||
|
elif choice == 'd' or choice == 'D' or choice == "3":
|
||||||
|
confirmDelete(db)
|
||||||
|
elif choice == 'l' or choice == 'L' or choice == "4":
|
||||||
|
listPosts(db)
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
|
||||||
Reference in New Issue
Block a user