repl.it
@pyelias/

Forum

Python

No description

fork
loading
Files
  • main.py
  • db.py
  • query.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# this is literally a forum! i better get some votes for this lol

#from replit import clear

from query import *
import db

def clear():
  print("\n" * 50) # replace this when replit module is fixed

def get_num(prompt, min_, max_):
  num = ""
  while not (num.isdigit() and min_ <= int(num) <= max_):
    num = input(prompt)
  
  return int(num)

def get_yn(prompt):
  given = ""
  while given not in ["y", "n"]:
    given = input(prompt)
  return given == "y"

posts =    db.ConnectedDatabase("forum1", "posts")
comments = db.ConnectedDatabase("forum1", "comments")

clear()

while True:
  all_posts = posts.query_with_id()
  
  for ind, (_, post) in enumerate(all_posts, 1):
    print(ind, post["title"])
  
  print("\n")
  print("Actions:")
  print("  1: view post")
  print("  2: make post")
  print("  3: exit")
  which = get_num("What to do (1,2,3)?", 1, 3)
  
  if which == 1:
    print("\n")
    which = get_num("Which post to view (number)?", 1, len(all_posts)) - 1
    
    post_pk, post = all_posts[which]
    while True:
      clear()
      print(post["title"])
      print()
      print(post["text"])
      for comment_pk in post["comments"]:
        print("-" * 50)
        comment, = comments.query(Pks([comment_pk]))
        print(comment["text"])
        
      print("\n")
      print("Actions:")
      print("  1: comment")
      print("  2: back to main menu")
      
      which = get_num("What to do (1,2)?", 1, 2)
      
      if which == 1:
        clear()
        print("What to say?")
        lines = []
        line = "a"
        while line:
          line = input()
          lines.append(line)
        comment_text = "\n".join(lines).strip()
        if comment_text and get_yn("Are you sure you want to comment (y/n)?"):
          comment_entry = {"post": post_pk, "text": comment_text}
          comment_pk, = comments.insert([comment_entry])
          post, = posts.query(Pks([post_pk]))
          post["comments"].append(comment_pk)
          posts.replace([post], Pks([post_pk]))
          clear()
        else:
          clear()
          print("Comment canceled\n")
        
      else:
        clear()
        break

  elif which == 2:
    clear()
    title = input("What to call the post?").strip()
    clear()
    print("What to say?")
    lines = []
    line = "a"
    while line:
      line = input()
      lines.append(line)
    text = "\n".join(lines).strip()
    if text and title and get_yn("Are you sure you want to post (y/n)?"):
      post = {"title": title, "text": text, "comments": []}
      posts.insert([post])
      clear()
    else:
      clear()
      print("Post canceled\n")
  
  else:
    clear()
    break
?