Beispiel #1
0
class DocumentAuxiliarySerializer(serializers.ModelSerializer):
    """
    Minimal Serializer for Documents as a property of CreditTrade
    """

    status = DocumentStatusSerializer(read_only=True)
    type = DocumentTypeSerializer(read_only=True)
    milestone = serializers.SerializerMethodField()

    def get_milestone(self, obj):
        """
        Additional information for milestone evidences
        """
        if obj.type.the_type == 'Evidence':
            milestone = obj.milestone
            serializer = DocumentMilestoneSerializer(milestone)

            return serializer.data

        return None

    class Meta:
        model = Document
        fields = ('id', 'title', 'status', 'type', 'milestone')
        read_only_fields = ('id', 'title', 'status', 'type', 'milestone')
Beispiel #2
0
class DocumentMinSerializer(serializers.ModelSerializer):
    """
    Minimal Serializer for Documents
    """
    attachments = DocumentFileAttachmentSerializer(many=True)
    create_user = UserMinSerializer(read_only=True)
    status = DocumentStatusSerializer(read_only=True)
    type = DocumentTypeSerializer(read_only=True)
    milestone = serializers.SerializerMethodField()
    credit_trades = CreditTradeAuxiliarySerializer(many=True, read_only=True)
    history = DocumentHistorySerializer(many=True, read_only=True)

    def get_milestone(self, obj):
        """
        Additional information for milestone evidences
        """
        if obj.type.the_type == 'Evidence':
            milestone = obj.milestone
            serializer = DocumentMilestoneSerializer(milestone)

            return serializer.data

        return None

    class Meta:
        model = Document
        fields = ('id', 'title', 'create_user', 'status', 'type', 'milestone',
                  'credit_trades', 'attachments', 'update_timestamp',
                  'history')
        read_only_fields = ('id', 'title', 'create_user', 'status', 'type',
                            'milestone', 'credit_trades', 'attachments',
                            'update_timestamp', 'history')
Beispiel #3
0
class DocumentAuxiliarySerializer(serializers.ModelSerializer):
    """
    Minimal Serializer for Documents as a property of CreditTrade
    """

    status = DocumentStatusSerializer(read_only=True)
    type = DocumentTypeSerializer(read_only=True)

    class Meta:
        model = Document
        fields = ('id', 'title', 'status', 'type', 'milestone')
        read_only_fields = ('id', 'title', 'status', 'type', 'milestone')
Beispiel #4
0
class DocumentSerializer(serializers.ModelSerializer):
    """
    Default Serializer for Documents
    """
    type = DocumentTypeSerializer(read_only=True)
    status = DocumentStatusSerializer(read_only=True)
    credit_trades = CreditTradeAuxiliarySerializer(many=True, read_only=True)

    class Meta:
        model = Document
        fields = ('id', 'title', 'status', 'type', 'create_timestamp',
                  'create_user', 'milestone', 'update_timestamp',
                  'update_user', 'credit_trades')

        read_only_fields = ('id', 'title', 'status', 'type',
                            'create_timestamp', 'create_user', 'milestone',
                            'update_timestamp', 'update_user', 'credit_trades')
Beispiel #5
0
class DocumentMinSerializer(serializers.ModelSerializer):
    """
    Minimal Serializer for Documents
    """
    attachments = DocumentFileAttachmentSerializer(many=True)
    create_user = UserMinSerializer(read_only=True)
    status = DocumentStatusSerializer(read_only=True)
    type = DocumentTypeSerializer(read_only=True)
    credit_trades = CreditTradeAuxiliarySerializer(many=True, read_only=True)
    history = DocumentHistorySerializer(many=True, read_only=True)

    class Meta:
        model = Document
        fields = ('id', 'title', 'create_user', 'status', 'type', 'milestone',
                  'credit_trades', 'attachments', 'update_timestamp',
                  'history')
        read_only_fields = ('id', 'title', 'create_user', 'status', 'type',
                            'milestone', 'credit_trades', 'attachments',
                            'update_timestamp', 'history')
Beispiel #6
0
class DocumentDetailSerializer(serializers.ModelSerializer):
    """
    Document Serializer with Full Details
    """
    actions = serializers.SerializerMethodField()
    attachments = serializers.SerializerMethodField()
    comment_actions = serializers.SerializerMethodField()
    link_actions = serializers.SerializerMethodField()
    comments = serializers.SerializerMethodField()
    compliance_period = CompliancePeriodSerializer(read_only=True)
    status = DocumentStatusSerializer(read_only=True)
    type = DocumentTypeSerializer(read_only=True)
    credit_trades = serializers.SerializerMethodField()
    create_user = UserMinSerializer(read_only=True)

    def get_actions(self, obj):
        """
        If the user doesn't have any roles assigned, treat as though the user
        doesn't have available permissions
        """
        cur_status = obj.status.status
        request = self.context.get('request')

        # If the user doesn't have any roles assigned, treat as though the user
        # doesn't have available permissions
        if not request.user.roles:
            return []

        if cur_status == "Security Scan Failed":
            return DocumentActions.scan_failed(request)

        if cur_status == "Draft":
            return DocumentActions.draft(request)

        if cur_status == "Submitted" or cur_status == 'Pending Submission':
            return DocumentActions.submitted(request)

        if cur_status == "Received":
            return DocumentActions.received(request)

        return []

    def get_link_actions(self, obj):
        cur_status = obj.status.status
        request = self.context.get('request')

        # If the user doesn't have any roles assigned, treat as though the user
        # doesn't have available permissions
        if not request.user.roles:
            return []

        return DocumentActions.link_actions(request, cur_status)

    def get_attachments(self, obj):
        """
        Returns all file attachments for the document.
        We have to make sure not to include attachments that have been
        marked for removal.
        """
        attachments = DocumentFileAttachment.objects.filter(document_id=obj.id,
                                                            is_removed=False)

        serializer = DocumentFileAttachmentSerializer(attachments, many=True)

        return serializer.data

    def get_comments(self, obj):
        """
        Returns all the attached comments for the credit trade
        """
        request = self.context.get('request')

        # If the user doesn't have any roles assigned, treat as though the user
        # doesn't have available permissions
        if not request.user.roles:
            return []

        if request.user.is_government_user:
            comments = obj.comments
        else:
            comments = obj.unprivileged_comments

        serializer = DocumentCommentSerializer(comments,
                                               many=True,
                                               context={'request': request})

        return serializer.data

    def get_comment_actions(self, obj):
        """Attach available commenting actions"""
        request = self.context.get('request')
        return DocumentCommentActions.available_comment_actions(request, obj)

    def get_credit_trades(self, obj):
        """
        Returns the linked credit transactions to the file submission
        Will also filter based on the user type
        """
        request = self.context.get('request')
        credit_trades = obj.credit_trades

        if not request.user.is_government_user:
            credit_trades = credit_trades.filter(
                status__status__in=["Approved", "Declined"])

        serializer = CreditTradeAuxiliarySerializer(credit_trades,
                                                    many=True,
                                                    read_only=True)

        return serializer.data

    class Meta:
        model = Document
        fields = ('id', 'title', 'create_timestamp', 'create_user',
                  'update_timestamp', 'update_user', 'status', 'type',
                  'attachments', 'compliance_period', 'actions',
                  'comment_actions', 'comments', 'link_actions', 'milestone',
                  'credit_trades')

        read_only_fields = ('id', 'create_timestamp', 'create_user',
                            'update_timestamp', 'update_user', 'title',
                            'status', 'type', 'attachments',
                            'compliance_period', 'actions', 'comment_actions',
                            'milestone', 'link_actions', 'credit_trades')