-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdatabase.py
More file actions
32 lines (26 loc) · 1.07 KB
/
database.py
File metadata and controls
32 lines (26 loc) · 1.07 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
import pymongo, hashlib
connection = pymongo.Connection()
db = connection["database"]
users = db.users
posts = db.posts
#return true if valid input; false if not
def checkPassword(passwordToCheck):
return len(passwordToCheck) > 0
def checkUsername(usernameToCheck):
return ((len(usernameToCheck) > 0) and (users.find({"username":usernameToCheck}).count()==0))
def checkPost(postToCheck):
return len(postToCheck) > 0
def addUser(username, password, emailaddress, gender, age):
record = users.find({"username":username})
if ((checkUsername(username) == False) or (checkPassword(password) == False)) or (record.count() != 0):
return False
else:
newUser = {"username": username, "password": hashlib.sha512(password).hexdigest(), "gender": gender, "age": age,"email" : emailaddress}
users.insert(newUser)
return True
def validateUser(username, password):
record = users.find({"username":username})
if (record.count() != 1):
return False
else:
return record[0]['password'] == hashlib.sha512(password).hexdigest()