This commit is contained in:
Eugene Pankov
2022-11-07 18:56:10 +01:00
parent 05b476aa23
commit 99264d2bfc
24 changed files with 476 additions and 313 deletions

View File

@@ -4,14 +4,17 @@ from . import app_version, auth, config, gateway, user
router = routers.DefaultRouter(trailing_slash=False)
router.register('api/1/configs', config.ConfigViewSet)
router.register('api/1/versions', app_version.AppVersionViewSet, basename='app-versions')
router.register("api/1/configs", config.ConfigViewSet)
router.register(
"api/1/versions", app_version.AppVersionViewSet, basename="app-versions"
)
urlpatterns = [
path('api/1/auth/logout', auth.LogoutView.as_view()),
path('api/1/user', user.UserViewSet.as_view({'get': 'retrieve', 'put': 'update'})),
path('api/1/gateways/choose', gateway.ChooseGatewayViewSet.as_view({'post': 'retrieve'})),
path('', include(router.urls)),
path("api/1/auth/logout", auth.LogoutView.as_view()),
path("api/1/user", user.UserViewSet.as_view({"get": "retrieve", "put": "update"})),
path(
"api/1/gateways/choose",
gateway.ChooseGatewayViewSet.as_view({"post": "retrieve"}),
),
path("", include(router.urls)),
]

View File

@@ -25,27 +25,28 @@ class AppVersionSerializer(DataclassSerializer):
class AppVersionViewSet(ListModelMixin, GenericViewSet):
serializer_class = AppVersionSerializer
lookup_field = 'id'
lookup_value_regex = r'[\w\d.-]+'
queryset = ''
lookup_field = "id"
lookup_value_regex = r"[\w\d.-]+"
queryset = ""
def _get_versions(self):
fs = fsspec.filesystem(urlparse(settings.APP_DIST_STORAGE).scheme)
return [
self._get_version(x['name'])
self._get_version(x["name"])
for x in fs.listdir(settings.APP_DIST_STORAGE)
if x['type'] == 'directory'
if x["type"] == "directory"
]
def _get_version(self, dir):
fs = fsspec.filesystem(urlparse(settings.APP_DIST_STORAGE).scheme)
plugins = [
os.path.basename(x['name'])
os.path.basename(x["name"])
for x in fs.listdir(dir)
if x['type'] == 'directory' and os.path.basename(x['name'])
if x["type"] == "directory"
and os.path.basename(x["name"])
not in [
'tabby-web-container',
'tabby-web-demo',
"tabby-web-container",
"tabby-web-demo",
]
]

View File

@@ -10,8 +10,8 @@ class ConfigSerializer(ModelSerializer):
class Meta:
model = Config
read_only_fields = ('user', 'created_at', 'modified_at')
fields = '__all__'
read_only_fields = ("user", "created_at", "modified_at")
fields = "__all__"
class ConfigViewSet(ModelViewSet):

View File

@@ -14,7 +14,7 @@ class GatewaySerializer(ModelSerializer):
auth_token = fields.CharField()
class Meta:
fields = '__all__'
fields = "__all__"
model = Gateway
def get_url(self, gw):
@@ -23,8 +23,8 @@ class GatewaySerializer(ModelSerializer):
class NoGatewaysError(APIException):
status_code = status.HTTP_503_SERVICE_UNAVAILABLE
default_detail = 'No connection gateways available.'
default_code = 'no_gateways'
default_detail = "No connection gateways available."
default_code = "no_gateways"
class ChooseGatewayViewSet(RetrieveModelMixin, GenericViewSet):

View File

@@ -19,30 +19,34 @@ class UserSerializer(ModelSerializer):
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',
"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')
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)
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()
social_auth = UserSocialAuth.objects.filter(user=obj, provider="github").first()
if not social_auth:
return None
return social_auth.extra_data.get('login')
return social_auth.extra_data.get("login")
class UserViewSet(RetrieveModelMixin, UpdateModelMixin, GenericViewSet):