1
0
mirror of https://github.com/Eugeny/tabby-web.git synced 2025-07-22 02:48:03 +00:00
Files
.github
backend
tabby
app
api
__init__.py
app_version.py
auth.py
config.py
gateway.py
info.py
user.py
management
migrations
__init__.py
admin.py
apps.py
gateway.py
models.py
sponsors.py
urls.py
views.py
__init__.py
middleware.py
settings.py
urls.py
wsgi.py
.dockerignore
.flake8
.gitignore
Dockerfile
cloudbuild.yaml
gunicorn.conf.py
manage.py
poetry.lock
pyproject.toml
start.sh
docs
frontend
.bumpversion.cfg
.editorconfig
.flake8
.gitignore
LICENSE
README.md
docker-compose.yml
tabby-web/backend/tabby/app/api/user.py
Eugene Pankov f677febac3 init
2021-10-31 18:15:23 +01:00

56 lines
1.7 KiB
Python

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 = (
'id',
'username',
'active_config',
'custom_connection_gateway',
'custom_connection_gateway_token',
'config_sync_token',
'is_pro',
'is_sponsor',
'github_username',
)
read_only_fields = ('id', 'username')
def get_is_pro(self, obj):
return obj.force_pro or not settings.GITHUB_ELIGIBLE_SPONSORSHIPS or check_is_sponsor_cached(obj)
def get_is_sponsor(self, obj):
return check_is_sponsor_cached(obj)
def get_github_username(self, obj):
social_auth = UserSocialAuth.objects.filter(user=obj, provider='github').first()
if not social_auth:
return None
return social_auth.extra_data.get('login')
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()