-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage_user.py
122 lines (95 loc) · 3.36 KB
/
manage_user.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
116
117
118
119
120
121
122
import sqlite3
import re
from datetime import date
DB = "crm.db"
TODAY = date.today()
# connect to our database and insert a new user
def insert_user(user_name, user_mail, user_phone, db=DB, entry_date=TODAY):
"""Create a new user into the users table
:param user_name:
:param user_mail:
:param user_phone:
:param db:
:param today:
:return: user_name, user_mail, user_phone, date
"""
db_connection = None
try:
db_connection = sqlite3.connect(db)
cursor = db_connection.cursor()
print("[*] DB Connection Successful!")
sql = '''INSERT INTO crm(entry_date, user_name, user_mail, user_phone)
VALUES(?,?,?,?)'''
cursor.execute(sql, (entry_date, user_name, user_mail, user_phone))
db_connection.commit()
print("[*] User Inserted!")
db_connection.close()
return entry_date, user_name, user_mail, user_phone
except ConnectionError as e:
print(f"[!] DB connection aborted! Error:{e}")
return f"[!] DB connection aborted! Error:{e}"
# extract all users from the users table
def retrieve_all_users(db=DB):
"""Retrieve all users from the users table
:param db:
:return: users
"""
db_connection = None
try:
db_connection = sqlite3.connect(db)
cursor = db_connection.cursor()
print("[*] DB Connection Successful!")
sql = '''SELECT rowid, entry_date, user_name, user_mail, user_phone FROM crm'''
cursor.execute(sql)
users = cursor.fetchall()
db_connection.commit()
print("[*] Users Retrieved!")
db_connection.close()
print(users)
return users
except ConnectionError as e:
print(f"[!] DB connection aborted! Error:{e}")
return f"[!] DB connection aborted! Error:{e}"
# extract a specific user from the users table
def retrieve_user(user_name, db=DB):
"""Retrieve one users from the users table
:param user_name:
:param db:
:return: user data
"""
db_connection = None
try:
db_connection = sqlite3.connect(db)
cursor = db_connection.cursor()
print("[*] DB Connection Successful!")
sql = '''SELECT rowid, entry_date, user_name, user_mail, user_phone
FROM crm
WHERE user_name = ?'''
cursor.execute(sql, (user_name,))
user = cursor.fetchall()
db_connection.commit()
db_connection.close()
if user != []:
print("[*] User Retrieved!")
else:
print(f"[!] User {user_name} not found")
user = [[(f"[!] User {user_name} not found")]]
print(user)
return user
except ConnectionError as e:
print(f"[!] DB connection aborted! Error:{e}")
return f"[!] DB connection aborted! Error:{e}"
# check formal correctness of user email
def mail_check(email):
"""Check if the email has a valid format
"""
regex = "^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$"
if (re.search(regex, email)):
return True
else:
return False
# call the functions
if __name__ == '__main__':
insert_user("dummy_user", "dummy@dummy.com", "1234567890")
retrieve_all_users()
retrieve_user("carlo")