-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
60 lines (48 loc) · 1.92 KB
/
handlers.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
import sqlite3
import smtplib
class UserError(Exception):
pass
class SqlHandler:
def __init__(self):
self.connection = sqlite3.connect("comments.db")
self.cursor = self.connection
cmd = """
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
username TEXT
email TEXT
password TEXT);"""
self.cursor.execute(cmd)
cmd = """
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY,
senderId INTEGER
comment TEXT);"""
self.cursor.execute(cmd)
def login(self, username, password):
cmd = "SELECT 1 FROM users WHERE ((username = {0} OR email = {0}) AND password = {1});".format(username, password)
self.cursor.execute(cmd)
user = self.cursor.fetchone()
try:
assert user != None
except AssertionError:
raise UserError("While logging in: Username or password not correct.")
return user
def signup(self, username, email, password):
cmd = "SELECT 1 FROM users WHERE (username = \"{0}\" OR email = \"{1}\");".format(username, email)
self.cursor.execute(cmd)
try:
assert user == None
except AssertionError:
raise UserError("While signing up: User with username or email already exists")
return False
cmd = "INSERT INTO users VALUES(NULL, {0}, {1}, {2});\n SELECT 1 FROM users WHERE (username = {0});".format(username, email, password)
self.cursor.execute(cmd)
return self.cursor.fetchone()
def sendComment(self, senderId, comment):
cmd = "INSERT INTO comments VALUES(NULL, {0}, {1});".format(senderId, comment)
self.cursor.execute(cmd)
def showComments(self):
cmd = "SELECT * FROM comments"
self.cursor.execute(cmd)
return self.fetchall