Skip to content

Commit 4a1cbb2

Browse files
author
Dan Finn
committed
adding stuff that was missing
1 parent 4a30b50 commit 4a1cbb2

File tree

7 files changed

+708
-0
lines changed

7 files changed

+708
-0
lines changed

__pycache__/users.cpython-35.pyc

1.93 KB
Binary file not shown.

create_database.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import sqlite3
2+
3+
db = sqlite3.connect('guestbook.db')
4+
db.execute('create table users (id INTEGER PRIMARY KEY autoincrement, username text, password text, date_joined int)')
5+
db.execute('create table posts (id INTEGER PRIMARY KEY autoincrement, poster_id int, title text, body text, time_posted int)')
6+
db.commit()
7+
db.close()

guestbook.db

16 KB
Binary file not shown.

guestbook.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import users
2+
3+
def display_menu():
4+
print('1: Login')
5+
print('2: Register')
6+
if users.is_logged_in == True:
7+
print('3: Post Comment')
8+
print('9: Exit')
9+
choice = input('Enter the number of where you want to go: ')
10+
check_choice(choice)
11+
12+
def check_choice(choice):
13+
if choice.isdigit():
14+
choice = int(choice)
15+
if choice == 1:
16+
users.login()
17+
display_menu()
18+
elif choice == 2:
19+
users.register()
20+
display_menu()
21+
elif choice == 3:
22+
pass
23+
elif choice == 9:
24+
pass
25+
else:
26+
print('Please choose a correct option')
27+
display_menu()
28+
else:
29+
print('Please choose a correct option')
30+
display_menu()
31+
display_menu()

users.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import sqlite3
2+
db = sqlite3.connect('guestbook.db')
3+
is_logged_in = False
4+
user_id = 0 # 0 means not set / not logged in.
5+
6+
def register():
7+
username = str(input('Please enter a username: '))
8+
if username_available(username) == True :
9+
import hashlib, time
10+
password = input('Please enter a password: ').encode('utf_8')
11+
encrypted = hashlib.sha256()
12+
encrypted.update(password)
13+
newpass = encrypted.hexdigest()
14+
15+
currenttime = int(time.time())
16+
db.execute('insert into users (username, password, date_joined) values (?,?,?)',(username,newpass, currenttime))
17+
db.commit()
18+
print('You have been added to the user database')
19+
return True
20+
else:
21+
print('Unfortunately, that username is already taken. Please try again')
22+
register()
23+
def login():
24+
username = input('Please enter your username: ')
25+
if username_available(username) == False: #if its taken, then it must exist.
26+
import hashlib
27+
password = input('Please enter your password: ').encode('utf_8')
28+
encrypted = hashlib.sha256()
29+
encrypted.update(password)
30+
newpass = encrypted.hexdigest()
31+
32+
db.row_factory = sqlite3.Row
33+
row = db.execute('select id from users where username = ? and password = ?', (username, newpass))
34+
if row is None:
35+
print('Sorry, that password doesn\' match the username')
36+
else:
37+
user_id = row.fetchone()['id']
38+
is_logged_in = True
39+
else:
40+
print('That user doesn\'t exist. Register it?')
41+
def logout():
42+
pass
43+
def username_available(username):
44+
db.row_factory = sqlite3.Row
45+
row = db.execute('select id from users WHERE username = ?',(username,))
46+
47+
if row.fetchone() is None:
48+
return True
49+
else:
50+
return False

users.pyc

2.21 KB
Binary file not shown.

0 commit comments

Comments
 (0)