# HG changeset patch # User Marcin Kuzminski # Date 2018-09-24 21:18:38 # Node ID fbcbf993ebaf7a4dda0fa5d9e32ca797bdd95672 # Parent ff035148f7f8cacf4e0d3a8de8f908a891d50196 users: use new session store for temporary settings for users. - use for clone url type selection - use for diffs mode diff --git a/rhodecode/apps/home/__init__.py b/rhodecode/apps/home/__init__.py --- a/rhodecode/apps/home/__init__.py +++ b/rhodecode/apps/home/__init__.py @@ -63,6 +63,10 @@ def includeme(config): name='markup_preview', pattern='/_markup_preview') + config.add_route( + name='store_user_session_value', + pattern='/_store_session_attr') + # register our static links via redirection mechanism routing_links.connect_redirection_links(config) diff --git a/rhodecode/apps/home/views.py b/rhodecode/apps/home/views.py --- a/rhodecode/apps/home/views.py +++ b/rhodecode/apps/home/views.py @@ -444,3 +444,18 @@ class HomeView(BaseAppView): if text: return h.render(text, renderer=renderer, mentions=True) return '' + + @LoginRequired() + @CSRFRequired() + @view_config( + route_name='store_user_session_value', request_method='POST', + renderer='string', xhr=True) + def store_user_session_attr(self): + key = self.request.POST.get('key') + val = self.request.POST.get('val') + + existing_value = self.request.session.get(key) + if existing_value != val: + self.request.session[key] = val + + return 'stored:{}'.format(key) diff --git a/rhodecode/lib/base.py b/rhodecode/lib/base.py --- a/rhodecode/lib/base.py +++ b/rhodecode/lib/base.py @@ -357,6 +357,28 @@ def attach_context_attributes(context, r 'appenlight.api_public_key', '') context.appenlight_server_url = config.get('appenlight.server_url', '') + diffmode = { + "unified": "unified", + "sideside": "sideside" + }.get(request.GET.get('diffmode')) + + if diffmode and diffmode != request.session.get('rc_user_session_attr.diffmode'): + request.session['rc_user_session_attr.diffmode'] = diffmode + + # session settings per user + session_attrs = { + # defaults + "clone_url_format": "http", + "diffmode": "sideside" + } + for k, v in request.session.items(): + pref = 'rc_user_session_attr.' + if k and k.startswith(pref): + k = k[len(pref):] + session_attrs[k] = v + + context.user_session_attrs = session_attrs + # JS template context context.template_context = { 'repo_name': None, @@ -367,6 +389,7 @@ def attach_context_attributes(context, r 'email': None, 'notification_status': False }, + 'session_attrs': session_attrs, 'visual': { 'default_renderer': None }, @@ -385,18 +408,6 @@ def attach_context_attributes(context, r } # END CONFIG VARS - diffmode = 'sideside' - if request.GET.get('diffmode'): - if request.GET['diffmode'] == 'unified': - diffmode = 'unified' - elif request.session.get('diffmode'): - diffmode = request.session['diffmode'] - - context.diffmode = diffmode - - if request.session.get('diffmode') != diffmode: - request.session['diffmode'] = diffmode - context.csrf_token = auth.get_csrf_token(session=request.session) context.backends = rhodecode.BACKENDS.keys() context.backends.sort() diff --git a/rhodecode/public/js/rhodecode/routes.js b/rhodecode/public/js/rhodecode/routes.js --- a/rhodecode/public/js/rhodecode/routes.js +++ b/rhodecode/public/js/rhodecode/routes.js @@ -150,6 +150,7 @@ function registerRCRoutes() { pyroutes.register('repo_list_data', '/_repos', []); pyroutes.register('goto_switcher_data', '/_goto_data', []); pyroutes.register('markup_preview', '/_markup_preview', []); + pyroutes.register('store_user_session_value', '/_store_session_attr', []); pyroutes.register('journal', '/_admin/journal', []); pyroutes.register('journal_rss', '/_admin/journal/rss', []); pyroutes.register('journal_atom', '/_admin/journal/atom', []); diff --git a/rhodecode/public/js/src/rhodecode.js b/rhodecode/public/js/src/rhodecode.js --- a/rhodecode/public/js/src/rhodecode.js +++ b/rhodecode/public/js/src/rhodecode.js @@ -553,3 +553,20 @@ var feedLifetimeOptions = function(query query.callback(data); }; + + +var storeUserSessionAttr = function (key, val) { + + var postData = { + 'key': key, + 'val': val, + 'csrf_token': CSRF_TOKEN + }; + + var success = function(o) { + return true + }; + + ajaxPOST(pyroutes.url('store_user_session_value'), postData, success); + return false; +}; diff --git a/rhodecode/templates/codeblocks/diffs.mako b/rhodecode/templates/codeblocks/diffs.mako --- a/rhodecode/templates/codeblocks/diffs.mako +++ b/rhodecode/templates/codeblocks/diffs.mako @@ -83,7 +83,7 @@ return '%s_%s_%i' % (h.safeid(filename), collapse_all = len(diffset.files) > collapse_when_files_over %> -%if c.diffmode == 'sideside': +%if c.user_session_attrs["diffmode"] == 'sideside':