aptivate / kashana

Aptivate logframe django project
GNU Affero General Public License v3.0
13 stars 15 forks source link

No UserPreferences created; logframe view crashes #22

Open bitterjug opened 8 years ago

bitterjug commented 8 years ago

I just did a new deploy:dev on master and, as there were no logframes (#21) I went over to /admin/logframe/logframe/ and created me one called test. Now I see it listed in the dashboard, but when I follow the link I get RelatedObjectDoesNotExist at /dashboard/test/.

Exception Value: User has no preferences.

You'd expect a user with no preferences would be easy to please.

Grepping for preferences in the source, I can't see anywhere where the preferences object gets created. Tests pass because the user object is mock.Mock()

And I suspect local user testing worked because a pre-existing user in the database got preferences added by the migration

But I can't see any code to create a preferences object for a new user.

Request Method: GET
Request URL: http://127.0.0.1:8000/dashboard/test/

Django Version: 1.8.13
Python Version: 2.7.11
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.humanize',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'rest_framework',
 'django_tables2',
 'jstemplate',
 'django_extensions',
 'django_assets',
 'floppyforms',
 'rest_framework_nested',
 'main',
 'logframe',
 'contacts',
 'dashboard',
 'appconf',
 'export')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')

Traceback:
File "/home/mark/workspace/kashana/django/website/.ve/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/mark/workspace/kashana/django/website/.ve/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)
File "/home/mark/workspace/kashana/django/website/.ve/local/lib/python2.7/site-packages/braces/views.py" in dispatch
  107.             request, *args, **kwargs)
File "/home/mark/workspace/kashana/django/website/.ve/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  89.         return handler(request, *args, **kwargs)
File "/home/mark/workspace/kashana/django/website/.ve/local/lib/python2.7/site-packages/django/views/generic/base.py" in get
  158.         context = self.get_context_data(**kwargs)
File "/home/mark/workspace/kashana/django/website/logframe/mixins.py" in get_context_data
  80.         lf = self.get_logframe()
File "/home/mark/workspace/kashana/django/website/dashboard/mixins.py" in get_logframe
  27.         logframe = user.preferences.last_viewed_logframe
File "/home/mark/workspace/kashana/django/website/.ve/local/lib/python2.7/site-packages/django/utils/functional.py" in inner
  226.         return func(self._wrapped, *args)
File "/home/mark/workspace/kashana/django/website/.ve/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in __get__
  480.                     self.related.get_accessor_name()

Exception Type: RelatedObjectDoesNotExist at /dashboard/test/
Exception Value: User has no preferences.
bitterjug commented 8 years ago

I think the fix should be to create the preferences when we try to access them, not when users are created, so as to be sure preferences are created transparently for existing users without them. Otherwise we have to trigger a save on each user to ensure they have a preferences instance. Maybe deploy along with another migration to make sure all existing users have preferences?

daniell commented 8 years ago

I've updated the fix to do the check when the 'preferences' property is accessed.