Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
some more api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Winkler committed Oct 21, 2020
1 parent f33dca2 commit e1ae068
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 52 deletions.
52 changes: 9 additions & 43 deletions src/documents/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
18 changes: 15 additions & 3 deletions src/documents/serialisers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,41 @@

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 = (
Expand All @@ -37,7 +48,8 @@ class Meta:
"name",
"colour",
"automatic_classification",
"is_inbox_tag"
"is_inbox_tag",
"document_count"
)


Expand Down
13 changes: 7 additions & 6 deletions src/documents/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -109,35 +110,35 @@ def form_invalid(self, form):

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,
Expand Down

0 comments on commit e1ae068

Please sign in to comment.