Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom encoder/decoder classes #101

Open
ChillyBwoy opened this issue Sep 19, 2014 · 0 comments
Open

Custom encoder/decoder classes #101

ChillyBwoy opened this issue Sep 19, 2014 · 0 comments

Comments

@ChillyBwoy
Copy link

serialize_instance function from provider.utils uses default DjangoJSONEncoder, which cannot serilaize model with custom fields. It would be better if one can specify encoder/decoder classnames in settings module.

Example:

...
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill, ResizeCanvas, Anchor


class User(BaseUser):
    registered_at = models.DateTimeField(auto_now_add=True)
    tags = models.ManyToManyField('core.Tag', through='core.TagLike')
    avatar = models.ImageField(upload_to=upload_to('users'),
                               blank=True, null=True)
    avatar_square = ImageSpecField(source='avatar', format='JPEG',
                                   processors=[
                                   ResizeToFill(300, 300),
                                   ResizeCanvas(300, 300,
                                                anchor=Anchor.CENTER)],
                                   options={'quality': 75})
    avatar_square_s = ImageSpecField(source='avatar', format='JPEG',
                                     processors=[
                                     ResizeToFill(100, 100),
                                     ResizeCanvas(100, 100,
                                                  anchor=Anchor.CENTER)],
                                     options={'quality': 90})
...

Trace

Environment:


Request Method: POST
Request URL: http://localhost:8000/oauth2/authorize/confirm

Django Version: 1.6.7
Python Version: 2.7.8
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.staticfiles',
 'south',
 'imagekit',
 'django_babel',
 'social.apps.django_app.default',
 'pamphlet',
 'pamphlet_images',
 'pamphlet_tags',
 'pamphlet_grid',
 'pamphlet_auth',
 'pamphlet_admin',
 'itsmywine.apps.accounts',
 'itsmywine.apps.core',
 'itsmywine.apps.static_pages',
 'itsmywine.packages.text',
 'itsmywine.packages.images',
 'itsmywine.packages.pattern',
 'itsmywine.packages.custom',
 'itsmywine.packages.shop',
 'rest_framework',
 'provider',
 'provider.oauth2',
 'debug_toolbar')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "/usr/local/var/virtualenvs/itsmywine/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/var/virtualenvs/itsmywine/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/usr/local/var/virtualenvs/itsmywine/lib/python2.7/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/usr/local/var/virtualenvs/itsmywine/lib/python2.7/site-packages/provider/views.py" in dispatch
  41.         response = super(OAuthView, self).dispatch(request, *args, **kwargs)
File "/usr/local/var/virtualenvs/itsmywine/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  87.         return handler(request, *args, **kwargs)
File "/usr/local/var/virtualenvs/itsmywine/lib/python2.7/site-packages/provider/views.py" in post
  285.         return self.handle(request, request.POST)
File "/usr/local/var/virtualenvs/itsmywine/lib/python2.7/site-packages/provider/views.py" in handle
  277.         self.cache_data(request, client.serialize(), "client")
File "/usr/local/var/virtualenvs/itsmywine/lib/python2.7/site-packages/provider/oauth2/models.py" in serialize
  56.         return dict(user=serialize_instance(self.user),
File "/usr/local/var/virtualenvs/itsmywine/lib/python2.7/site-packages/provider/utils.py" in serialize_instance
  81.     return json.loads(json.dumps(ret, cls=DjangoJSONEncoder))
File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py" in dumps
  250.         sort_keys=sort_keys, **kw).encode(obj)
File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py" in encode
  207.         chunks = self.iterencode(o, _one_shot=True)
File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py" in iterencode
  270.         return _iterencode(o, 0)
File "/usr/local/var/virtualenvs/itsmywine/lib/python2.7/site-packages/django/core/serializers/json.py" in default
  104.             return super(DjangoJSONEncoder, self).default(o)
File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py" in default
  184.         raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /oauth2/authorize/confirm
Exception Value: <ImageFieldFile: uploads/users/1406301951.e25eee345552ad7736f1bae3f3c0819c.jpg> is not JSON serializable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant