samedi 27 février 2021

Python - User registration class?

I am working on a simple CRUD app as a personal project using Flask. I am currently working on the user blueprint and trying to use as less as libraries as I could (that's why I am using FlaskWTF for example). However, I am wondering if my RegisterForm make sense or I should not have to create a specific class for userRegistration?

user/views.py

user = Blueprint('user', __name__, template_folder='templates')

@user.route('/register')
def login():
    return render_template('register.html')

@user.route('/new-user',methods = ['POST'])
def register_user():
    form = RegisterForm(request.form.to_dict())
    if not form.validate_new_user:
        #ask to user to fix the issues
    return render_template('')

user/forms.py

class RegisterForm():
    def __init__(self, form_data):
        self.email = form_data.get("email")
        self.password = form_data.get("psw")
        self.password_two = form_data.get("psw-repeat-two")
        print(form_data)
        
    def is_new_user():
        #if self.email already in the database.users then return False
        return True 
    
    def both_password_match():
        if password != password_two:
            return False 
        return True
        
    def is_password_strong_enough():
        if len(self.password) < 8:
            return False
        elif re.search('[0-9]',self.password) is None:
            return False
        elif re.search('[A-Z]',self.password) is None: 
            return False
        return True
                   
    def validate_new_user():
        if self.is_new_user and self.both_password_match and self.is_password_strong_enough:
            ## register new user 
        

class LoginForm():
    ...

Aucun commentaire:

Enregistrer un commentaire