From b159f165133d84c4df8e6a72bb72d86c0acd03d7 Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 1 Nov 2022 18:40:42 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E6=B7=BB=E5=8A=A0=20applet=20download?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/terminal/api/applet/applet.py | 12 ++++++++++++ .../automations/deploy_applet_host/playbook.yml | 4 ++-- apps/terminal/signal_handlers.py | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/apps/terminal/api/applet/applet.py b/apps/terminal/api/applet/applet.py index bce0738d6..54391cc48 100644 --- a/apps/terminal/api/applet/applet.py +++ b/apps/terminal/api/applet/applet.py @@ -5,6 +5,7 @@ import os.path from django.core.files.storage import default_storage from rest_framework import viewsets +from django.http import HttpResponse from rest_framework.decorators import action from rest_framework.response import Response from rest_framework.serializers import ValidationError @@ -22,6 +23,7 @@ class AppletViewSet(viewsets.ModelViewSet): serializer_class = serializers.AppletSerializer rbac_perms = { 'upload': 'terminal.add_applet', + 'download': 'terminal.view_applet', } def perform_destroy(self, instance): @@ -84,6 +86,16 @@ class AppletViewSet(viewsets.ModelViewSet): serializer.save() return Response(serializer.data, status=201) + @action(detail=True, methods=['get']) + def download(self, request, *args, **kwargs): + instance = super().get_object() + path = default_storage.path('applets/{}'.format(instance.name)) + zip_path = shutil.make_archive(path, 'zip', path) + with open(zip_path, 'rb') as f: + response = HttpResponse(f.read(), status=200, content_type='application/octet-stream') + response['Content-Disposition'] = 'attachment; filename*=UTF-8\'\'{}.zip'.format(instance.name) + return response + class AppletPublicationViewSet(viewsets.ModelViewSet): queryset = AppletPublication.objects.all() diff --git a/apps/terminal/automations/deploy_applet_host/playbook.yml b/apps/terminal/automations/deploy_applet_host/playbook.yml index 920546e33..0c115e91a 100644 --- a/apps/terminal/automations/deploy_applet_host/playbook.yml +++ b/apps/terminal/automations/deploy_applet_host/playbook.yml @@ -7,7 +7,7 @@ HOST_NAME: test CORE_HOST: https://demo.jumpserver.org BOOTSTRAP_TOKEN: PleaseChangeMe - RDS_Licensing: enabled + RDS_Licensing: true RDS_LicenseServer: 127.0.0.1 RDS_LicensingMode: 4 RDS_fSingleSessionPerUser: 1 @@ -20,7 +20,7 @@ name: RDS-Licensing state: present include_management_tools: yes - when: RDS_Licensing == "enabled" + when: RDS_Licensing - name: Install RDS-RD-Server (RDS) ansible.windows.win_feature: diff --git a/apps/terminal/signal_handlers.py b/apps/terminal/signal_handlers.py index ec51c5a2b..5c92fd2ab 100644 --- a/apps/terminal/signal_handlers.py +++ b/apps/terminal/signal_handlers.py @@ -1,2 +1,18 @@ # -*- coding: utf-8 -*- # + +from django.db.models.signals import post_save +from django.dispatch import receiver + + +from .models import Applet, AppletHost + + +@receiver(post_save, sender=AppletHost) +def on_applet_host_create(sender, instance, created=False, **kwargs): + pass + + +@receiver(post_save, sender=Applet) +def on_applet_create(sender, instance, created=False, **kwargs): + pass