-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_database.py
More file actions
100 lines (77 loc) · 3.09 KB
/
user_database.py
File metadata and controls
100 lines (77 loc) · 3.09 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
import pymongo
import os, sys
import app_settings
import encryption as enc
import re
from user import User
from datetime import datetime, timedelta
connectionUri = os.environ.get('chatreMongoConnectionUri')
cluster = pymongo.MongoClient(connectionUri)
db = cluster["users"]
user_collection = db["users"]
def is_user_taken(username, email):
if user_collection.find({ "username":username }).count():
return 'Username Taken'
elif user_collection.find({ 'email':email }).count():
return 'Email Taken'
else:
return False
def authenticate(username, password):
user_taken = is_user_taken(username, username)
if not user_taken:
return False
elif user_taken == 'Username Taken':
u = 'username'
elif user_taken == 'Email Taken':
u = 'email'
user = user_collection.find_one({ u: username })
if password == enc.decrypt(user['password']):
return user
return False
def create_user(username, first_name, last_name, email, age, password1, password2):
not_allowed_chars = '[@_!#$%^&*()<>?/\|}]{~:,;+'
# Rules
age_rule = age < app_settings.Rules["MINIMUM_AGE"]
user_taken = is_user_taken(username, email)
password_match_rule = password1 != password2
space_in_username_rule = ' ' in username
special_char_in_username_rule = re.compile(not_allowed_chars).search(username)
if not(age_rule or user_taken or password_match_rule or space_in_username_rule or special_char_in_username_rule):
user_collection.insert_one({
"username": username,
"first_name":first_name,
"last_name":last_name,
"email":email.lower(),
"age":age,
"password": enc.encrypt(password1),
"email_confirmed": False
})
if user_collection.find_one({'username':username}):
return 'User created successfully! Please Sign in to your newly created slakr account!'
else:
return app_settings.Syntax["UNKNOWN_ERROR_TRY_AGAIN"]
elif age_rule:
return "Users must be at least 13 years of age"
elif user_taken:
return user_taken
elif password_match_rule:
return "Your passwords must match"
elif space_in_username_rule:
return "Username cannot contain a space in it"
elif special_char_in_username_rule:
return f"You cannot use any of the following characters in your username: {', '.join(not_allowed_chars)}"
else:
return app_settings.Syntax["UNKNOWN_ERROR_TRY_AGAIN"]
def get_user(username, get_by="username"):
user = user_collection.find_one({get_by:username})
if user:
return User(user['_id'], user['username'], user['first_name'], user['last_name'], user['email'])
else:
return False
def search_users(search, search_by="username"):
query = { search_by: { "$regex":search } }
search_results = user_collection.find(query)
results = []
for result in search_results:
results.append(User(result['_id'], result['username'], result['first_name'], result['last_name'], result['email']))
return results