-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
282 lines (226 loc) · 8.83 KB
/
model.py
File metadata and controls
282 lines (226 loc) · 8.83 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import sqlite3
from os import error
import time
from tabulate import tabulate
from clint.textui import puts, colored, indent
from rich.progress import Progress
conn = sqlite3.connect('database.db')
db = conn.cursor()
def create_user_table():
create_users_table = """
CREATE TABLE users (
rowid INTEGER PRIMARY KEY ,
first_name blob NOT NULL,
last_name blob NOT NULL,
email blob UNIQUE NOT NULL,
phone blob NOT NULL,
password blob NOT NULL,
created_at blob NOT NULL );
"""
try:
db.execute(create_users_table)
response = "Database created"
return response
except:
response = "Users Table already created"
return response
def create_message_table():
create_messages_table = """
CREATE TABLE messages (
rowid INTEGER PRIMARY KEY ,
user_id INTERGER NOT NULL,
message text NOT NULL,
status text NOT NULL,
receiver_number text NOT NULL,
time_scheduled text NOT NULL,
created_at text NOT NULL,
updated_at text NOT NULL);
"""
try:
db.execute(create_messages_table)
response = "Message table created"
return response
except:
response = 'Message table already created'
return response
#add message
def add_scheduled_message(user_id,message,receiver_number,time_scheduled,current_date):
try:
db.execute("INSERT INTO messages (user_id,message,status, receiver_number,time_scheduled, created_at, updated_at) VALUES (?,?,'Scheduled',?,?,?,?)",(str(user_id),str(message),str(receiver_number),str(time_scheduled),str(current_date),str(current_date)))
conn.commit()
response = "Message added Successfully"
return response
except:
response = "Error adding new message"
#display all messages
def display_all_messages(user_id):
try:
db.execute("SELECT * FROM messages WHERE user_id = ?;",(str(user_id)))
record = db.fetchall()
headers = ["ROWID", "User ID", "Message", "Status" , "Receiver Number", "Time Scheduled","Created At", "Updated At"]
print(colored.green(tabulate(record, headers,tablefmt="grid")))
response = "Displaying all message"
return response
except:
response = "User not Found"
return response
def display_all_scheduled_in_the_database():
try:
db.execute("SELECT * FROM messages where status = 'Scheduled';")
record = db.fetchall()
headers = ["ROWID", "User ID", "Message", "Status" , "Receiver Number", "Time Scheduled","Created At", "Updated At"]
print(colored.green(tabulate(record, headers,tablefmt="grid")))
response = "Displaying all message"
return response
except:
response = "User not Found"
return response
# display scheduled messages
def display_scheduled_messages(user_id):
try:
db.execute("SELECT * FROM messages WHERE user_id = ? and status = 'Scheduled';",(str(user_id)))
record = db.fetchall()
headers = ["ROWID", "User ID", "Message", "Status" , "Receiver Number", "Time Scheduled","Created At", "Updated At"]
print(colored.green(tabulate(record, headers,tablefmt="grid")))
response = "Displaying all message"
return response
except:
response = "User not Found"
return response
# display cancelled messages
def display_cancelled_messages(user_id):
try:
db.execute("SELECT * FROM messages WHERE user_id = ? and status = 'Cancelled';",(str(user_id)))
record = db.fetchall()
headers = ["ROWID", "User ID", "Message", "Status" , "Receiver Number", "Time Scheduled","Created At", "Updated At"]
print(colored.green(tabulate(record, headers,tablefmt="grid")))
response = "Displaying all message"
return response
except:
response = "User not Found"
return response
# display cancelled messages
def display_sent_messages(user_id):
try:
db.execute("SELECT * FROM messages WHERE user_id = ? and status = 'Sent';",(str(user_id)))
record = db.fetchall()
headers = ["ROWID", "User ID", "Message", "Status" , "Receiver Number", "Time Scheduled","Created At", "Updated At"]
print(colored.green(tabulate(record, headers,tablefmt="grid")))
response = "Displaying all message"
return response
except:
response = "User not Found"
return response
# select single message
def display_selected_messages(rowid,user_id):
db.execute("SELECT * FROM messages WHERE rowid = ? and user_id = ?;",(str(rowid),str(user_id)))
record = db.fetchall()
#print(record)
with Progress() as progress:
task3 = progress.add_task("[cyan]Loading Messages...", total=100)
while not progress.finished:
progress.update(task3, advance=1.5)
time.sleep(0.02)
headers = ["ROWID", "User ID", "Message", "Status" , "Receiver Number", "Time Scheduled","Created At", "Updated At"]
print(colored.green(tabulate(record, headers,tablefmt="grid")))
if( len(record) == 1 ):
response = "Message Found"
return response
else:
response = "Message not Found"
return response
#edit message from single message
def edit_message_message(message,rowid):
try:
db.execute(""" UPDATE messages SET message = ? WHERE rowid = ?;""",[message,rowid])
conn.commit()
response = "Message Successfully Updated"
return response
except:
response = "Message not updated"
return response
#edit status from single message
def edit_message_status(status,rowid):
try:
db.execute(""" UPDATE messages SET status = ? WHERE rowid = ?;""",[status,rowid])
conn.commit()
response = "Status Successfully Updated"
return response
except:
response = "Status not Updated"
return response
#edit schedule from single message
def edit_message_schedule(rowid,schedule):
db.execute(''' UPDATE messages SET time_scheduled = ? WHERE rowid = ?;''',[schedule,rowid])
conn.commit()
response = "Schedule Successfully Updated"
return response
# edit first name
def edit_first_name(first_name,row_id):
try:
db.execute( ''' UPDATE users SET first_name = ? WHERE rowid = ?; ''',[first_name,row_id])
conn.commit()
response = "First Name Successfully Updated"
return response
except:
response = "Error Updating First Name"
return response
#edit last name
def edit_last_name(last_name,row_id):
try:
db.execute( ''' UPDATE users SET last_name = ? WHERE rowid = ?; ''',[last_name,row_id])
conn.commit()
response = "Last Name Successfully Updated"
return response
except:
response = "Error Updating Last Name"
return response
#change password
def edit_password(password,row_id):
try:
db.execute( ''' UPDATE users SET password = ? WHERE rowid = ?; ''',[password,row_id])
conn.commit()
response = "Password Successfully Updated"
return response
except:
response = "Error Updating Password"
return response
#change phone number
def edit_phone(phone,row_id):
try:
db.execute( ''' UPDATE users SET phone = ? WHERE rowid = ?; ''',[phone,row_id])
conn.commit()
response = "Phone Successfully Updated"
return response
except:
response = "Error Updating Phone"
return response
#forget password
def reset_password(password,email):
try:
db.execute( ''' UPDATE users SET password = ? WHERE email = ?; ''',[password,email])
conn.commit()
response = "Password Successfully Updated"
return response
except:
response = "Error Resetting Password"
return response
def display_user_profile(rowid):
db.execute("SELECT * FROM users WHERE rowid =?;",(str(rowid)))
record = db.fetchall()
#print(record)
with Progress() as progress:
task3 = progress.add_task("[cyan]Loading Messages...", total=100)
while not progress.finished:
progress.update(task3, advance=1.5)
time.sleep(0.02)
headers = ["ROWID", "First Name", "Last Name", "Email" , "Phone", "Password","Created At"]
print(colored.green(tabulate(record, headers,tablefmt="grid")))
if( len(record) == 1 ):
response = "Message Found"
return response
else:
response = "Message not Found"
return response
create_user_table()
create_message_table()