forked from arvindh75/BetterSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.py
47 lines (41 loc) · 1.58 KB
/
validators.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
from werkzeug.security import check_password_hash
from models import db, User, Dues, Transactions
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
import re
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///bettersplit.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
def validate_login(username, password):
errors = {}
existing_user = User.query.filter_by(username = username).first()
if not existing_user:
errors['username'] = 'User does not exist!'
else:
if not check_password_hash(existing_user.password, password):
errors['password'] = 'Incorrect password!'
return errors
def validate_signup(name, username, password, phone):
errors = {}
if len(name) < 3:
errors['name'] = 'Use your real name!'
if len(username) < 5:
errors['username'] = 'Username must be at least 5 characters long!'
else:
username_taken = User.query.filter_by(username = username).first()
if username_taken:
errors['username'] = 'Username is already taken!'
if len(password) < 8:
errors['password'] = 'Password is too short!'
check_phone = '^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$'
result = re.match(check_phone, phone)
if not result or len(phone) != 10:
errors['phone'] = 'Invalid phone number!'
else:
phone_taken = User.query.filter_by(phone = phone).first()
if phone_taken:
errors['phone'] = 'Phone number already in use!'
return errors
def validate_quickpay(amount, to):
pass