Finished the rest of the path types

This commit is contained in:
sloumdrone 2018-12-16 10:18:24 -08:00
parent 07432b33c5
commit b7997af3e3
1 changed files with 76 additions and 15 deletions

91
cspc.py
View File

@ -27,6 +27,7 @@ user = os.path.split(userdir)[-1]
topic = None
post = None
available_ids = []
messages = []
def go_back():
@ -53,8 +54,7 @@ def get_prompt():
def get_data():
if post and topic:
# sql to view a post and its replies
pass
get_single_post()
elif topic:
get_posts()
else:
@ -62,7 +62,15 @@ def get_data():
def show_help():
print('This is a help menu placeholder')
help_menu = [
'{}add:{} add a new topic/post/reply (whichever is relavent tot he screen you are on)'.format(c.cyan, c.end),
'{}back:{} move up a level (post->posts->topics)'.format(c.cyan, c.end),
'{}help:{} display this menu'.format(c.cyan, c.end),
'{}item id:{} enter the item number to view it'.format(c.cyan, c.end),
'{}q, quit, exit:{} leave cspc'.format(c.cyan, c.end)
]
for x in help_menu:
messages.append(x)
def make_add():
@ -114,6 +122,7 @@ def add_new(a):
def parse_command(com):
global topic
global post
global messages
comlist = ['add', 'help', 'quit', 'back']
if com == 'back':
@ -130,16 +139,16 @@ def parse_command(com):
try:
ident = int(com)
if not ident in available_ids:
print('{}ERROR: {} is not an available option!\n'.format(c.red, com, c.end))
messages.append('{}ERROR: {} is not an available option!\n'.format(c.red, com, c.end))
return False
if not topic:
topic = ident
elif not post:
post = ident
else:
print('{}A number command is not relevant right now...{}'.format(c.purple, c.end))
messages.append('{}A number command is not relevant right now...{}'.format(c.purple, c.end))
except ValueError:
print('{}Input not recognized{}'.format(c.red, c.end))
messages.append('{}Input not recognized{}'.format(c.red, c.end))
#------- DB Related --------#
@ -154,6 +163,7 @@ def check_and_build_db():
conn.close()
def db_do(query, var=False, noresval=False):
global messages
if os.path.isfile(db_path):
conn = sql.connect(db_path)
c = conn.cursor()
@ -171,7 +181,7 @@ def db_do(query, var=False, noresval=False):
conn.close()
return out
else:
print("{}ERROR:{} Database cannot be found or is corrupt".format(c.red, c.end))
messages.append("{}ERROR:{} Database cannot be found or is corrupt".format(c.red, c.end))
return False
@ -182,9 +192,10 @@ def get_posts():
q = "SELECT rowid, title, author, last_updated FROM data WHERE type = 'post' and topic_id = ? ORDER BY last_updated DESC"
v = (topic,)
res = db_do(q, v)
print('{}{:^6} | {:^35} | {:^12} | {:^18}{}'.format(c.yellow, 'ID', 'Post Title', 'Author', 'Last Updated', c.end))
print('{}{:^6} {:35} {:12} {:18}{}'.format(c.yellow, 'ID', 'Post Title', 'Author', 'Last Updated', c.end))
print('{}-----------------------------------------------------------------------------{}'.format( c.yellow, c.end))
if not len(res):
print('{}There are no posts for this topic yet. Add one!{}'.format(c.cyan, c.end))
print('\n{}There are no posts for this topic yet. Add one!{}'.format(c.cyan, c.end))
else:
for row in res:
available_ids.append(row[0])
@ -192,7 +203,7 @@ def get_posts():
title = row[1]
if len(title) > 35:
title = '{}...'.format(title[:32])
print('({:^4}) | {:35} | {:12} | {:18}'.format(row[0], title, row[2], dtime))
print('({:^4}) {:35} {:12} {:18}'.format(row[0], title, row[2], dtime))
return True
@ -201,38 +212,88 @@ def get_topics():
available_ids = []
q = "SELECT title, last_updated, rowid FROM data WHERE type = 'topic' ORDER BY last_updated DESC"
res = db_do(q)
print('{}{:^6} | {:35} | {:18}{}'.format(c.yellow, 'ID', 'Topic Name', 'Last Updated', c.end))
print('{}{:^6} {:35} {:18}{}'.format(c.yellow, 'ID', 'Topic Name', 'Last Updated', c.end))
print('{}-----------------------------------------------------------------{}'.format( c.yellow, c.end))
for row in res:
available_ids.append(row[2])
dtime = datetime.datetime.utcfromtimestamp(row[1]).strftime('%Y-%m-%d %H:%M')
title = row[0]
if len(title) > 35:
title = '{}...'.format(title[:32])
print('({:^4}) | {}{:35}{} | {:18}'.format(row[2], c.cyan, title, c.end, dtime))
print('({:^4}) {}{:35}{} {:18}'.format(row[2], c.cyan, title, c.end, dtime))
return True
def get_single_post():
global topic
global post
q1 = "SELECT title, body, author, last_updated FROM data WHERE type = 'post' and topic_id = ? and rowid = ?"
v1 = (topic, post)
res1 = db_do(q1, v1)
q2 = "SELECT body, author, last_updated FROM data WHERE type = 'reply' and post_id = ? and topic_id = ? ORDER BY last_updated ASC"
v2 = (post, topic)
res2 = db_do(q2, v2)
post_title = "{}Title:{} {}{}".format(c.yellow, c.white, res1[0][0], c.end)
post_author = "{}Author:{} {}{}".format(c.yellow, c.white, res1[0][2], c.end)
dtime = datetime.datetime.utcfromtimestamp(res1[0][3]).strftime('%Y-%m-%d %H:%M')
post_time = "{}Post date:{} {}{}\n".format(c.yellow, c.white, dtime, c.end)
post_body = res1[0][1]
print(post_title)
print(post_author)
print(post_time)
print(post_body)
print("\n{}- - - - - - - - - - - - - - - -{}\n".format(c.white, c.end))
if len(res2):
for row in res2:
dtime = datetime.datetime.utcfromtimestamp(row[2]).strftime('%Y-%m-%d %H:%M')
print("{} | {}\n".format(row[1],dtime))
print(row[0])
print("\n- - - - - - -\n")
else:
print('No replies. Add one!')
def update_time(t):
if topic and post:
q = "UPDATE data SET last_updated = ? WHERE rowid in (?, ?)"
v = (t, topic, post)
elif topic:
q = "UPDATE data SET last_updated = ? WHERE rowid = ?"
v = (t, topic)
else:
return True
res = db_do(q, v, noresval=True)
return res
def add_to_db(action, data):
update_time = int(time.time())
utime = int(time.time())
q = "INSERT INTO data VALUES (?, ?, ?, ?, ?, ?, ?)"
v = (topic, post, action, data['title'], data['body'], user, update_time)
v = (topic, post, action, data['title'], data['body'], user, utime)
res = db_do(q, v, noresval=True)
if not res:
print("{}ERROR:{} There was an error adding your topic".format(c.red, c.end))
messages.append("{}ERROR:{} There was an error adding your topic".format(c.red, c.end))
return False
tupdate = update_time(utime)
return True
#------- Main loop --------#
def mainloop():
global messages
while True:
subprocess.run(['clear'])
print('{}Colorfield Space Bulletin Board{}\n\n'.format(c.b_blue, c.end))
output = get_data()
# print the formatted data
print('')
for x in messages:
print(x)
print('')
command = input(get_prompt())
print('')
messages = []
if not command:
continue
parse_command(command)