from django.db import models
from django.core.exceptions import ValidationError

from pyuploadcare.dj import forms, UploadCare
from pyuploadcare.file import File


class FileField(models.Field):
    __metaclass__ = models.SubfieldBase

    description = "UploadCare file id/URI with cached data"

    def get_internal_type(self):
        return "TextField"

    def to_python(self, value):
        if not value:
            return None

        if isinstance(value, basestring):
            return UploadCare().file(value)

        if isinstance(value, File):
            return value

        raise ValidationError('Invalid value for a field')

    def get_prep_value(self, value):
        return value.serialize()

    def get_db_prep_save(self, value, connection=None):
        if value:
            value.store()
            return value.serialize()

    def value_to_string(self, obj):
        assert False

    def formfield(self, **kwargs):
        defaults = {'widget': forms.FileWidget, 'form_class': forms.FileField}
        defaults.update(kwargs)

        # yay for super!
        return super(FileField, self).formfield(**defaults)

from django.db import models
from django.core.exceptions import ValidationError

from pyuploadcare.dj import forms, UploadCare
from pyuploadcare.exceptions import InvalidRequestError
from pyuploadcare.file import File


class FileField(models.Field):
    __metaclass__ = models.SubfieldBase

    description = "UploadCare file id/URI with cached data"

    def get_internal_type(self):
        return "TextField"

    def to_python(self, value):
        if not value:
            return None

        if isinstance(value, File):
            return value

        if not isinstance(value, basestring):
            raise ValidationError(
                u'Invalid value for a field: string was expected'
            )

        try:
            return UploadCare().file(value)
        except InvalidRequestError as exc:
            raise ValidationError(
                u'Invalid value for a field: {exc}'.format(exc=exc)
            )

    def get_prep_value(self, value):
        return value.serialize()

    def get_db_prep_save(self, value, connection=None):
        if value:
            value.store()
            return value.serialize()

    def value_to_string(self, obj):
        assert False

    def formfield(self, **kwargs):
        defaults = {'widget': forms.FileWidget, 'form_class': forms.FileField}
        defaults.update(kwargs)

        # yay for super!
        return super(FileField, self).formfield(**defaults)

Add handling of InvalidRequestError in ``to_python`
Add handling of InvalidRequestError in ``to_python`

  from django.db import models
  from django.core.exceptions import ValidationError
  
  from pyuploadcare.dj import forms, UploadCare
+ from pyuploadcare.exceptions import InvalidRequestError
  from pyuploadcare.file import File
  
  
  class FileField(models.Field):
      __metaclass__ = models.SubfieldBase
  
      description = "UploadCare file id/URI with cached data"
  
      def get_internal_type(self):
          return "TextField"
  
      def to_python(self, value):
          if not value:
              return None
  
-         if isinstance(value, basestring):
-             return UploadCare().file(value)
- 
          if isinstance(value, File):
              return value
  
-         raise ValidationError('Invalid value for a field')
+         if not isinstance(value, basestring):
+             raise ValidationError(
+                 u'Invalid value for a field: string was expected'
+             )
+ 
+         try:
+             return UploadCare().file(value)
+         except InvalidRequestError as exc:
+             raise ValidationError(
+                 u'Invalid value for a field: {exc}'.format(exc=exc)
+             )
  
      def get_prep_value(self, value):
          return value.serialize()
  
      def get_db_prep_save(self, value, connection=None):
          if value:
              value.store()
              return value.serialize()
  
      def value_to_string(self, obj):
          assert False
  
      def formfield(self, **kwargs):
          defaults = {'widget': forms.FileWidget, 'form_class': forms.FileField}
          defaults.update(kwargs)
  
          # yay for super!
          return super(FileField, self).formfield(**defaults)
  
Add handling of InvalidRequestError in ``to_python`
## Code Before:
from django.db import models
from django.core.exceptions import ValidationError

from pyuploadcare.dj import forms, UploadCare
from pyuploadcare.file import File


class FileField(models.Field):
    __metaclass__ = models.SubfieldBase

    description = "UploadCare file id/URI with cached data"

    def get_internal_type(self):
        return "TextField"

    def to_python(self, value):
        if not value:
            return None

        if isinstance(value, basestring):
            return UploadCare().file(value)

        if isinstance(value, File):
            return value

        raise ValidationError('Invalid value for a field')

    def get_prep_value(self, value):
        return value.serialize()

    def get_db_prep_save(self, value, connection=None):
        if value:
            value.store()
            return value.serialize()

    def value_to_string(self, obj):
        assert False

    def formfield(self, **kwargs):
        defaults = {'widget': forms.FileWidget, 'form_class': forms.FileField}
        defaults.update(kwargs)

        # yay for super!
        return super(FileField, self).formfield(**defaults)

## Instruction:
Add handling of InvalidRequestError in ``to_python`
## Code After:
from django.db import models
from django.core.exceptions import ValidationError

from pyuploadcare.dj import forms, UploadCare
from pyuploadcare.exceptions import InvalidRequestError
from pyuploadcare.file import File


class FileField(models.Field):
    __metaclass__ = models.SubfieldBase

    description = "UploadCare file id/URI with cached data"

    def get_internal_type(self):
        return "TextField"

    def to_python(self, value):
        if not value:
            return None

        if isinstance(value, File):
            return value

        if not isinstance(value, basestring):
            raise ValidationError(
                u'Invalid value for a field: string was expected'
            )

        try:
            return UploadCare().file(value)
        except InvalidRequestError as exc:
            raise ValidationError(
                u'Invalid value for a field: {exc}'.format(exc=exc)
            )

    def get_prep_value(self, value):
        return value.serialize()

    def get_db_prep_save(self, value, connection=None):
        if value:
            value.store()
            return value.serialize()

    def value_to_string(self, obj):
        assert False

    def formfield(self, **kwargs):
        defaults = {'widget': forms.FileWidget, 'form_class': forms.FileField}
        defaults.update(kwargs)

        # yay for super!
        return super(FileField, self).formfield(**defaults)
