mirror of
https://github.com/Eugeny/tabby-web.git
synced 2026-08-18 07:16:03 +01:00
wip
This commit is contained in:
@@ -1,19 +1,66 @@
|
||||
# from rest_framework import fields
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import logout
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.viewsets import GenericViewSet, ModelViewSet
|
||||
from rest_framework.serializers import ModelSerializer
|
||||
from rest_framework_dataclasses.serializers import DataclassSerializer
|
||||
|
||||
from .models import Config
|
||||
|
||||
|
||||
@dataclass
|
||||
class AppVersion:
|
||||
version: str
|
||||
|
||||
|
||||
class AppVersionSerializer(DataclassSerializer):
|
||||
class Meta:
|
||||
dataclass = AppVersion
|
||||
|
||||
|
||||
class ConfigSerializer(ModelSerializer):
|
||||
class Meta:
|
||||
model = Config
|
||||
read_only_fields = ('user', 'created_at', 'modified_at')
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class ConfigViewSet(ModelViewSet):
|
||||
queryset = Config.objects.all()
|
||||
serializer_class = ConfigSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return Config.objects.filter(user=self.request.user)
|
||||
if self.request.user.is_authenticated:
|
||||
return Config.objects.filter(user=self.request.user)
|
||||
return Config.objects.none()
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(user=self.request.user)
|
||||
|
||||
|
||||
class AppVersionViewSet(ListModelMixin, GenericViewSet):
|
||||
serializer_class = AppVersionSerializer
|
||||
lookup_field = 'id'
|
||||
lookup_value_regex = r'[\w\d.-]+'
|
||||
queryset = ''
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def _get_versions(self):
|
||||
return [AppVersion(version=x) for x in os.listdir(settings.APP_DIST_PATH)]
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
return Response(self.serializer_class(
|
||||
self._get_versions(),
|
||||
many=True,
|
||||
).data)
|
||||
|
||||
|
||||
class LogoutView(APIView):
|
||||
def post(self, request, format=None):
|
||||
logout(request)
|
||||
|
||||
@@ -8,11 +8,14 @@ from . import views
|
||||
|
||||
router = routers.DefaultRouter(trailing_slash=False)
|
||||
router.register('api/1/configs', api.ConfigViewSet)
|
||||
router.register('api/1/versions', api.AppVersionViewSet, basename='app-versions')
|
||||
|
||||
urlpatterns = [
|
||||
path('api/1/auth/logout', api.LogoutView.as_view()),
|
||||
|
||||
path('', views.IndexView.as_view()),
|
||||
path('terminal', views.TerminalView.as_view()),
|
||||
path('app-dist/<path:path>', views.AppDistView.as_view()),
|
||||
path('app-dist/<version>/<path:path>', views.AppDistView.as_view()),
|
||||
path('build/<path:path>', views.BuildView.as_view()),
|
||||
path('', include(router.urls)),
|
||||
]
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import os
|
||||
from django.conf import settings
|
||||
from django.contrib.staticfiles.views import serve
|
||||
from django.http.response import HttpResponse
|
||||
from rest_framework.views import APIView
|
||||
from django.views import static
|
||||
from rest_framework.views import APIView
|
||||
|
||||
|
||||
class IndexView(APIView):
|
||||
@@ -18,8 +17,8 @@ class TerminalView(APIView):
|
||||
|
||||
|
||||
class AppDistView(APIView):
|
||||
def get(self, request, path=None, format=None):
|
||||
return static.serve(request, path, document_root=str(settings.BASE_DIR / 'app-dist'))
|
||||
def get(self, request, version=None, path=None, format=None):
|
||||
return static.serve(request, os.path.join(version, path), document_root=str(settings.APP_DIST_PATH))
|
||||
|
||||
|
||||
class BuildView(APIView):
|
||||
|
||||
@@ -133,3 +133,5 @@ CSRF_USE_SESSIONS = False
|
||||
CSRF_COOKIE_HTTPONLY = False
|
||||
CSRF_COOKIE_NAME = 'XSRF-TOKEN'
|
||||
CSRF_HEADER_NAME = 'HTTP_X_XSRF_TOKEN'
|
||||
|
||||
APP_DIST_PATH = BASE_DIR / 'app-dist'
|
||||
|
||||
Reference in New Issue
Block a user