diff --git a/ordr2/events.py b/ordr2/events.py index 411f4ee..f569fe9 100644 --- a/ordr2/events.py +++ b/ordr2/events.py @@ -1,4 +1,4 @@ -from pyramid.events import subscriber +from pyramid.events import NewRequest, subscriber class UserLogIn(object): @@ -8,12 +8,14 @@ class UserLogIn(object): @subscriber(UserLogIn) +@subscriber(NewRequest) def set_display_defaults(event): - defaults = { - 'users': { - 'first': True, - 'last': True, - 'email': True, + if event.request.user and 'display' not in event.request.session: + defaults = { + 'users': { + 'first': True, + 'last': True, + 'email': True, + } } - } - event.request.session['display'] = defaults + event.request.session['display'] = defaults diff --git a/ordr2/views/__init__.py b/ordr2/views/__init__.py index e903c81..435f66b 100644 --- a/ordr2/views/__init__.py +++ b/ordr2/views/__init__.py @@ -10,6 +10,18 @@ def flash(request, channel, message, description='', dismissable=True): request.session.flash(msg, channel, allow_duplicate=False) +def update_column_display(request, section): + ''' update the session values for wich columns to display ''' + if section not in request.session['display']: + return + display_keys = request.session['display'][section].keys() + display = dict.fromkeys(display_keys, False) + for column in request.POST.values(): + if column in display: + display[column] = True + request.session['display'][section] = display + + def includeme(config): ''' adding request helpers and static views ''' config.add_request_method(flash, 'flash') diff --git a/ordr2/views/admin.py b/ordr2/views/admin.py index 9e947dd..4234e6c 100644 --- a/ordr2/views/admin.py +++ b/ordr2/views/admin.py @@ -8,6 +8,8 @@ from pyramid.view import view_config from ordr2.models import User, Role from ordr2.schemas.account import UserSchema +from . import update_column_display + # user log in and log out @view_config( @@ -53,14 +55,7 @@ def user_list(context, request): ) def change_column_view(context, request): ''' changes the columns to display ''' - display = { - 'first': False, - 'last': False, - 'email': False - } - columns = [col for key, col in request.POST.items() if key == 'display'] - display.update({ column: True for column in columns }) - request.session['display']['users'] = display + update_column_display(request, 'users') return HTTPFound(context.url())