Files
tabby-web/backend/tabby/app/api/user.py

58 lines
1.7 KiB
Python
Raw Normal View History

2021-10-31 18:15:23 +01:00
from django.conf import settings
from rest_framework import fields
from rest_framework.exceptions import PermissionDenied
from rest_framework.mixins import RetrieveModelMixin, UpdateModelMixin
from rest_framework.viewsets import GenericViewSet
from rest_framework.serializers import ModelSerializer
from social_django.models import UserSocialAuth
from ..sponsors import check_is_sponsor_cached
from ..models import User
class UserSerializer(ModelSerializer):
id = fields.IntegerField()
is_pro = fields.SerializerMethodField()
is_sponsor = fields.SerializerMethodField()
github_username = fields.SerializerMethodField()
class Meta:
model = User
fields = (
2022-11-07 18:56:10 +01:00
"id",
"username",
"active_config",
"custom_connection_gateway",
"custom_connection_gateway_token",
"config_sync_token",
"is_pro",
"is_sponsor",
"github_username",
2021-10-31 18:15:23 +01:00
)
2022-11-07 18:56:10 +01:00
read_only_fields = ("id", "username")
2021-10-31 18:15:23 +01:00
def get_is_pro(self, obj):
2022-11-07 18:56:10 +01:00
return (
2022-12-26 18:49:25 +01:00
obj.force_pro or not settings.GITHUB_ELIGIBLE_SPONSORSHIPS or check_is_sponsor_cached(obj)
2022-11-07 18:56:10 +01:00
)
2021-10-31 18:15:23 +01:00
def get_is_sponsor(self, obj):
return check_is_sponsor_cached(obj)
def get_github_username(self, obj):
2022-11-07 18:56:10 +01:00
social_auth = UserSocialAuth.objects.filter(user=obj, provider="github").first()
2021-10-31 18:15:23 +01:00
if not social_auth:
return None
2022-11-07 18:56:10 +01:00
return social_auth.extra_data.get("login")
2021-10-31 18:15:23 +01:00
class UserViewSet(RetrieveModelMixin, UpdateModelMixin, GenericViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
def get_object(self):
if self.request.user.is_authenticated:
return self.request.user
raise PermissionDenied()