Browse Source

pref: 修改 applets api

pull/9009/head
ibuler 2 years ago
parent
commit
c8881d56ea
  1. 18
      apps/terminal/api/applet/applet.py
  2. 3
      apps/terminal/models/applet/applet.py
  3. 6
      apps/terminal/serializers/applet.py

18
apps/terminal/api/applet/applet.py

@ -5,9 +5,10 @@ from typing import Callable
import yaml import yaml
import os.path import os.path
from django.core.files.storage import default_storage
from rest_framework import viewsets from rest_framework import viewsets
from django.http import HttpResponse from django.http import HttpResponse
from django.core.files.storage import default_storage
from django.db.models import Prefetch
from rest_framework.decorators import action from rest_framework.decorators import action
from rest_framework.request import Request from rest_framework.request import Request
from rest_framework.response import Response from rest_framework.response import Response
@ -97,16 +98,15 @@ class AppletViewSet(DownloadUploadMixin, viewsets.ModelViewSet):
'download': 'terminal.view_applet', 'download': 'terminal.view_applet',
} }
def filter_queryset(self, queryset): def get_queryset(self):
queryset = list(super().filter_queryset(queryset)) queryset = super().get_queryset()
host = self.request.query_params.get('host') host = self.request.query_params.get('host')
if not host:
return queryset
publication_mapper = {p.applet: p for p in AppletPublication.objects.filter(host_id=host)} if not host:
for applet in queryset: return queryset.prefetch_related('publications')
applet.publication = publication_mapper.get(applet) else:
return queryset publications = AppletPublication.objects.filter(host_id=host)
return queryset.prefetch_related(Prefetch('publications', queryset=publications))
def perform_destroy(self, instance): def perform_destroy(self, instance):
if not instance.name: if not instance.name:

3
apps/terminal/models/applet/applet.py

@ -56,7 +56,7 @@ class Applet(JMSBaseModel):
@lazyproperty @lazyproperty
def publication(self): def publication(self):
return None return self.publications.latest()
class AppletPublication(JMSBaseModel): class AppletPublication(JMSBaseModel):
@ -67,3 +67,4 @@ class AppletPublication(JMSBaseModel):
class Meta: class Meta:
unique_together = ('applet', 'host') unique_together = ('applet', 'host')
get_latest_by = 'date_created'

6
apps/terminal/serializers/applet.py

@ -2,6 +2,7 @@ from rest_framework import serializers
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from common.drf.fields import ObjectRelatedField, LabeledChoiceField from common.drf.fields import ObjectRelatedField, LabeledChoiceField
from common.const.choices import Status
from ..models import Applet, AppletPublication, AppletHost from ..models import Applet, AppletPublication, AppletHost
@ -18,14 +19,13 @@ class AppletUploadSerializer(serializers.Serializer):
class AppletPublicationSerializer(serializers.ModelSerializer): class AppletPublicationSerializer(serializers.ModelSerializer):
applet = ObjectRelatedField(queryset=Applet.objects.all()) applet = ObjectRelatedField(queryset=Applet.objects.all())
host = ObjectRelatedField(queryset=AppletHost.objects.all()) host = ObjectRelatedField(queryset=AppletHost.objects.all())
status = LabeledChoiceField(choices=Status.choices, label=_("Status"))
class Meta: class Meta:
model = AppletPublication model = AppletPublication
fields_mini = ['id', 'applet', 'host'] fields_mini = ['id', 'applet', 'host']
read_only_fields = ['date_created', 'date_updated'] read_only_fields = ['date_created', 'date_updated']
fields = fields_mini + [ fields = fields_mini + ['status', 'comment'] + read_only_fields
'status', 'comment',
] + read_only_fields
class AppletSerializer(serializers.ModelSerializer): class AppletSerializer(serializers.ModelSerializer):

Loading…
Cancel
Save