more bulk edit

This commit is contained in:
jonaswinkler 2020-12-06 14:39:53 +01:00
parent 8699b6931c
commit 5369e0be03
3 changed files with 65 additions and 11 deletions

View File

@ -1,5 +1,4 @@
from documents.models import Document from documents.models import Document, Correspondent
methods_supported = [ methods_supported = [
"set_correspondent" "set_correspondent"
@ -36,4 +35,15 @@ def perform_bulk_edit(data):
def set_correspondent(ids, args): def set_correspondent(ids, args):
print("WOW") if not len(args) == 1:
raise ValueError()
if not args[0]:
correspondent = None
else:
if not isinstance(args[0], int):
raise ValueError()
correspondent = Correspondent.objects.get(args[0])
Document.objects.filter(id__in=ids).update(correspondent=correspondent)

View File

@ -113,6 +113,34 @@ class LogSerializer(serializers.ModelSerializer):
) )
class BulkEditSerializer(serializers.Serializer):
documents = serializers.PrimaryKeyRelatedField(
many=True,
label="Documents",
write_only=True,
queryset=Document.objects.all()
)
method = serializers.ChoiceField(
choices=[
"set_correspondent",
"set_document_type",
"add_tag",
"remove_tag",
"delete"
],
label="Method",
write_only=True,
)
parameters = serializers.DictField(allow_empty=True)
def validate(self, attrs):
return attrs
class PostDocumentSerializer(serializers.Serializer): class PostDocumentSerializer(serializers.Serializer):
document = serializers.FileField( document = serializers.FileField(

View File

@ -46,7 +46,8 @@ from .serialisers import (
LogSerializer, LogSerializer,
TagSerializer, TagSerializer,
DocumentTypeSerializer, DocumentTypeSerializer,
PostDocumentSerializer PostDocumentSerializer,
BulkEditSerializer
) )
@ -165,13 +166,6 @@ class DocumentViewSet(RetrieveModelMixin,
disposition, filename) disposition, filename)
return response return response
@action(methods=['post'], detail=False)
def bulk_edit(self, request, pk=None):
try:
return Response(perform_bulk_edit(request.data))
except Exception as e:
return HttpResponseBadRequest(str(e))
@action(methods=['get'], detail=True) @action(methods=['get'], detail=True)
def metadata(self, request, pk=None): def metadata(self, request, pk=None):
try: try:
@ -225,6 +219,28 @@ class LogViewSet(ReadOnlyModelViewSet):
ordering_fields = ("created",) ordering_fields = ("created",)
class BulkEditView(APIView):
permission_classes = (IsAuthenticated,)
serializer_class = BulkEditSerializer
parser_classes = (parsers.JSONParser,)
def get_serializer_context(self):
return {
'request': self.request,
'format': self.format_kwarg,
'view': self
}
def get_serializer(self, *args, **kwargs):
kwargs['context'] = self.get_serializer_context()
return self.serializer_class(*args, **kwargs)
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
class PostDocumentView(APIView): class PostDocumentView(APIView):
permission_classes = (IsAuthenticated,) permission_classes = (IsAuthenticated,)