def register(): errors = {} fields_missing = require_values(request.json, ["email", "username", "password"]) if len(fields_missing): errors["fields_missing"] = fields_missing email = request.json["email"] username = request.json["username"] password = request.json["password"] if password: pass_invalid = User.validate_password(password) if len(pass_invalid): errors["pass_errors"] = pass_invalid if len(errors): return response.errors(errors) try: users_functions.register_user(email=email, username=username, password=password) except UserExists as e: response.errors(e.errors) return response.success()
def reset_password(): fields_missing = require_values( request.json, ["email", "token", "password", "confirm_password"]) if len(fields_missing): return response.errors({"fields_missing": fields_missing}) password_reset = PasswordResetToken.validate_token( token=request.json.get("token"), email=request.json.get("email"), get_obj=True) if not password_reset: return response.errors({"invalid_token": True}) errors = {} password, confirm_password = ( request.json["password"], request.json["confirm_password"], ) if password != confirm_password: errors["password_mismatch"] = True pass_invalid = User.validate_password(password) if len(pass_invalid): errors["pass_errors"] = pass_invalid if errors: return response.errors(errors) user = password_reset.user user.set_password(password) user.save() password_reset.use() return response.success({})
def check_password_reset(): fields_missing = require_values(request.args, ["email", "token"]) if len(fields_missing): return response.errors({"fields_missing": fields_missing}) valid_token = PasswordResetToken.validate_token( token=request.args.get("token"), email=request.args.get("email")) return response.success({"valid_token": valid_token})
def create_forum(): request_data = request.json fields_missing = require_values(request_data, ["title", "forumType", "parent"]) if len(fields_missing): return response.errors({"fields_missing": fields_missing}) invalid_values = {} if request_data["forumType"] not in Forum.ForumTypes.values: invalid_values[ "forum_type" ] = f"forumTypes must be in [{Forum.ForumTypes.values}])" if not int(request_data["parent"]): invalid_values["parent"] = f"parent must be an integer" try: parent: Forum = get_objects_by_id( request_data["parent"], Forum, CacheKeys.FORUM_DETAILS.value ) except Forum.DoesNotExist: invalid_values["parent"] = f"parent \"{request_data['parent']}\" does not exist" game = None if "game_id" in request_data: game: Game = get_objects_by_id( request_data["game_id"], Game, CacheKeys.GAME_DETAILS.value ) if not game: invalid_values[ "game_id" ] = f"game_id \"{request_data['game_id']}\" does not exist" if invalid_values: return response.errors({"invalid_values": invalid_values}) forum_values = { "title": request_data["title"], "forumType": request_data["forumType"], "parent": parent, } if "description" in request_data: forum_values["description"] = request_data["description"] if game: forum_values["game"] = game forum = Forum(**forum_values) forum.save() forum.generate_heritage() forum.save() return response.success({"forum": {"id": forum.id, "title": forum.title}})
def login(): fields_missing = require_values(request.json, ["email", "password"]) if len(fields_missing): return response.errors({"fields_missing": fields_missing}) email = request.json["email"] try: user = User.objects.get(email=email) except User.DoesNotExist: user = None if user: password = request.json["password"] if user.check_pass(password): return response.success({ "logged_in": True, "jwt": user.generate_jwt(), "user": user.to_dict() }) return response.errors({"invalid_user": True})
def activate_user(token): try: account_activation_token = AccountActivationToken.objects.get( token=token) except AccountActivationToken.DoesNotExist: return response.errors({"invalid_token": True}) user = account_activation_token.user user.activate() account_activation_token.use() return response.success()
def generate_password_reset(): fields_missing = require_values(request.json, ["email"]) if len(fields_missing): return response.errors({"fields_missing": fields_missing}) email = request.json["email"] try: user = User.objects.get(email=email) except User.DoesNotExist: return response.errors({"no_account": True}) try: password_reset = PasswordResetToken.objects.get(user=user) except PasswordResetToken.DoesNotExist: password_reset = PasswordResetToken(user=user) password_reset.save() email_content = get_template( "authorization/templates/reset_password.html", reset_link="http://gamersplane.com/auth/resetPass/" + password_reset.token, ) send_email(email, "Password reset for Gamers' Plane", email_content) return response.success()
def get_user(id): try: user = User.objects.get(id=id) except User.DoesNotExist: return response.errors({"noUser": True}) return response.success({"user": user.to_dict()})