[GH-ISSUE #884] KeyError exception when altering user via API #782

Closed
opened 2026-02-27 11:13:31 +03:00 by kerem · 3 comments
Owner

Originally created by @jficz on GitHub (Jun 2, 2016).
Original GitHub issue: https://github.com/modoboa/modoboa/issues/884

When I try to alter an account via API using API key of a SuperAdmin user (the id=1 user in fact), I get this error:

KeyError at /api/v1/accounts/2/
'role'
Request Method: PATCH
Request URL:    https://<my_domain>/api/v1/accounts/2/
Django Version: 1.9.6
Exception Type: KeyError
Exception Value:    
'role'
Exception Location: /srv/app/modoboa/env/local/lib/python2.7/site-packages/modoboa/admin/serializers.py in validate, line 189
Python Executable:  /usr/bin/uwsgi-core
Python Version: 2.7.9
Python Path:    
['.',
 '',
 '/srv/app/modoboa/env/lib/python2.7',
 '/srv/app/modoboa/env/lib/python2.7/plat-x86_64-linux-gnu',
 '/srv/app/modoboa/env/lib/python2.7/lib-tk',
 '/srv/app/modoboa/env/lib/python2.7/lib-old',
 '/srv/app/modoboa/env/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/srv/app/modoboa/env/local/lib/python2.7/site-packages']
Server time:    Thu, 2 Jun 2016 21:01:30 +0000

With this stack trace:

Environment:


Request Method: PATCH
Request URL: https://<my_domain>/api/v1/accounts/2/

Django Version: 1.9.6
Python Version: 2.7.9
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.sites',
 'django.contrib.staticfiles',
 'reversion',
 'rest_framework.authtoken',
 'rest_framework_swagger',
 'modoboa',
 'modoboa.core',
 'modoboa.lib',
 'modoboa.admin',
 'modoboa.relaydomains',
 'modoboa.limits',
 'modoboa_sievefilters',
 'modoboa_amavis',
 'modoboa_stats',
 'modoboa_postfix_autoreply',
 'modoboa_webmail')
Installed Middleware:
('x_forwarded_for.middleware.XForwardedForMiddleware',
 '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.locale.LocaleMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'modoboa.lib.middleware.AjaxLoginRedirect',
 'modoboa.lib.middleware.CommonExceptionCatcher',
 'modoboa.lib.middleware.RequestCatcherMiddleware')



Traceback:

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/django/utils/decorators.py" in inner
  184.                     return func(*args, **kwargs)

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/django/utils/decorators.py" in inner
  184.                     return func(*args, **kwargs)

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/viewsets.py" in view
  87.             return self.dispatch(request, *args, **kwargs)

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  466.             response = self.handle_exception(exc)

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  463.             response = handler(request, *args, **kwargs)

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/mixins.py" in partial_update
  78.         return self.update(request, *args, **kwargs)

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/mixins.py" in update
  69.         serializer.is_valid(raise_exception=True)

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/serializers.py" in is_valid
  213.                 self._validated_data = self.run_validation(self.initial_data)

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/serializers.py" in run_validation
  410.             value = self.validate(value)

File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/modoboa/admin/serializers.py" in validate
  189.         if data["role"] == "SimpleUsers":

Exception Type: KeyError at /api/v1/accounts/2/
Exception Value: 'role'

Debian Jessie latest, barebone. Modoba installed in a virutalenv using pip install method.

Curl command used to make the api call:
curl -ik -X POST -H "Authorization: Token XXX" -d username=root -d language=cs https://<my_domain>api/v1/accounts/2/

Response header:
HTTP/1.1 500 Internal Server Error Server: nginx/1.10.1 Date: Thu, 02 Jun 2016 21:01:30 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Language, Cookie X-Frame-Options: SAMEORIGIN Content-Language: en

I'll be more than happy to provide any additional information as needed.

Originally created by @jficz on GitHub (Jun 2, 2016). Original GitHub issue: https://github.com/modoboa/modoboa/issues/884 When I try to alter an account via API using API key of a SuperAdmin user (the id=1 user in fact), I get this error: ``` KeyError at /api/v1/accounts/2/ 'role' Request Method: PATCH Request URL: https://<my_domain>/api/v1/accounts/2/ Django Version: 1.9.6 Exception Type: KeyError Exception Value: 'role' Exception Location: /srv/app/modoboa/env/local/lib/python2.7/site-packages/modoboa/admin/serializers.py in validate, line 189 Python Executable: /usr/bin/uwsgi-core Python Version: 2.7.9 Python Path: ['.', '', '/srv/app/modoboa/env/lib/python2.7', '/srv/app/modoboa/env/lib/python2.7/plat-x86_64-linux-gnu', '/srv/app/modoboa/env/lib/python2.7/lib-tk', '/srv/app/modoboa/env/lib/python2.7/lib-old', '/srv/app/modoboa/env/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/srv/app/modoboa/env/local/lib/python2.7/site-packages'] Server time: Thu, 2 Jun 2016 21:01:30 +0000 ``` With this stack trace: ``` Environment: Request Method: PATCH Request URL: https://<my_domain>/api/v1/accounts/2/ Django Version: 1.9.6 Python Version: 2.7.9 Installed Applications: ('django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.sites', 'django.contrib.staticfiles', 'reversion', 'rest_framework.authtoken', 'rest_framework_swagger', 'modoboa', 'modoboa.core', 'modoboa.lib', 'modoboa.admin', 'modoboa.relaydomains', 'modoboa.limits', 'modoboa_sievefilters', 'modoboa_amavis', 'modoboa_stats', 'modoboa_postfix_autoreply', 'modoboa_webmail') Installed Middleware: ('x_forwarded_for.middleware.XForwardedForMiddleware', '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.locale.LocaleMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'modoboa.lib.middleware.AjaxLoginRedirect', 'modoboa.lib.middleware.CommonExceptionCatcher', 'modoboa.lib.middleware.RequestCatcherMiddleware') Traceback: File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 149. response = self.process_exception_by_middleware(e, request) File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 147. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/django/utils/decorators.py" in inner 184. return func(*args, **kwargs) File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/django/utils/decorators.py" in inner 184. return func(*args, **kwargs) File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view 58. return view_func(*args, **kwargs) File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/viewsets.py" in view 87. return self.dispatch(request, *args, **kwargs) File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch 466. response = self.handle_exception(exc) File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch 463. response = handler(request, *args, **kwargs) File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/mixins.py" in partial_update 78. return self.update(request, *args, **kwargs) File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/mixins.py" in update 69. serializer.is_valid(raise_exception=True) File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/serializers.py" in is_valid 213. self._validated_data = self.run_validation(self.initial_data) File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/rest_framework/serializers.py" in run_validation 410. value = self.validate(value) File "/srv/app/modoboa/env/local/lib/python2.7/site-packages/modoboa/admin/serializers.py" in validate 189. if data["role"] == "SimpleUsers": Exception Type: KeyError at /api/v1/accounts/2/ Exception Value: 'role' ``` Debian Jessie latest, barebone. Modoba installed in a virutalenv using pip install method. Curl command used to make the api call: `curl -ik -X POST -H "Authorization: Token XXX" -d username=root -d language=cs https://<my_domain>api/v1/accounts/2/` Response header: `HTTP/1.1 500 Internal Server Error Server: nginx/1.10.1 Date: Thu, 02 Jun 2016 21:01:30 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Language, Cookie X-Frame-Options: SAMEORIGIN Content-Language: en` I'll be more than happy to provide any additional information as needed.
kerem 2026-02-27 11:13:31 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@tonioo commented on GitHub (Jun 6, 2016):

@cptMikky Thanks for the feedback.

I realize I did not implement the PATCH method properly. It is provided by default (django rest framework) and I should have disabled it.

<!-- gh-comment-id:223972333 --> @tonioo commented on GitHub (Jun 6, 2016): @cptMikky Thanks for the feedback. I realize I did not implement the PATCH method properly. It is provided by default (django rest framework) and I should have disabled it.
Author
Owner

@jficz commented on GitHub (Jun 6, 2016):

How does one alter an existing account via API then? Or am I missing something?

<!-- gh-comment-id:224013924 --> @jficz commented on GitHub (Jun 6, 2016): How does one alter an existing account via API then? Or am I missing something?
Author
Owner

@tonioo commented on GitHub (Jun 7, 2016):

You can use the PUT method.

<!-- gh-comment-id:224193782 --> @tonioo commented on GitHub (Jun 7, 2016): You can use the PUT method.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/modoboa-modoboa#782
No description provided.