inventorymgr/registration.py
def handle_registration_request(token: str) -> Tuple[Dict[str, Any], int]:
    """Flask view that handles submits of the registration form."""
    missing_fields = []
    for field in ("username", "password", "repeat_password"):
        if not request.json.get(field):
            missing_fields.append(field)
    if missing_fields:
        return {"reason": "missing_fields", "missing": missing_fields}, 400

    username = request.json["username"]
    password = request.json["password"]
    repeat_password = request.json["repeat_password"]

    if password != repeat_password:
        return {"reason": "password_mismatch"}, 400

    token_obj = RegistrationToken.query.filter_by(token=token).first()
    if token_obj is None:
        return {"reason": "invalid_token"}, 400

    if token_obj.expires < _utcnow():
        db.session.delete(token_obj)
        db.session.commit()
        return {"reason": "expired_token"}, 400

    db.session.delete(token_obj)
    db.session.commit()

    try:
        db.session.add(
            User(username=username, password=generate_password_hash(password))
        )
        db.session.commit()
    except IntegrityError:
        db.session.rollback()

    return {"success": True}, 200
Flask view that handles submits of the registration form.
Flask view that handles submits of the registration form.
Flask view that handles submits of the registration form .
def handle_registration_request(token: str) -> Tuple[Dict[str, Any], int]:
    missing_fields = []
    for field in ("username", "password", "repeat_password"):
        if not request.json.get(field):
            missing_fields.append(field)
    if missing_fields:
        return {"reason": "missing_fields", "missing": missing_fields}, 400
    username = request.json["username"]
    password = request.json["password"]
    repeat_password = request.json["repeat_password"]
    if password != repeat_password:
        return {"reason": "password_mismatch"}, 400
    token_obj = RegistrationToken.query.filter_by(token=token).first()
    if token_obj is None:
        return {"reason": "invalid_token"}, 400
    if token_obj.expires < _utcnow():
        db.session.delete(token_obj)
        db.session.commit()
        return {"reason": "expired_token"}, 400
    db.session.delete(token_obj)
    db.session.commit()
    try:
        db.session.add(
            User(username=username, password=generate_password_hash(password))
        )
        db.session.commit()
    except IntegrityError:
        db.session.rollback()
    return {"success": True}, 200
def handle_registration_request ( token : str ) -> Tuple [ Dict [ str , Any ] , int ] : missing_fields = [ ] for field in ( "username" , "password" , "repeat_password" ) : if not request . json . get ( field ) : missing_fields . append ( field ) if missing_fields : return { "reason" : "missing_fields" , "missing" : missing_fields } , 400 username = request . json [ "username" ] password = request . json [ "password" ] repeat_password = request . json [ "repeat_password" ] if password != repeat_password : return { "reason" : "password_mismatch" } , 400 token_obj = RegistrationToken . query . filter_by ( token = token ) . first ( ) if token_obj is None : return { "reason" : "invalid_token" } , 400 if token_obj . expires < _utcnow ( ) : db . session . delete ( token_obj ) db . session . commit ( ) return { "reason" : "expired_token" } , 400 db . session . delete ( token_obj ) db . session . commit ( ) try : db . session . add ( User ( username = username , password = generate_password_hash ( password ) ) ) db . session . commit ( ) except IntegrityError : db . session . rollback ( ) return { "success" : True } , 200
Flask view that handles submits of the registration form.
Flask view that handles submits of the registration form .
"""Flask view that handles submits of the registration form."""
token
str