def confirm_account(token): """ Confirm account. """ try: data = decode_token(token) email = data["identity"] if not data["user_claims"].get("token_registration"): raise DecodeError user = User.query.filter_by(email=email).first() if user and not user.confirmed: reason = "Confirmed." status_code = 200 db_update(user, confirmed=True, confirmed_at=datetime.utcnow()) else: raise DecodeError except DecodeError: reason = "Invalid token." status_code = 403 return {"reason": reason, "status_code": status_code}
def password_reset(token, **kwds): """ Reset user password. """ try: data = decode_token(token) email = data["identity"] if not data["user_claims"].get("token_password_reset"): raise DecodeError user = User.query.filter_by(email=email).first() passw = kwds.get("password") if user and passw: passw = generate_password_hash(passw) db_update(user, password=passw) reason = "Successfully change password." status_code = 200 else: reason = "User not found." status_code = 404 except DecodeError: reason = "Invalid token." status_code = 401 except ExpiredSignatureError: reason = "Token expired." status_code = 403 return {"reason": reason, "status_code": status_code}
def confirm_account(token): """ Confirm account. """ try: data = decode_token(token) if not data["user_claims"].get("token_registration"): raise DecodeError uid = data["identity"] user = User.query.filter_by(id=uid).first() if user and not user.confirmed: message = "Confirmed." status_code = 200 db_update(user, confirmed=True, date_confirmed=datetime.utcnow()) on_confirmed_user.send(user) else: raise DecodeError except DecodeError: message = "Invalid token." status_code = 403 return {"message": message, "code": status_code}
def password_reset(kwds, token): """ Reset user password. """ try: data = decode_token(token) if not data["user_claims"].get("token_password_reset"): raise DecodeError uid = data["identity"] user = User.query.filter_by(id=uid).first() passw = kwds.get("password") if user and passw: passw = generate_password_hash(passw) log = Log(date_set_new_password=datetime.utcnow()) user.logs.append(log) db_update(user, password=passw) on_reset_password.send(user) message = "Successfully change password." status_code = 200 else: message = "User not found." status_code = 404 except DecodeError: message = "Invalid token." status_code = 401 except ExpiredSignatureError: message = "Token expired." status_code = 403 return {"message": message, "code": status_code}
def update(): """ Update data. """ json = request.get_json() new_data = json.pop("__update__", None) if new_data and isinstance(new_data, dict): model = ${name}.query.filter_by(**json).first() if not model: status_code = 404 reason = "Data tidak ditemukan." else: db_update(db, model, **new_data) status_code = 200 reason = "Data berhasil diperbaharui." else: status_code = 403 reason = "Data baru tidak ditemukan." return { "status_code": status_code, "reason": reason }