class WritableCommentSerializer(serializers.ModelSerializer): document = DocumentSerializer(read_only=True) document_comments_url = serializers.SerializerMethodField() url = serializers.SerializerMethodField() user = UserSerializer(read_only=True) class Meta: fields = ( 'comment', 'document', 'document_comments_url', 'id', 'submit_date', 'url', 'user' ) model = Comment read_only_fields = ('document',) def create(self, validated_data): validated_data['document'] = self.context['document'] validated_data['user'] = self.context['request'].user return super(WritableCommentSerializer, self).create(validated_data) def get_document_comments_url(self, instance): return reverse( 'rest_api:comment-list', args=( instance.document.pk, ), request=self.context['request'], format=self.context['format'] ) def get_url(self, instance): return reverse( 'rest_api:comment-detail', args=( instance.document.pk, instance.pk ), request=self.context['request'], format=self.context['format'] )
class DocumentMetadataSerializer(serializers.HyperlinkedModelSerializer): document = DocumentSerializer(read_only=True) metadata_type = MetadataTypeSerializer(read_only=True) url = serializers.SerializerMethodField() class Meta: fields = ('document', 'id', 'metadata_type', 'url', 'value') model = DocumentMetadata read_only_fields = ('document', 'metadata_type',) def get_url(self, instance): return reverse( 'rest_api:documentmetadata-detail', args=( instance.document.pk, instance.pk ), request=self.context['request'], format=self.context['format'] ) def validate(self, attrs): self.instance.value = attrs['value'] try: self.instance.full_clean() except DjangoValidationError as exception: raise ValidationError(exception) return attrs
def patch(self, request: Request, document_id: int, *args, **kwargs): serializer = ChangeDocumentSerializer(data=request.data) serializer.is_valid(raise_exception=True) if not has_permission(request.headers, PermissionSet.DocumentEdit.value): return Response( data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) file = request.FILES.get('file') document = Document.objects.filter(id=document_id).first() if not document: return Response(data={'detail': 'Document not found'}, status=status.HTTP_404_NOT_FOUND) document.name = serializer.validated_data.get('name', document.name) document.type = serializer.validated_data.get('type', document.type) if file: document.file.delete() document.file = file document.save() return Response(data=DocumentSerializer(document, context={ 'request': request }).data, status=status.HTTP_200_OK)
class CommentSerializer(serializers.HyperlinkedModelSerializer): document = DocumentSerializer(read_only=True) document_comments_url = serializers.SerializerMethodField() url = serializers.SerializerMethodField() user = UserSerializer(read_only=True) class Meta: fields = ( 'comment', 'document', 'document_comments_url', 'id', 'submit_date', 'url', 'user' ) model = Comment def get_document_comments_url(self, instance): return reverse( 'rest_api:comment-list', args=( instance.document.pk, ), request=self.context['request'], format=self.context['format'] ) def get_url(self, instance): return reverse( 'rest_api:comment-detail', args=( instance.document.pk, instance.pk ), request=self.context['request'], format=self.context['format'] )
class DocumentListView(APIView): @swagger_auto_schema(request_body=AddDocumentSerializer, responses={'201': DocumentSerializer}) def post(self, request: Request, *args, **kwargs): serializer = AddDocumentSerializer(data=request.data) serializer.is_valid(raise_exception=True) if not has_permission(request.headers, PermissionSet.DocumentEdit.value): return Response( data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) if not (file := request.FILES.get('file')): return Response(data={'detail': 'No file received'}, status=status.HTTP_400_BAD_REQUEST) try: document = Document.objects.create( name=serializer.validated_data.get('name'), type=serializer.validated_data.get('type'), file=file) except IntegrityError: return Response(data={'detail': 'Wrong document info'}, status=status.HTTP_400_BAD_REQUEST) return Response(data=DocumentSerializer(document, context={ 'request': request }).data, status=status.HTTP_201_CREATED)
class CommunicationHistoryDetailSerializer(CommunicationHistorySerializer): from documents.serializers import DocumentSerializer from projects.serializers import ProjectSerializer author = CRMUserTinySerializer() contact = ContactPersonDetailSerializer() task = ProjectTaskSerializer() project = ProjectSerializer() document = DocumentSerializer()
class GroupSerializer(serializers.HyperlinkedModelSerializer): document_set = DocumentSerializer(many=True) thread_set = SmallThreadSerializer(many=True) class Meta: model = Group fields = ('id', 'name', 'slug', 'url', 'categories', 'document_set', 'thread_set') extra_kwargs = {'url': {'lookup_field': 'slug'}}
def get(self, request: Request, document_id: int, *args, **kwargs): document = Document.objects.filter(id=document_id).first() if not document: return Response(data={'detail': 'Document not found'}, status=status.HTTP_404_NOT_FOUND) return Response( data=DocumentSerializer(document, context={ 'request': request }).data)
class AnnouncementSerializer(serializers.ModelSerializer): documents = DocumentSerializer(many=True, read_only=True) class Meta: model = Announcement fields = ( "id", "text", "starts", "expires", "event", "documents", "title", 'visibility', )
class CourseSerializer(serializers.HyperlinkedModelSerializer): document_set = DocumentSerializer(many=True) class Meta: model = Course fields = ( "id", "name", "slug", "url", "categories", "document_set", "followers_count", "description", ) extra_kwargs = {"url": {"lookup_field": "slug"}}
class AnnouncementListSerializer(serializers.ModelSerializer): document = DocumentSerializer(read_only=True) external_url = serializers.CharField(required=False, allow_blank=True) external_name = serializers.CharField(required=False, allow_blank=True) class Meta: model = Announcement fields = ( "id", "text", "external_url", "external_name", "document", "title", "starts", "expires", )
class CourseSerializer(serializers.HyperlinkedModelSerializer): document_set = DocumentSerializer(many=True) thread_set = SmallThreadSerializer(many=True) class Meta: model = Course fields = ( 'id', 'name', 'slug', 'url', 'categories', 'document_set', 'thread_set', 'gehol_url', 'followers_count', 'description', ) extra_kwargs = {'url': {'lookup_field': 'slug'}}
class DocumentCheckoutSerializer(serializers.ModelSerializer): document = DocumentSerializer() class Meta: fields = ('document', ) model = DocumentCheckout
def __init__(self, *args, **kwargs): # Hide this import otherwise strange circular import error occur from documents.serializers import DocumentSerializer super(DocumentCheckoutSerializer, self).__init__(*args, **kwargs) self.fields['document'] = DocumentSerializer()
def __init__(self, *args, **kwargs): super(DocumentCheckoutSerializer, self).__init__(*args, **kwargs) self.fields['document'] = DocumentSerializer()
class PropertySerializer(serializers.ModelSerializer): documents = DocumentSerializer(many=True) class Meta: model = Property exclude = ['user', 'local_node']