mirror of
https://github.com/Eugeny/tabby-web.git
synced 2026-08-18 15:26:04 +01:00
wip
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import asyncio
|
||||
import random
|
||||
from tabby.app.consumers import GatewayAdminConnection
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import logout
|
||||
from dataclasses import dataclass
|
||||
@@ -14,8 +13,11 @@ from rest_framework.views import APIView
|
||||
from rest_framework.viewsets import GenericViewSet, ModelViewSet
|
||||
from rest_framework.serializers import ModelSerializer, Serializer
|
||||
from rest_framework_dataclasses.serializers import DataclassSerializer
|
||||
from social_django.models import UserSocialAuth
|
||||
from typing import List
|
||||
|
||||
from .consumers import GatewayAdminConnection
|
||||
from .sponsors import get_sponsor_usernames
|
||||
from .models import Config, Gateway, User
|
||||
|
||||
|
||||
@@ -98,6 +100,7 @@ class AppVersionViewSet(ListModelMixin, GenericViewSet):
|
||||
class UserSerializer(ModelSerializer):
|
||||
id = fields.IntegerField()
|
||||
is_pro = fields.SerializerMethodField()
|
||||
github_username = fields.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
@@ -108,11 +111,22 @@ class UserSerializer(ModelSerializer):
|
||||
'custom_connection_gateway',
|
||||
'custom_connection_gateway_token',
|
||||
'is_pro',
|
||||
'github_username',
|
||||
)
|
||||
read_only_fields = ('id', 'username')
|
||||
|
||||
def get_is_pro(self, obj):
|
||||
return False
|
||||
username = self.get_github_username(obj)
|
||||
if not username:
|
||||
return False
|
||||
return username in get_sponsor_usernames()
|
||||
|
||||
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):
|
||||
|
||||
@@ -16,8 +16,8 @@ class Config(models.Model):
|
||||
class User(AbstractUser):
|
||||
active_config = models.ForeignKey(Config, null=True, on_delete=models.SET_NULL, related_name='+')
|
||||
active_version = models.CharField(max_length=32, null=True)
|
||||
custom_connection_gateway = models.CharField(max_length=255, null=True)
|
||||
custom_connection_gateway_token = models.CharField(max_length=255, null=True)
|
||||
custom_connection_gateway = models.CharField(max_length=255, null=True, blank=True)
|
||||
custom_connection_gateway_token = models.CharField(max_length=255, null=True, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
modified_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from gql import Client, gql
|
||||
from gql.transport.requests import RequestsHTTPTransport
|
||||
|
||||
|
||||
GQL_ENDPOINT = 'https://api.github.com/graphql'
|
||||
CACHE_KEY = 'cached-sponsors'
|
||||
|
||||
|
||||
def get_sponsor_usernames():
|
||||
def fetch_sponsor_usernames():
|
||||
client = Client(
|
||||
transport=RequestsHTTPTransport(
|
||||
url=GQL_ENDPOINT,
|
||||
@@ -61,3 +63,9 @@ def get_sponsor_usernames():
|
||||
result.append(node['sponsor']['login'])
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def get_sponsor_usernames():
|
||||
if not cache.get(CACHE_KEY):
|
||||
cache.set(CACHE_KEY, fetch_sponsor_usernames(), timeout=30)
|
||||
return cache.get(CACHE_KEY)
|
||||
|
||||
Reference in New Issue
Block a user