def join_qaf(self, qaf_id): qafs = self.qafs_joined.split(',') if qaf_id not in qafs: command = 'UPDATE users \ SET qafs_joined = qafs_joined || "{}," \ WHERE id = {}'.format(qaf_id, self.id) execute(command)
def get_comments(self): command = f'SELECT id FROM comments WHERE post_id = {self.id}' data = execute(command).fetchall() comments = [] for comment_id in data: comments.append(Comment(comment_id[0])) return comments
def get_posts(self): command = 'SELECT id FROM posts WHERE qaf_id = {} ORDER BY time_created DESC'.format( self.id) data = execute(command).fetchall() posts = [] for post_id in data: posts.append(Post(post_id[0])) return posts
def __init__(self, id): command = 'SELECT * FROM qafs WHERE id={}'.format(id) data = execute(command).fetchall() self.id = int(data[0][0]) self.name = str(data[0][1]) self.owner_id = int(data[0][2]) self.mode = str(data[0][3]) self.owner = util.user.User(self.owner_id).username
def get_user(username): command = 'SELECT id from users WHERE username = "******"'.format(username) data = execute(command).fetchall() if len(data ) == 0: # if no user exists with the username then return None return None else: return User(data[0][0]) #returns user object
def __init__(self, id): command = 'SELECT * FROM users WHERE id={}'.format(id) data = execute(command).fetchall() self.id = int(data[0][0]) self.username = str(data[0][1]) self.password = str(data[0][2]) self.waffles = int(data[0][3]) self.qafs_joined = str(data[0][4])
def qafSearch(): input = request.args.get("input", 0, type=str) if (input == ""): return jsonify(result="") output = execute( f"""SELECT * FROM 'qafs' WHERE name LIKE '%{input}%' AND mode = 'public';""" ).fetchall() print(output) return jsonify(result=output)
def __init__(self, id): command = 'SELECT * FROM comments WHERE id={}'.format(id) data = execute(command).fetchall() self.id = int(data[0][0]) self.author_id = int(data[0][1]) self.author = util.user.User(self.author_id).username self.content = str(data[0][2]) self.post_id = int(data[0][3]) self.qaf_id = int(data[0][4]) self.time_created = str(data[0][5]) self.net_vote = int(data[0][6])
def __init__(self, id): command = 'SELECT * FROM posts WHERE id={}'.format(id) data = execute(command).fetchall() self.id = int(data[0][0]) self.author_id = int(data[0][1]) self.title = str(data[0][2]) self.content = str(data[0][3]) self.qaf_id = int(data[0][4]) self.tags = str(data[0][5]).split(',') self.tags_str = str(data[0][5]) self.time_created = str(data[0][6]) self.net_vote = int(data[0][7])
def downvoted(self): command = 'UPDATE posts \ SET net_vote = {} \ WHERE id = {}'.format(self.net_vote - 1, self.id) execute(command)
def new_user(username, password): command = f'INSERT INTO users (username, password, waffles, qafs_joined) VALUES ("{username}", "{password}", 0, "")' execute(command)
def username_avaliable(username): command = 'SELECT id FROM users WHERE username = "******"'.format(username) data = execute(command).fetchall() return len(data) == 0
def change_password(self, new_pass): command = 'UPDATE users \ SET password = "******" \ WHERE id = {}'.format(new_pass, self.id) execute(command)
def update(self, title, content, tags): command = f'UPDATE posts SET title = "{title}", content = "{content}", tags = "{tags}" WHERE id = {self.id}' execute(command)
def new_post(author_id, title, content, qaf_id, tags): command = f'INSERT INTO posts (author_id, title, content, qaf_id, tags, net_vote) VALUES ("{author_id}", "{title}", "{content}", {qaf_id}, "{tags}", 0)' execute(command) util.user.User(author_id).join_qaf(qaf_id)
def new_qaf(name, owner_id, mode): command = 'INSERT INTO qafs (name, owner_id, mode) VALUES ("{}", "{}", "{}")'.format( name, owner_id, mode) cursor = execute(command) return cursor.lastrowid
def downvote(self): command = 'UPDATE comments \ SET net_vote = "{}" \ WHERE id = {}'.format(self.net_vote - 1, self.id) execute(command)
def new_comment(author_id, content, post_id, qaf_id): command = f'INSERT INTO comments (author_id, content, post_id, qaf_id, net_vote) VALUES ("{author_id}", "{content}","{post_id}","{qaf_id}", 0)' execute(command) util.user.User(author_id).join_qaf(qaf_id)
def update(self, content): command = f'UPDATE comments SET content = "{content}" WHERE id = {self.id}' execute(command)