Skip to content

Commit

Permalink
Add property setter for RequestHandler.locale
Browse files Browse the repository at this point in the history
  • Loading branch information
bdarnell committed Aug 12, 2014
1 parent fc8264a commit bb00b7e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/releases/next.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ In progress
`.WebSocketHandler.get_compression_options` to enable on the server
side, and use the ``compression_options`` keyword argument to
`.websocket_connect` on the client side.
* `.RequestHandler.locale` now has a property setter.
1 change: 1 addition & 0 deletions docs/web.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
.. automethod:: RequestHandler.get_status
.. automethod:: RequestHandler.get_template_path
.. automethod:: RequestHandler.get_user_locale
.. autoattribute:: RequestHandler.locale
.. automethod:: RequestHandler.log_exception
.. automethod:: RequestHandler.on_connection_close
.. automethod:: RequestHandler.require_setting
Expand Down
17 changes: 12 additions & 5 deletions tornado/test/web_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tornado.escape import json_decode, utf8, to_unicode, recursive_unicode, native_str, to_basestring
from tornado.httputil import format_timestamp
from tornado.iostream import IOStream
from tornado import locale
from tornado.log import app_log, gen_log
from tornado.simple_httpclient import SimpleAsyncHTTPClient
from tornado.template import DictLoader
Expand All @@ -21,7 +22,6 @@
import os
import re
import socket
import sys

try:
import urllib.parse as urllib_parse # py3
Expand Down Expand Up @@ -1554,19 +1554,26 @@ def test_multi_exception(self):


@wsgi_safe
class SetCurrentUserTest(SimpleHandlerTestCase):
class SetLazyPropertiesTest(SimpleHandlerTestCase):
class Handler(RequestHandler):
def prepare(self):
self.current_user = 'Ben'
self.locale = locale.get('en_US')

def get_user_locale(self):
raise NotImplementedError()

def get_current_user(self):
raise NotImplementedError()

def get(self):
self.write('Hello %s' % self.current_user)
self.write('Hello %s (%s)' % (self.current_user, self.locale.code))

def test_set_current_user(self):
def test_set_properties(self):
# Ensure that current_user can be assigned to normally for apps
# that want to forgo the lazy get_current_user property
response = self.fetch('/')
self.assertEqual(response.body, b'Hello Ben')
self.assertEqual(response.body, b'Hello Ben (en_US)')


@wsgi_safe
Expand Down
9 changes: 8 additions & 1 deletion tornado/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,12 +956,15 @@ def write_error(self, status_code, **kwargs):

@property
def locale(self):
"""The local for the current session.
"""The locale for the current session.
Determined by either `get_user_locale`, which you can override to
set the locale based on, e.g., a user preference stored in a
database, or `get_browser_locale`, which uses the ``Accept-Language``
header.
.. versionchanged: 4.1
Added a property setter.
"""
if not hasattr(self, "_locale"):
self._locale = self.get_user_locale()
Expand All @@ -970,6 +973,10 @@ def locale(self):
assert self._locale
return self._locale

@locale.setter
def locale(self, value):
self._locale = value

def get_user_locale(self):
"""Override to determine the locale from the authenticated user.
Expand Down

0 comments on commit bb00b7e

Please sign in to comment.