Exemplo n.º 1
0
def login() -> ApiResponse:
    """Login a member"""
    member_json = request.get_json()
    email = member_json["email"]
    password = member_json["password"]

    member = MemberModel.find_by_email(email)

    if member and member.verify_password(password) and member.is_active:
        identity = member_schema.dump(member)
        access_token = create_access_token(identity=identity, fresh=True)
        _refresh_token = create_refresh_token(identity=identity)
        add_token_to_database([access_token, _refresh_token], member.id)

        return (
            jsonify(
                {
                    "access_token": access_token,
                    "refresh_token": _refresh_token,
                    "member": identity,
                }, ),
            200,
        )

    abort(401, description=MEMBER_401)
    def test_find_by_email(self):
        with self.app_context:
            member_1, _ = self.add_member_to_db(self.member_1, self.role_1)

            member = MemberModel.find_by_email(self.EMAIL)

            self.assertEqual(member.id, member_1.id)
Exemplo n.º 3
0
def put_member(member_id: int) -> ApiResponse:
    member = MemberModel.find_by_id(member_id)
    identity = get_jwt_identity()

    if member and identity["id"] != member.id:
        abort(
            401,
            description=ERROR_401,
        )

    if not member:
        abort(
            404,
            description=ERROR_404.format("Member", "id", member_id),
        )

    member_json = request.get_json()

    member_by_email = MemberModel.find_by_email(member_json.get("email"))

    if member_by_email and member_by_email.id != member_id:
        abort(
            409,
            description=ERROR_409.format(
                "Member",
                "email",
                member_json.get("email"),
            ),
        )

    member.email = member_json.get("email")
    member.mobile_phone = member_json.get("mobile_phone")
    member.first_name = member_json.get("first_name")
    member.last_name = member_json.get("last_name")
    member.linkedin_profile = member_json.get("linkedin_profile")
    member.github_profile = member_json.get("github_profile")
    member.twitter_profile = member_json.get("twitter_profile")
    member.profile_picture = member_json.get("profile_picture")
    member.save_to_db()

    return (
        jsonify({
            "message": MODIFIED.format("Member"),
            "member": member_schema.dump(member),
        }),
        200,
    )
Exemplo n.º 4
0
def post_member() -> ApiResponse:
    member_json = request.get_json()

    if MemberModel.find_by_email(member_json.get("email")):
        abort(
            409,
            description=ERROR_409.format(
                "Member",
                "email",
                member_json.get("email"),
            ),
        )

    member = member_schema.load(member_json)
    member.save_to_db()

    return (
        jsonify({
            "message": CREATED.format("Member"),
            "member": member_schema.dump(member),
        }),
        201,
    )