-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
104 lines (83 loc) · 3.53 KB
/
helpers.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
from re import match
from hashlib import sha256
from random import choice
import string
def is_empty(input):
return True if input == "" else False
def has_space(input):
return True if " " in input else False
def valid_len(value):
val_len = len(value)
return True if val_len >= 3 and val_len <= 20 else False
def valid_pass(password, verify):
return True if password == verify else False
def valid_email(email):
reg = match(r"[^@]+@[^@]+\.[^@]+", email)
return True if reg != None else False
def gen_hash(password, salt=None):
if not salt:
salt = "".join(choice(string.ascii_letters) for i in range(5))
return "{0},{1}".format(sha256(str.encode(password+salt)).hexdigest(),salt)
def check_hash(pw_hash, password):
pw_hash = pw_hash.split(',')
return True if pw_hash[0] == sha256(str.encode(password+pw_hash[1])).hexdigest() else False
def check_errors(errors):
if all(True if err == "" else False for err in errors.values()):
return True
else:
return errors
def validate_signup(username, email, password, verify):
errors = {'username_err':"",'email_err':"",'password_err':"",'verify_err':""}
# Validate Username
if is_empty(username):
errors['username_err'] = "You must enter a username"
elif has_space(username):
errors['username_err'] = "Your username cannot contain spaces"
elif not valid_len(username):
errors['username_err'] = "That's not a valid username length (3-20 characters)"
# Validate Emails
if is_empty(email):
errors['email_err'] = "You must enter an email"
elif has_space(email):
errors['email_err'] = "Your email cannot contain spaces"
elif not valid_email(email):
errors['email_err'] = "That's not a valid email ([email protected])"
# Validate Passwords
if is_empty(password) or is_empty(verify):
if is_empty(password):
errors['password_err'] = "You must enter a password"
if is_empty(verify):
errors['verify_err'] = "You must enter a verification password"
elif has_space(password) or has_space(verify):
errors['password_err'] = "Your password cannot contain spaces"
elif not valid_len(password):
errors['password_err'] = "That's not a valid password length (3-20 characters)"
elif not valid_pass(password, verify):
errors['password_err'] = "Passwords don't match"
return check_errors(errors)
def validate_login(user, password):
errors = {'username_err':"",'password_err':""}
# Validate User
if not user:
errors['username_err'] = "Username not found, please try again or signup"
# Validate Passwords
if user and password == "":
errors['password_err'] = "Please enter your password"
elif user and not check_hash(user.password, password):
errors['password_err'] = "Incorrect password, please try again"
return check_errors(errors)
def validate_post(title, body):
'''Validate user login'''
errors={'title_err':"",'body_err':""}
white_punc = string.whitespace+string.punctuation
# Validate Title
if is_empty(title):
errors['title_err'] = "You must enter a title"
elif all(char in white_punc for char in title):
errors['title_err'] = "How about putting some words in your title?"
# Validate Body
if is_empty(body):
errors['body_err'] = "You must enter a body"
elif all(char in white_punc for char in body):
errors['body_err'] = "How about putting some words in your body?"
return check_errors(errors)