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 get_systems(): systems = System.objects if request.values.get("basic"): systems = systems.basic() systems = systems.order_by("sortName").values() return response.success({"systems": [system for system in systems]})
def list_roles(): args = request.args roles = Role.objects if args.get("filter"): roles = roles.filter(name__icontains=args.get("filter")) if not args.get("all") and not g.user.admin: roles = roles.filter(users__id=g.user.id) if not issubclass(type(roles), models.QuerySet): roles = roles.all() roles_list = [] for role in roles: roles_list.append({ "id": role.id, "name": role.name, "owner": { "id": role.owner.id, "username": role.owner.username, }, "member": bool(role.users.filter(id=g.user.id)), "admin": g.user.admin, }) role_admins = [ int(permission.permission.split("_")[2]) for permission in Permission.objects.filter( permission__startswith="role_admin_") ] for role in roles_list: if role["id"] in role_admins: role["admin"] = True return response.success({"roles": roles_list})
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 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 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 list_user_roles(user_id): user = User.objects.get(id=user_id) roles_list = [] for role in user.roles.all(): roles_list.append({ "id": role.id, "name": role.name, "owner": { "id": role.owner.id, "username": role.owner.username, }, "member": bool(role.users.filter(id=g.user.id)), "admin": g.user.admin, }) role_admins = [] for permission in user.permissions: if permission.startswith(ValidPermissions.ROLE_ADMIN.value[0]): role_admins.append(int(permission.split("_")[2])) for role in roles_list: if role["id"] in role_admins: role["admin"] = True return response.success({"roles": roles_list})
def update_forum(forum_id: int): try: forum: Forum = get_objects_by_id(forum_id, Forum, CacheKeys.FORUM_DETAILS.value) except Forum.DoesNotExist: return response.not_found() request_data = request.json invalid_values = {} if ( "forumType" in request_data and request_data["forumType"] not in Forum.ForumTypes.values ): invalid_values[ "forum_type" ] = f"forumTypes must be in [{Forum.ForumTypes.values}])" if "parent" in request_data: if not int(request_data["parent"]): invalid_values["parent"] = f"parent must be an integer" else: 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" request_data["parent"] = parent for key, value in request_data.items(): if key in ["title", "description", "forumType", "parent", "order"]: setattr(forum, key, value) forum.generate_heritage() forum.save() set_cache(CacheKeys.FORUM_DETAILS.value, {"id": forum.id}, forum) serialized_forum = ForumSerializer(forum) return response.success({"updated": True, "forum": serialized_forum.data})
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_forums(forum_id: int = 0): forum = get_objects_by_id(forum_id, Forum, CacheKeys.FORUM_DETAILS.value) serialized_forum = ForumSerializer(forum) return response.success(data={"forum": serialized_forum.data})
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()})
def get_referral_links(): links = ReferralLink.objects.order_by("order").values() return response.success({"referralLinks": [link for link in links]})