validate module

This commit is contained in:
CHIEFSOFT\ameye
2024-12-08 17:49:02 -05:00
parent 935fba5111
commit d76c696420
3 changed files with 49 additions and 25 deletions
+12 -25
View File
@@ -5,6 +5,11 @@ from functools import wraps
import datetime
import jwt
import project.validate.validate as validate
#from models.models import User
from flask import (
Flask,
jsonify,
@@ -12,7 +17,7 @@ from flask import (
request,
)
from flask_sqlalchemy import SQLAlchemy
from werkzeug.utils import secure_filename
#from werkzeug.utils import secure_filename
load_dotenv()
@@ -70,6 +75,12 @@ def start_login():
"data": None,
"error": "Bad request"
}, 400
# validate input
is_validated = validate.validate_username_and_password(data.get('username'), data.get('password'))
if is_validated is not True:
return dict(message='Invalid data', data=None, error=is_validated), 400
GLOBAL_AVG = """SELECT * FROM members WHERE id = 1;"""
with connection:
@@ -120,27 +131,3 @@ def panel_products():
def recent_actions():
return jsonify(hello="ameye world")
@app.route("/static/<path:filename>")
def staticfiles(filename):
return send_from_directory(app.config["STATIC_FOLDER"], filename)
@app.route("/media/<path:filename>")
def mediafiles(filename):
return send_from_directory(app.config["MEDIA_FOLDER"], filename)
@app.route("/upload", methods=["GET", "POST"])
def upload_file():
if request.method == "POST":
file = request.files["file"]
filename = secure_filename(file.filename)
file.save(os.path.join(app.config["MEDIA_FOLDER"], filename))
return """
<!doctype html>
<title>upload new File</title>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file><input type=submit value=Upload>
</form>
"""
+37
View File
@@ -0,0 +1,37 @@
"""Validator Module"""
import re
#from bson.objectid import ObjectId
def validate(data, regex):
"""Custom Validator"""
return True if re.match(regex, data) else False
def validate_password(password: str):
"""Password Validator"""
reg = r"\b^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{8,20}$\b"
return validate(password, reg)
def validate_username(username: str):
if not 6 <= len(username.split(' ')) <= 15:
return {
'name': 'Username must be between 6 and 15 words'
}
return True
def validate_username_and_password(username, password):
"""Username and Password Validator"""
if not (username and password):
return {
'username': 'Username is required',
'password': 'Password is required'
}
if not validate_username(username):
return {
'email': 'Username is invalid'
}
if not validate_password(password):
return {
'password': 'Password is invalid, Should be at least 8 characters with \
upper and lower case letters, numbers and special characters'
}
return True