some more api changes

This commit is contained in:
Jonas Winkler 2020-10-21 12:53:14 +02:00
parent f33dca2904
commit e1ae068cda
3 changed files with 31 additions and 52 deletions

View File

@ -83,72 +83,38 @@ class CorrespondentAdmin(CommonAdmin):
list_display = (
"name",
"automatic_classification",
"document_count",
"last_correspondence"
"automatic_classification"
)
list_editable = ("automatic_classification",)
readonly_fields = ("slug",)
def get_queryset(self, request):
qs = super(CorrespondentAdmin, self).get_queryset(request)
qs = qs.annotate(
document_count=models.Count("documents"),
last_correspondence=models.Max("documents__created")
)
return qs
def document_count(self, obj):
return obj.document_count
document_count.admin_order_field = "document_count"
def last_correspondence(self, obj):
return obj.last_correspondence
last_correspondence.admin_order_field = "last_correspondence"
class TagAdmin(CommonAdmin):
list_display = (
"name",
"colour",
"automatic_classification",
"document_count")
"automatic_classification"
)
list_filter = ("colour",)
list_editable = ("colour", "automatic_classification")
readonly_fields = ("slug",)
class Media:
js = ("js/colours.js",)
def get_queryset(self, request):
qs = super(TagAdmin, self).get_queryset(request)
qs = qs.annotate(document_count=models.Count("documents"))
return qs
def document_count(self, obj):
return obj.document_count
document_count.admin_order_field = "document_count"
class DocumentTypeAdmin(CommonAdmin):
list_display = ("name", "automatic_classification", "document_count")
list_display = (
"name",
"automatic_classification"
)
list_editable = ("automatic_classification",)
readonly_fields = ("slug",)
def get_queryset(self, request):
qs = super(DocumentTypeAdmin, self).get_queryset(request)
qs = qs.annotate(document_count=models.Count("documents"))
return qs
def document_count(self, obj):
return obj.document_count
document_count.admin_order_field = "document_count"
class DocumentAdmin(CommonAdmin):

View File

@ -5,30 +5,41 @@ from .models import Correspondent, Tag, Document, Log, DocumentType
class CorrespondentSerializer(serializers.HyperlinkedModelSerializer):
document_count = serializers.IntegerField(read_only=True)
last_correspondence = serializers.DateTimeField(read_only=True)
class Meta:
model = Correspondent
fields = (
"id",
"slug",
"name",
"automatic_classification"
"automatic_classification",
"document_count",
"last_correspondence"
)
class DocumentTypeSerializer(serializers.HyperlinkedModelSerializer):
document_count = serializers.IntegerField(read_only=True)
class Meta:
model = DocumentType
fields = (
"id",
"slug",
"name",
"automatic_classification"
"automatic_classification",
"document_count"
)
class TagSerializer(serializers.HyperlinkedModelSerializer):
document_count = serializers.IntegerField(read_only=True)
class Meta:
model = Tag
fields = (
@ -37,7 +48,8 @@ class TagSerializer(serializers.HyperlinkedModelSerializer):
"name",
"colour",
"automatic_classification",
"is_inbox_tag"
"is_inbox_tag",
"document_count"
)

View File

@ -1,3 +1,4 @@
from django.db.models import Count, Max
from django.http import HttpResponse, HttpResponseBadRequest
from django.views.generic import DetailView, FormView, TemplateView
from django_filters.rest_framework import DjangoFilterBackend
@ -109,35 +110,35 @@ class PushView(SessionOrBasicAuthMixin, FormView):
class CorrespondentViewSet(ModelViewSet):
model = Correspondent
queryset = Correspondent.objects.all()
queryset = Correspondent.objects.annotate(document_count=Count('documents'), last_correspondence=Max('documents__created'))
serializer_class = CorrespondentSerializer
pagination_class = StandardPagination
permission_classes = (IsAuthenticated,)
filter_backends = (DjangoFilterBackend, OrderingFilter)
filter_class = CorrespondentFilterSet
ordering_fields = ("name", "slug")
ordering_fields = ("name", "document_count", "last_correspondence")
class TagViewSet(ModelViewSet):
model = Tag
queryset = Tag.objects.all()
queryset = Tag.objects.annotate(document_count=Count('documents'))
serializer_class = TagSerializer
pagination_class = StandardPagination
permission_classes = (IsAuthenticated,)
filter_backends = (DjangoFilterBackend, OrderingFilter)
filter_class = TagFilterSet
ordering_fields = ("name", "slug")
ordering_fields = ("name", "document_count")
class DocumentTypeViewSet(ModelViewSet):
model = DocumentType
queryset = DocumentType.objects.all()
queryset = DocumentType.objects.annotate(document_count=Count('documents'))
serializer_class = DocumentTypeSerializer
pagination_class = StandardPagination
permission_classes = (IsAuthenticated,)
filter_backends = (DjangoFilterBackend, OrderingFilter)
filter_class = DocumentTypeFilterSet
ordering_fields = ("name", "slug")
ordering_fields = ("name", "document_count")
class DocumentViewSet(RetrieveModelMixin,