Ejemplo n.º 1
0
    def create(self, request, *args, **kwargs):
        serializer = ImageNewSerializer(data=request.data)
        if serializer.is_valid():
            form_url = serializer.validated_data.get('form_url', None)
            form_file = serializer.validated_data.get('form_file', None)
            image_instance = Image()
            if form_url:
                file_name = os.path.basename(
                    urllib.parse.urlparse(form_url).path)
                req = urllib.request.Request(form_url)
                try:
                    response = urllib.request.urlopen(req)
                except urllib.error.HTTPError:
                    return Response(status=status.HTTP_400_BAD_REQUEST,
                                    data={"form_url": "error"})
                django_file = ContentFile(BytesIO(response.read()).getvalue())
                image_instance.file.save(file_name, django_file)

            elif form_file:
                image_instance.file = form_file
            image_instance.save()
            return Response(data=ImageSerializer(image_instance).data,
                            status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
    def create(self, request, *args, **kwargs):

        image = Image()
        image.title = request.data['title']
        image.format = request.data['format']
        image.file = request.data['file']
        image.date_created = timezone.now()
        image.date_updated = timezone.now()
        #return super().create(request, *args, **kwargs)
        image.save()
        return Response({"message": "Image entry successful"},
                        status=status.HTTP_201_CREATED)