diff --git a/rhodecode/apps/my_account/__init__.py b/rhodecode/apps/my_account/__init__.py --- a/rhodecode/apps/my_account/__init__.py +++ b/rhodecode/apps/my_account/__init__.py @@ -41,12 +41,15 @@ def includeme(config): pattern=ADMIN_PREFIX + '/my_account/auth_tokens') config.add_route( name='my_account_auth_tokens_add', - pattern=ADMIN_PREFIX + '/my_account/auth_tokens/new', - ) + pattern=ADMIN_PREFIX + '/my_account/auth_tokens/new') config.add_route( name='my_account_auth_tokens_delete', - pattern=ADMIN_PREFIX + '/my_account/auth_tokens/delete', - ) + pattern=ADMIN_PREFIX + '/my_account/auth_tokens/delete') + + # channelstream test + config.add_route( + name='my_account_notifications_test_channelstream', + pattern=ADMIN_PREFIX + '/my_account/test_channelstream') # Scan module for configuration decorators. config.scan() diff --git a/rhodecode/apps/my_account/views.py b/rhodecode/apps/my_account/views.py --- a/rhodecode/apps/my_account/views.py +++ b/rhodecode/apps/my_account/views.py @@ -19,14 +19,17 @@ # and proprietary license terms, please see https://rhodecode.com/licenses/ import logging +import datetime from pyramid.httpexceptions import HTTPFound from pyramid.view import view_config from rhodecode.apps._base import BaseAppView from rhodecode import forms +from rhodecode.lib import helpers as h from rhodecode.lib.auth import LoginRequired, NotAnonymous, CSRFRequired -from rhodecode.lib import helpers as h +from rhodecode.lib.channelstream import channelstream_request, \ + ChannelstreamException from rhodecode.lib.utils2 import safe_int, md5 from rhodecode.model.auth_token import AuthTokenModel from rhodecode.model.meta import Session @@ -192,3 +195,37 @@ class MyAccountView(BaseAppView): h.flash(_("Auth token successfully deleted"), category='success') return HTTPFound(h.route_path('my_account_auth_tokens')) + + @LoginRequired() + @NotAnonymous() + @CSRFRequired() + @view_config( + route_name='my_account_notifications_test_channelstream', + request_method='POST', renderer='json_ext') + def my_account_notifications_test_channelstream(self): + message = 'Test message sent via Channelstream by user: {}, on {}'.format( + self._rhodecode_user.username, datetime.datetime.now()) + payload = { + # 'channel': 'broadcast', + 'type': 'message', + 'timestamp': datetime.datetime.utcnow(), + 'user': 'system', + 'pm_users': [self._rhodecode_user.username], + 'message': { + 'message': message, + 'level': 'info', + 'topic': '/notifications' + } + } + + registry = self.request.registry + rhodecode_plugins = getattr(registry, 'rhodecode_plugins', {}) + channelstream_config = rhodecode_plugins.get('channelstream', {}) + + try: + channelstream_request(channelstream_config, [payload], '/message') + except ChannelstreamException as e: + log.exception('Failed to send channelstream data') + return {"response": 'ERROR: {}'.format(e.__class__.__name__)} + return {"response": 'Channelstream data sent. ' + 'You should see a new live message now.'} diff --git a/rhodecode/config/routing.py b/rhodecode/config/routing.py --- a/rhodecode/config/routing.py +++ b/rhodecode/config/routing.py @@ -513,10 +513,6 @@ def make_map(config): '/my_account/toggle_visibility', action='my_notifications_toggle_visibility', conditions={'method': ['POST']}) - m.connect('my_account_notifications_test_channelstream', - '/my_account/test_channelstream', - action='my_account_notifications_test_channelstream', - conditions={'method': ['POST']}) # NOTIFICATION REST ROUTES with rmap.submapper(path_prefix=ADMIN_PREFIX, diff --git a/rhodecode/controllers/admin/my_account.py b/rhodecode/controllers/admin/my_account.py --- a/rhodecode/controllers/admin/my_account.py +++ b/rhodecode/controllers/admin/my_account.py @@ -24,11 +24,9 @@ my account controller for RhodeCode admi """ import logging -import datetime import formencode from formencode import htmlfill -from pyramid.threadlocal import get_current_registry from pyramid.httpexceptions import HTTPFound from pylons import request, tmpl_context as c, url @@ -44,8 +42,6 @@ from rhodecode.lib.base import BaseContr from rhodecode.lib.utils import jsonify from rhodecode.lib.utils2 import safe_int, str2bool from rhodecode.lib.ext_json import json -from rhodecode.lib.channelstream import channelstream_request, \ - ChannelstreamException from rhodecode.model.db import ( Repository, PullRequest, UserEmailMap, User, UserFollowing) @@ -332,33 +328,3 @@ class MyAccountController(BaseController user.update_userdata(notification_status=new_status) Session().commit() return user.user_data['notification_status'] - - @auth.CSRFRequired() - @jsonify - def my_account_notifications_test_channelstream(self): - message = 'Test message sent via Channelstream by user: {}, on {}'.format( - c.rhodecode_user.username, datetime.datetime.now()) - payload = { - 'type': 'message', - 'timestamp': datetime.datetime.utcnow(), - 'user': 'system', - #'channel': 'broadcast', - 'pm_users': [c.rhodecode_user.username], - 'message': { - 'message': message, - 'level': 'info', - 'topic': '/notifications' - } - } - - registry = get_current_registry() - rhodecode_plugins = getattr(registry, 'rhodecode_plugins', {}) - channelstream_config = rhodecode_plugins.get('channelstream', {}) - - try: - channelstream_request(channelstream_config, [payload], '/message') - except ChannelstreamException as e: - log.exception('Failed to send channelstream data') - return {"response": 'ERROR: {}'.format(e.__class__.__name__)} - return {"response": 'Channelstream data sent. ' - 'You should see a new live message now.'} 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 @@ -123,5 +123,6 @@ function registerRCRoutes() { pyroutes.register('my_account_auth_tokens', '/_admin/my_account/auth_tokens', []); pyroutes.register('my_account_auth_tokens_add', '/_admin/my_account/auth_tokens/new', []); pyroutes.register('my_account_auth_tokens_delete', '/_admin/my_account/auth_tokens/delete', []); + pyroutes.register('my_account_notifications_test_channelstream', '/_admin/my_account/test_channelstream', []); pyroutes.register('apiv2', '/_admin/api', []); } diff --git a/rhodecode/templates/admin/my_account/my_account_notifications.mako b/rhodecode/templates/admin/my_account/my_account_notifications.mako --- a/rhodecode/templates/admin/my_account/my_account_notifications.mako +++ b/rhodecode/templates/admin/my_account/my_account_notifications.mako @@ -10,7 +10,7 @@