2022-06-03 08:09:47 -06:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
2022-11-29 09:41:56 +01:00
|
|
|
from apps.api.permissions import LegacyAccessControlRole
|
2022-06-03 08:09:47 -06:00
|
|
|
from apps.slack.models import SlackUserIdentity
|
|
|
|
|
from apps.user_management.models import User
|
|
|
|
|
from common.api_helpers.mixins import EagerLoadingMixin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SlackUserIdentitySerializer(serializers.ModelSerializer):
|
2024-02-01 17:16:57 -03:00
|
|
|
user_id: str = serializers.CharField(source="slack_id")
|
|
|
|
|
team_id: str = serializers.CharField(source="slack_team_identity.slack_id")
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SlackUserIdentity
|
|
|
|
|
fields = (
|
|
|
|
|
"user_id",
|
|
|
|
|
"team_id",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FastUserSerializer(serializers.ModelSerializer):
|
2024-02-01 17:16:57 -03:00
|
|
|
id: str = serializers.ReadOnlyField(read_only=True, source="public_primary_key")
|
2024-09-24 15:16:22 -04:00
|
|
|
grafana_id: int = serializers.IntegerField(read_only=True, source="user_id")
|
2024-02-01 17:16:57 -03:00
|
|
|
email: str = serializers.EmailField(read_only=True)
|
|
|
|
|
role: str = serializers.SerializerMethodField() # LEGACY, should be removed eventually
|
|
|
|
|
is_phone_number_verified: bool = serializers.SerializerMethodField()
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = User
|
2024-09-24 15:16:22 -04:00
|
|
|
fields = [
|
|
|
|
|
"id",
|
|
|
|
|
"grafana_id",
|
|
|
|
|
"email",
|
|
|
|
|
"username",
|
|
|
|
|
"role",
|
|
|
|
|
"is_phone_number_verified",
|
|
|
|
|
]
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
@staticmethod
|
2024-02-01 17:16:57 -03:00
|
|
|
def get_role(obj: User) -> str:
|
2022-11-29 09:41:56 +01:00
|
|
|
"""
|
|
|
|
|
LEGACY, should be removed eventually
|
|
|
|
|
"""
|
|
|
|
|
return LegacyAccessControlRole(obj.role).name.lower()
|
2022-06-03 08:09:47 -06:00
|
|
|
|
2024-02-01 17:16:57 -03:00
|
|
|
def get_is_phone_number_verified(self, obj: User) -> bool:
|
2022-06-03 08:09:47 -06:00
|
|
|
return obj.verified_phone_number is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer, EagerLoadingMixin):
|
2024-02-01 17:16:57 -03:00
|
|
|
id: str = serializers.ReadOnlyField(read_only=True, source="public_primary_key")
|
2024-09-24 15:16:22 -04:00
|
|
|
grafana_id: int = serializers.IntegerField(read_only=True, source="user_id")
|
2024-02-01 17:16:57 -03:00
|
|
|
email: str = serializers.EmailField(read_only=True)
|
|
|
|
|
slack: SlackUserIdentity = SlackUserIdentitySerializer(read_only=True, source="slack_user_identity")
|
|
|
|
|
role: str = serializers.SerializerMethodField() # LEGACY, should be removed eventually
|
|
|
|
|
is_phone_number_verified: bool = serializers.SerializerMethodField()
|
|
|
|
|
teams: list[str] = serializers.SlugRelatedField(read_only=True, many=True, slug_field="public_primary_key")
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
SELECT_RELATED = [
|
|
|
|
|
"slack_user_identity",
|
|
|
|
|
"slack_user_identity__slack_team_identity",
|
|
|
|
|
]
|
2024-02-01 17:16:57 -03:00
|
|
|
PREFETCH_RELATED = ["teams"]
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = User
|
2024-09-24 15:16:22 -04:00
|
|
|
fields = [
|
|
|
|
|
"id",
|
|
|
|
|
"grafana_id",
|
|
|
|
|
"email",
|
|
|
|
|
"slack",
|
|
|
|
|
"username",
|
|
|
|
|
"role",
|
|
|
|
|
"is_phone_number_verified",
|
|
|
|
|
"timezone",
|
|
|
|
|
"teams",
|
|
|
|
|
]
|
2023-11-10 22:17:11 +05:30
|
|
|
read_only_fields = ["timezone"]
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
@staticmethod
|
2024-02-01 17:16:57 -03:00
|
|
|
def get_role(obj: User) -> str:
|
2022-11-29 09:41:56 +01:00
|
|
|
"""
|
|
|
|
|
LEGACY, should be removed eventually
|
|
|
|
|
"""
|
|
|
|
|
return LegacyAccessControlRole(obj.role).name.lower()
|
2022-06-03 08:09:47 -06:00
|
|
|
|
2024-02-01 17:16:57 -03:00
|
|
|
def get_is_phone_number_verified(self, obj: User) -> bool:
|
2022-06-03 08:09:47 -06:00
|
|
|
return obj.verified_phone_number is not None
|