From f6fe673b283164af1018b896942d25645af591e5 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 2 Nov 2022 14:13:45 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=20applet=20publicati?= =?UTF-8?q?ons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/terminal/api/applet/applet.py | 11 +---------- apps/terminal/models/applet/applet.py | 5 ----- apps/terminal/serializers/applet.py | 5 ++--- 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/apps/terminal/api/applet/applet.py b/apps/terminal/api/applet/applet.py index 1ce665184..97e6fb103 100644 --- a/apps/terminal/api/applet/applet.py +++ b/apps/terminal/api/applet/applet.py @@ -98,16 +98,6 @@ class AppletViewSet(DownloadUploadMixin, viewsets.ModelViewSet): 'download': 'terminal.view_applet', } - def get_queryset(self): - queryset = super().get_queryset() - host = self.request.query_params.get('host') - - if not host: - return queryset.prefetch_related('publications') - else: - publications = AppletPublication.objects.filter(host_id=host) - return queryset.prefetch_related(Prefetch('publications', queryset=publications)) - def perform_destroy(self, instance): if not instance.name: raise ValidationError('Applet is not null') @@ -120,3 +110,4 @@ class AppletViewSet(DownloadUploadMixin, viewsets.ModelViewSet): class AppletPublicationViewSet(viewsets.ModelViewSet): queryset = AppletPublication.objects.all() serializer_class = serializers.AppletPublicationSerializer + filterset_fields = ['host', 'applet'] diff --git a/apps/terminal/models/applet/applet.py b/apps/terminal/models/applet/applet.py index 0d0b37dfc..d8e6c2fc6 100644 --- a/apps/terminal/models/applet/applet.py +++ b/apps/terminal/models/applet/applet.py @@ -54,10 +54,6 @@ class Applet(JMSBaseModel): return None return os.path.join(settings.MEDIA_URL, 'applets', self.name, 'icon.png') - @lazyproperty - def publication(self): - return self.publications.latest() - class AppletPublication(JMSBaseModel): applet = models.ForeignKey('Applet', on_delete=models.PROTECT, related_name='publications', verbose_name=_('Applet')) @@ -67,4 +63,3 @@ class AppletPublication(JMSBaseModel): class Meta: unique_together = ('applet', 'host') - get_latest_by = 'date_created' diff --git a/apps/terminal/serializers/applet.py b/apps/terminal/serializers/applet.py index efd337e29..c8c132e58 100644 --- a/apps/terminal/serializers/applet.py +++ b/apps/terminal/serializers/applet.py @@ -17,7 +17,7 @@ class AppletUploadSerializer(serializers.Serializer): class AppletPublicationSerializer(serializers.ModelSerializer): - applet = ObjectRelatedField(queryset=Applet.objects.all()) + applet = ObjectRelatedField(attrs=('id', 'display_name', 'icon'), queryset=Applet.objects.all()) host = ObjectRelatedField(queryset=AppletHost.objects.all()) status = LabeledChoiceField(choices=Status.choices, label=_("Status")) @@ -31,13 +31,12 @@ class AppletPublicationSerializer(serializers.ModelSerializer): class AppletSerializer(serializers.ModelSerializer): icon = serializers.ReadOnlyField(label=_("Icon")) type = LabeledChoiceField(choices=Applet.Type.choices, label=_("Type")) - publication = AppletPublicationSerializer(allow_null=True, label=_("Publication")) class Meta: model = Applet fields_mini = ['id', 'name', 'display_name'] read_only_fields = [ - 'publication', 'icon', 'date_created', 'date_updated', + 'icon', 'date_created', 'date_updated', ] fields = fields_mini + [ 'version', 'author', 'type', 'protocols',