为model.imagefield重写Django for base64字符串的验证

时间:2015-06-19 07:00:15

标签: angularjs django override django-rest-framework serializer

我使用Angular和Bootstrap来提供表单。如果用户上传图像,Angular会在"数据中提供图像:"格式,但Django正在寻找一种文件类型。我通过覆盖perform_authentication(将图像修改为文件)和perform_create(注入我的user_id)来解决此问题。有更好的方式来覆盖?

我不想覆盖我的观点。我宁愿覆盖Django验证ImageFields的方式。我想要做的是检查传递的值是否为64位字符串,如果是,将其修改为文件类型,然后验证ImageField。以下代码按原样运行,我感觉不是最佳。

以下是我的观点:

class UserCredentialList(generics.ListCreateAPIView):
    permission_classes = (IsCredentialOwnerOrAdmin,)
    serializer_class = CredentialSerializer

    """
    This view should return a list of all the purchases
    for the currently authenticated user.
    """

    def get_queryset(self):
        """
        This view should return a list of all models by
        the maker passed in the URL
        """
        user = self.request.user
        return Credential.objects.filter(member=user)

    def perform_create(self, serializer):
        serializer.save(member_id=self.request.user.id)

    def perform_authentication(self, request):
        if request.method == 'POST':
            data = request.data.pop('document_image', None)

            from django.core.files.base import ContentFile
            import base64
            import six
            import uuid

            # Check if this is a base64 string
            if isinstance(data, six.string_types):
                # Check if the base64 string is in the "data:" format
                if 'data:' in data and ';base64,' in data:
                    # Break out the header from the base64 content
                    header, data = data.split(';base64,')

                # Try to decode the file. Return validation error if it fails.
                try:
                    decoded_file = base64.b64decode(data)
                except TypeError:
                    self.fail('invalid_image')

                # Generate file name:
                file_name = str(uuid.uuid4())[:12]  # 12 characters are more than enough.
                # Get the file name extension:
                import imghdr

                file_extension = imghdr.what(file_name, decoded_file)
                file_extension = "jpg" if file_extension == "jpeg" else file_extension

                complete_file_name = "%s.%s" % (file_name, file_extension,)

                data = ContentFile(decoded_file, name=complete_file_name)

                request.data['document_image'] = data
        request.user

这是我的序列化器:

class CredentialSerializer(serializers.ModelSerializer):
    class Meta:
        model = Credential
        fields = (
            'id',
            'credential_type',
            'credential_number',
            'date_received',
            'is_verified',
            'date_verified',
            'document_image',
        )

这是我的模特:

class Credential(models.Model):
    """Used to store various credentials for member validation."""
    document_image = models.ImageField(
        upload_to=get_upload_path(instance="instance",
                                  filename="filename.ext",
                                  path='images/credentials/'))

    PASSENGER = 'P'
    OWNER = 'O'
    CAPTAIN = 'C'
    CREDENTIAL_CHOICES = (
        (PASSENGER, 'Passenger'),
        (OWNER, 'Owner'),
        (CAPTAIN, 'Captain'),
    )

    credential_type = models.CharField(max_length=1,
                                       choices=CREDENTIAL_CHOICES,
                                       default=PASSENGER)
    credential_number = models.CharField(max_length=255)
    date_received = models.DateTimeField(auto_now_add=True)
    is_verified = models.BooleanField(default=False)
    date_verified = models.DateTimeField(blank=True, null=True)
    member = models.ForeignKey(settings.AUTH_USER_MODEL,
                                  related_name='credentials')

我使用以下链接来帮助我,现在我只是想弄清楚如何覆盖正确的方法 Django REST Framework upload image: "The submitted data was not a file"

自从我做了以后我做了一个更改:我已将此功能移到我的序列化程序中,而我现在覆盖了方法:is_valid并且也可以。至少它不在我看来了。

0 个答案:

没有答案