-
Notifications
You must be signed in to change notification settings - Fork 0
/
dboperations.py
116 lines (102 loc) · 3.75 KB
/
dboperations.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
110
111
112
113
114
115
import sqlite3
import datetime
class DBOperations(object):
"""
Simple wrapper over common SQLite operations.
"""
def __init__(self, path, type='sqlite'):
self.type = type
self.path = path
self.connection = sqlite3.connect(path)
self.cursor = self.connection.cursor()
def setup(self):
"""
Creates tables if not already present
:return: None
"""
self.cursor.execute(
"CREATE TABLE IF NOT EXISTS user (username TEXT UNIQUE NOT NULL, password TEXT, last_login DATETIME, last_logout DATETIME, online INT);")
self.cursor.execute(
"CREATE TABLE IF NOT EXISTS messages (from_user REFERENCES user (username), to_user REFERENCES user (username),content TEXT, timestamp DATETIME);")
self.connection.commit()
def add_user(self, username, password):
"""
Adds a user to the database. Stores password in plain text.
:param username:
:param password:
:return:
"""
self.cursor.execute("INSERT INTO user VALUES (?,?,?,NULL,?);", (username, password, datetime.datetime.now(), 0))
self.connection.commit()
def add_message(self, from_user, to_user, message):
"""
Adds a message to the message table with appropriate foreign keys
:param to_user:
:param from_user:
:param message:
:return:
"""
self.cursor.execute("INSERT INTO messages VALUES (?,?,?,?);",
(from_user, to_user, message, datetime.datetime.now()))
self.connection.commit()
def update_login(self, username):
"""
Updates the last login time of a user
:param username:
:return:
"""
self.cursor.execute("UPDATE user SET last_login = ? WHERE username LIKE ?;",
(datetime.datetime.now(), username))
self.connection.commit()
def update_logout(self, username):
"""
Updates the last log out time of the user
:param username:
:return:
"""
self.cursor.execute("UPDATE user SET last_logout = ? WHERE username LIKE ?;",
(datetime.datetime.now(), username))
self.connection.commit()
def return_all_users(self):
"""
Returns all registered users
:return:
"""
return self.cursor.execute("SELECT username FROM user;").fetchall()
def return_online_users(self):
"""
Returns all users who are online
:return:
"""
return self.cursor.execute("SELECT username FROM user WHERE online = 1;").fetchall()
def auth_user(self, username, password):
"""
Attempts to authenticate credentials against a database
:param username:
:param password:
:return: 0 or 1 (Boolean)
"""
return len(self.cursor.execute('SELECT * FROM user WHERE username LIKE ? AND password LIKE ?;',
(username, password)).fetchall()) > 0
def set_online(self, username):
"""
Sets a users status to be online
:para m username:
:return:
"""
self.cursor.execute("UPDATE user SET online = 1 WHERE username LIKE ?;",
(username,))
self.connection.commit()
def set_offline(self, username=False, all=False):
"""
Sets a users status to be online
:param username:
:param all:
:return:
"""
if all:
self.cursor.execute("UPDATE user SET online = 0;")
elif username:
self.cursor.execute("UPDATE user SET online = 0 WHERE username LIKE ?;",
(username,))
self.connection.commit()