##// END OF EJS Templates
helpers: remove usage of pylons session.
marcink -
r2095:ed5795d8 default
parent child Browse files
Show More
@@ -41,7 +41,7 b' def route_path(name, params=None, **kwar'
41
41
42
42
43 @pytest.mark.usefixtures("app")
43 @pytest.mark.usefixtures("app")
44 class TestDefaultsController(object):
44 class TestDefaultsView(object):
45
45
46 def test_index(self, autologin_user):
46 def test_index(self, autologin_user):
47 response = self.app.get(route_path('admin_defaults_repositories'))
47 response = self.app.get(route_path('admin_defaults_repositories'))
@@ -608,10 +608,20 b' def add_events_routes(config):'
608
608
609 def bootstrap_request(**kwargs):
609 def bootstrap_request(**kwargs):
610 import pyramid.testing
610 import pyramid.testing
611 request = pyramid.testing.DummyRequest(**kwargs)
611
612 request.application_url = kwargs.pop('application_url', 'http://example.com')
612 class TestRequest(pyramid.testing.DummyRequest):
613 request.host = kwargs.pop('host', 'example.com:80')
613 application_url = kwargs.pop('application_url', 'http://example.com')
614 request.domain = kwargs.pop('domain', 'example.com')
614 host = kwargs.pop('host', 'example.com:80')
615 domain = kwargs.pop('domain', 'example.com')
616
617 class TestDummySession(pyramid.testing.DummySession):
618 def save(*arg, **kw):
619 pass
620
621 request = TestRequest(**kwargs)
622 request.session = TestDummySession()
615
623
616 config = pyramid.testing.setUp(request=request)
624 config = pyramid.testing.setUp(request=request)
617 add_events_routes(config)
625 add_events_routes(config)
626 return request
627
@@ -60,7 +60,6 b' from webhelpers.html.tags import auto_di'
60 submit, text, password, textarea, title, ul, xml_declaration, radio
60 submit, text, password, textarea, title, ul, xml_declaration, radio
61 from webhelpers.html.tools import auto_link, button_to, highlight, \
61 from webhelpers.html.tools import auto_link, button_to, highlight, \
62 js_obfuscate, mail_to, strip_links, strip_tags, tag_re
62 js_obfuscate, mail_to, strip_links, strip_tags, tag_re
63 from webhelpers.pylonslib import Flash as _Flash
64 from webhelpers.text import chop_at, collapse, convert_accented_entities, \
63 from webhelpers.text import chop_at, collapse, convert_accented_entities, \
65 convert_misc_entities, lchop, plural, rchop, remove_formatting, \
64 convert_misc_entities, lchop, plural, rchop, remove_formatting, \
66 replace_whitespace, urlify, truncate, wrap_paragraphs
65 replace_whitespace, urlify, truncate, wrap_paragraphs
@@ -661,19 +660,48 b' class _Message(object):'
661 return escape(safe_unicode(self.message))
660 return escape(safe_unicode(self.message))
662
661
663
662
664 class Flash(_Flash):
663 class Flash(object):
664 # List of allowed categories. If None, allow any category.
665 categories = ["warning", "notice", "error", "success"]
666
667 # Default category if none is specified.
668 default_category = "notice"
669
670 def __init__(self, session_key="flash", categories=None,
671 default_category=None):
672 """
673 Instantiate a ``Flash`` object.
674
675 ``session_key`` is the key to save the messages under in the user's
676 session.
665
677
666 def pop_messages(self, request=None):
678 ``categories`` is an optional list which overrides the default list
667 """Return all accumulated messages and delete them from the session.
679 of categories.
680
681 ``default_category`` overrides the default category used for messages
682 when none is specified.
683 """
684 self.session_key = session_key
685 if categories is not None:
686 self.categories = categories
687 if default_category is not None:
688 self.default_category = default_category
689 if self.categories and self.default_category not in self.categories:
690 raise ValueError(
691 "unrecognized default category %r" % (self.default_category,))
692
693 def pop_messages(self, session=None, request=None):
694 """
695 Return all accumulated messages and delete them from the session.
668
696
669 The return value is a list of ``Message`` objects.
697 The return value is a list of ``Message`` objects.
670 """
698 """
671 messages = []
699 messages = []
672
700
673 if request:
701 if not session:
702 if not request:
703 request = get_current_request()
674 session = request.session
704 session = request.session
675 else:
676 from pylons import session
677
705
678 # Pop the 'old' pylons flash messages. They are tuples of the form
706 # Pop the 'old' pylons flash messages. They are tuples of the form
679 # (category, message)
707 # (category, message)
@@ -692,9 +720,9 b' class Flash(_Flash):'
692 session.save()
720 session.save()
693 return messages
721 return messages
694
722
695 def json_alerts(self, request=None):
723 def json_alerts(self, session=None, request=None):
696 payloads = []
724 payloads = []
697 messages = flash.pop_messages(request=request)
725 messages = flash.pop_messages(session=session, request=request)
698 if messages:
726 if messages:
699 for message in messages:
727 for message in messages:
700 subdata = {}
728 subdata = {}
@@ -715,6 +743,18 b' class Flash(_Flash):'
715 })
743 })
716 return json.dumps(payloads)
744 return json.dumps(payloads)
717
745
746 def __call__(self, message, category=None, ignore_duplicate=False,
747 session=None, request=None):
748
749 if not session:
750 if not request:
751 request = get_current_request()
752 session = request.session
753
754 session.flash(
755 message, queue=category, allow_duplicate=not ignore_duplicate)
756
757
718 flash = Flash()
758 flash = Flash()
719
759
720 #==============================================================================
760 #==============================================================================
@@ -120,7 +120,7 b" c.template_context['default_user'] = {"
120 <script language="javascript" type="text/javascript" src="${h.asset('js/excanvas.min.js')}"></script>
120 <script language="javascript" type="text/javascript" src="${h.asset('js/excanvas.min.js')}"></script>
121 <![endif]-->
121 <![endif]-->
122 <script language="javascript" type="text/javascript" src="${h.asset('js/rhodecode/routes.js', ver=c.rhodecode_version_hash)}"></script>
122 <script language="javascript" type="text/javascript" src="${h.asset('js/rhodecode/routes.js', ver=c.rhodecode_version_hash)}"></script>
123 <script> var alertMessagePayloads = ${h.flash.json_alerts(request)|n}; </script>
123 <script> var alertMessagePayloads = ${h.flash.json_alerts(request=request)|n}; </script>
124 ## avoide escaping the %N
124 ## avoide escaping the %N
125 <script language="javascript" type="text/javascript" src="${h.asset('js/rhodecode-components.js', ver=c.rhodecode_version_hash)}"></script>
125 <script language="javascript" type="text/javascript" src="${h.asset('js/rhodecode-components.js', ver=c.rhodecode_version_hash)}"></script>
126 <script>CodeMirror.modeURL = "${h.asset('') + 'js/mode/%N/%N.js?ver='+c.rhodecode_version_hash}";</script>
126 <script>CodeMirror.modeURL = "${h.asset('') + 'js/mode/%N/%N.js?ver='+c.rhodecode_version_hash}";</script>
@@ -28,27 +28,16 b' from os.path import join as jn'
28
28
29 from tempfile import _RandomNameSequence
29 from tempfile import _RandomNameSequence
30
30
31 from paste.deploy import loadapp
31 from pylons import url
32 from paste.script.appinstall import SetupCommand
33
32
34 import pylons
35 import pylons.test
36 from pylons import config, url
37 from pylons.i18n.translation import _get_translator
38 from pylons.util import ContextObj
39
40 from routes.util import URLGenerator
41 from nose.plugins.skip import SkipTest
33 from nose.plugins.skip import SkipTest
42 import pytest
34 import pytest
43
35
44 from rhodecode import is_windows
45 from rhodecode.config.routing import ADMIN_PREFIX
46 from rhodecode.model.meta import Session
47 from rhodecode.model.db import User
36 from rhodecode.model.db import User
48 from rhodecode.lib import auth
37 from rhodecode.lib import auth
49 from rhodecode.lib import helpers as h
38 from rhodecode.lib import helpers as h
50 from rhodecode.lib.helpers import flash, link_to
39 from rhodecode.lib.helpers import flash, link_to
51 from rhodecode.lib.utils2 import safe_unicode, safe_str
40 from rhodecode.lib.utils2 import safe_str
52
41
53
42
54 log = logging.getLogger(__name__)
43 log = logging.getLogger(__name__)
@@ -117,6 +106,7 b' try:'
117 import ldap
106 import ldap
118 ldap_lib_installed = True
107 ldap_lib_installed = True
119 except ImportError:
108 except ImportError:
109 ldap = None
120 # means that python-ldap is not installed
110 # means that python-ldap is not installed
121 pass
111 pass
122
112
@@ -196,18 +186,18 b' def login_user(app, username=TEST_USER_A'
196 return login_user_session(app, username, password)['rhodecode_user']
186 return login_user_session(app, username, password)['rhodecode_user']
197
187
198
188
199 def assert_session_flash(response=None, msg=None, category=None, no_=None):
189 def assert_session_flash(response, msg=None, category=None, no_=None):
200 """
190 """
201 Assert on a flash message in the current session.
191 Assert on a flash message in the current session.
202
192
203 :param msg: Required. The expected message. Will be evaluated if a
193 :param response: Response from give calll, it will contain flash
194 messages or bound session with them.
195 :param msg: The expected message. Will be evaluated if a
204 :class:`LazyString` is passed in.
196 :class:`LazyString` is passed in.
205 :param response: Optional. For functional testing, pass in the response
206 object. Otherwise don't pass in any value.
207 :param category: Optional. If passed, the message category will be
197 :param category: Optional. If passed, the message category will be
208 checked as well.
198 checked as well.
209 :param no_: Optional. If passed, the message will be checked to NOT be in the
199 :param no_: Optional. If passed, the message will be checked to NOT
210 flash session
200 be in the flash session
211 """
201 """
212 if msg is None and no_ is None:
202 if msg is None and no_ is None:
213 raise ValueError("Parameter msg or no_ is required.")
203 raise ValueError("Parameter msg or no_ is required.")
@@ -215,7 +205,8 b' def assert_session_flash(response=None, '
215 if msg and no_:
205 if msg and no_:
216 raise ValueError("Please specify either msg or no_, but not both")
206 raise ValueError("Please specify either msg or no_, but not both")
217
207
218 messages = flash.pop_messages()
208 session = response.get_session_from_response()
209 messages = flash.pop_messages(session=session)
219 msg = _eval_if_lazy(msg)
210 msg = _eval_if_lazy(msg)
220
211
221 assert messages, 'unable to find message `%s` in empty flash list' % msg
212 assert messages, 'unable to find message `%s` in empty flash list' % msg
@@ -242,14 +233,6 b' def _eval_if_lazy(value):'
242 return value.eval() if hasattr(value, 'eval') else value
233 return value.eval() if hasattr(value, 'eval') else value
243
234
244
235
245 def assert_session_flash_is_empty(response):
246 assert 'flash' in response.session, 'Response session has no flash key'
247
248 msg = 'flash messages are present in session:%s' % \
249 response.session['flash'][0]
250 pytest.fail(safe_str(msg))
251
252
253 def no_newline_id_generator(test_name):
236 def no_newline_id_generator(test_name):
254 """
237 """
255 Generates a test name without spaces or newlines characters. Used for
238 Generates a test name without spaces or newlines characters. Used for
@@ -1669,8 +1669,8 b' def request_stub():'
1669 """
1669 """
1670 Stub request object.
1670 Stub request object.
1671 """
1671 """
1672 request = pyramid.testing.DummyRequest()
1672 from rhodecode.lib.base import bootstrap_request
1673 request.scheme = 'https'
1673 request = bootstrap_request(scheme='https')
1674 return request
1674 return request
1675
1675
1676
1676
General Comments 0
You need to be logged in to leave comments. Login now