-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Our current Avatars are a little dry, using a limited variety of colors and showing the user's initials. The app could use a little more personality. Now given we already have a slack integration and administrators can configure their `SLACK_API_TOKEN`, I was thinking we can use this integration to get Slack's avatars integrated in Superset. Given the popularity of Slack and users familiarity with the avatars there, I thought it'd be neat to bring a bit more color in Superset. The approach here is centered around a new endpoint `/api/v1/user/{id}/avatar.png`, which fetches from a new column in UserAttribute (our one-to-one extension to FAB's User model) for avatar_url, and redirects to the destination. When loading for the first time, if information is empty and Slack is configured, we ask Slack for the URL, which is secret, but serves the image from outside the authentication part of their API.
- Loading branch information
1 parent
8afe973
commit 9a412fc
Showing
17 changed files
with
409 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 |
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,59 @@ | ||
# 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() | ||
print("HEREHREHRHEHRHR") | ||
print("HEREHREHRHEHRHR") | ||
print("HEREHREHRHEHRHR") | ||
print("HEREHREHRHEHRHR") | ||
print("HEREHREHRHEHRHR") | ||
print("HEREHREHRHEHRHR") | ||
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.