forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Slack Avatar integration (apache#27849)
- Loading branch information
1 parent
e38e1d8
commit 8736537
Showing
18 changed files
with
441 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
from flask_appbuilder.security.sqla.models import User | ||
|
||
from superset.daos.base import BaseDAO | ||
from superset.extensions import db | ||
from superset.models.user_attributes import UserAttribute | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class UserDAO(BaseDAO[User]): | ||
@staticmethod | ||
def get_by_id(user_id: int) -> User: | ||
return db.session.query(User).filter_by(id=user_id).one() | ||
|
||
@staticmethod | ||
def set_avatar_url(user: User, url: str) -> None: | ||
if user.extra_attributes: | ||
user.extra_attributes[0].avatar_url = url | ||
else: | ||
attrs = UserAttribute(avatar_url=url, user_id=user.id) | ||
user.extra_attributes = [attrs] | ||
db.session.add(attrs) | ||
db.session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
superset/migrations/versions/2024-04-01_22-44_c22cb5c2e546_user_attr_avatar_url.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# # Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""empty message | ||
Revision ID: c22cb5c2e546 | ||
Revises: be1b217cd8cd | ||
Create Date: 2024-04-01 22:44:40.386543 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "c22cb5c2e546" | ||
down_revision = "be1b217cd8cd" | ||
|
||
|
||
def upgrade(): | ||
op.add_column( | ||
"user_attribute", sa.Column("avatar_url", sa.String(length=100), nullable=True) | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_column("user_attribute", "avatar_url") |
38 changes: 38 additions & 0 deletions
38
superset/migrations/versions/2024-04-11_00-49_bbf146925528_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""empty message | ||
Revision ID: bbf146925528 | ||
Revises: ('678eefb4ab44', 'c22cb5c2e546') | ||
Create Date: 2024-04-11 00:49:51.592325 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "bbf146925528" | ||
down_revision = ("678eefb4ab44", "c22cb5c2e546") | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
def upgrade(): | ||
pass | ||
|
||
|
||
def downgrade(): | ||
pass |
38 changes: 38 additions & 0 deletions
38
superset/migrations/versions/2024-04-15_16-06_0dc386701747_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""empty message | ||
Revision ID: 0dc386701747 | ||
Revises: ('5ad7321c2169', 'bbf146925528') | ||
Create Date: 2024-04-15 16:06:29.946059 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "0dc386701747" | ||
down_revision = ("5ad7321c2169", "bbf146925528") | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
def upgrade(): | ||
pass | ||
|
||
|
||
def downgrade(): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
|
||
from flask import current_app | ||
from slack_sdk import WebClient | ||
|
||
|
||
class SlackClientError(Exception): | ||
pass | ||
|
||
|
||
def get_slack_client() -> WebClient: | ||
token: str = current_app.config["SLACK_API_TOKEN"] | ||
if callable(token): | ||
token = token() | ||
return WebClient(token=token, proxy=current_app.config["SLACK_PROXY"]) | ||
|
||
|
||
def get_user_avatar(email: str, client: WebClient = None) -> str: | ||
client = client or get_slack_client() | ||
try: | ||
response = client.users_lookupByEmail(email=email) | ||
except Exception as ex: | ||
raise SlackClientError(f"Failed to lookup user by email: {email}") from ex | ||
|
||
user = response.data.get("user") | ||
if user is None: | ||
raise SlackClientError("No user found with that email.") | ||
|
||
profile = user.get("profile") | ||
if profile is None: | ||
raise SlackClientError("User found but no profile available.") | ||
|
||
avatar_url = profile.get("image_192") | ||
if avatar_url is None: | ||
raise SlackClientError("Profile image is not available.") | ||
|
||
return avatar_url |
Oops, something went wrong.