This commit is contained in:
Eugene Pankov
2021-06-25 21:55:40 +02:00
parent 663615fe06
commit 0689c984ff
24 changed files with 573 additions and 320 deletions

View File

@@ -3,12 +3,13 @@ from dataclasses import dataclass
from django.conf import settings
from django.contrib.auth import logout
from rest_framework import fields
from rest_framework.exceptions import PermissionDenied
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin, UpdateModelMixin
from rest_framework.views import APIView
from rest_framework.viewsets import GenericViewSet, ModelViewSet
from rest_framework.serializers import ModelSerializer, Field
from rest_framework.serializers import ModelSerializer
from rest_framework_dataclasses.serializers import DataclassSerializer
from .models import Config, User
@@ -64,21 +65,25 @@ class AppVersionViewSet(ListModelMixin, GenericViewSet):
class UserSerializer(ModelSerializer):
id = fields.IntegerField()
is_pro = fields.SerializerMethodField()
class Meta:
model = User
fields = ('id', 'username', 'active_config', 'custom_connection_gateway', 'custom_connection_gateway_token')
fields = ('id', 'username', 'active_config', 'custom_connection_gateway', 'custom_connection_gateway_token', 'is_pro')
read_only_fields = ('id', 'username')
def get_is_pro(self, obj):
return False
class UserViewSet(RetrieveModelMixin, GenericViewSet):
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
return None
raise PermissionDenied()
class LogoutView(APIView):

63
terminus/app/sponsors.py Normal file
View File

@@ -0,0 +1,63 @@
from django.conf import settings
from gql import Client, gql
from gql.transport.requests import RequestsHTTPTransport
GQL_ENDPOINT = 'https://api.github.com/graphql'
def get_sponsor_usernames():
client = Client(
transport=RequestsHTTPTransport(
url=GQL_ENDPOINT,
use_json=True,
headers={
'Authorization': f'Bearer {settings.GITHUB_TOKEN}',
}
)
)
result = []
after = None
while True:
params = 'first: 1'
if after:
params += f', after:"{after}"'
query = '''
query {
user (login: "eugeny") {
sponsorshipsAsMaintainer(%s, includePrivate: true) {
pageInfo {
startCursor
hasNextPage
endCursor
}
nodes {
createdAt
tier {
monthlyPriceInDollars
}
sponsor{
... on User {
login
}
}
}
}
}
}
''' % (params,)
response = client.execute(gql(query))
after = response['user']['sponsorshipsAsMaintainer']['pageInfo']['endCursor']
nodes = response['user']['sponsorshipsAsMaintainer']['nodes']
if not len(nodes):
break
for node in nodes:
if node['tier']['monthlyPriceInDollars'] >= settings.GITHUB_SPONSORS_MIN_PAYMENT:
result.append(node['sponsor']['login'])
return result

View File

@@ -12,7 +12,7 @@ router.register('api/1/versions', api.AppVersionViewSet, basename='app-versions'
urlpatterns = [
path('api/1/auth/logout', api.LogoutView.as_view()),
path('api/1/user', api.UserViewSet.as_view({'get': 'retrieve'})),
path('api/1/user', api.UserViewSet.as_view({'get': 'retrieve', 'put': 'update'})),
path('', views.IndexView.as_view()),
path('terminal', views.TerminalView.as_view()),

View File

@@ -136,6 +136,8 @@ AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_GITHUB_SCOPE = ['read:user', 'user:email']
LOGIN_REDIRECT_URL = '/'
APP_DIST_PATH = BASE_DIR / 'app-dist'
@@ -152,9 +154,19 @@ for key in [
'CONNECTION_GATEWAY_AUTH_CA',
'CONNECTION_GATEWAY_AUTH_CERTIFICATE',
'CONNECTION_GATEWAY_AUTH_KEY',
'GITHUB_SPONSORS_USER',
'GITHUB_SPONSORS_MIN_PAYMENT',
'GITHUB_TOKEN',
]:
globals()[key] = os.getenv(key)
for key in [
'GITHUB_SPONSORS_MIN_PAYMENT',
]:
globals()[key] = int(globals()[key]) if globals()[key] else None
for key in [
'CONNECTION_GATEWAY_AUTH_CA',
'CONNECTION_GATEWAY_AUTH_CERTIFICATE',