Skip to content

Commit

Permalink
add api for fetching timezones (#1959)
Browse files Browse the repository at this point in the history
SDCP-279
  • Loading branch information
petrjasek committed Aug 14, 2020
1 parent b74a0f4 commit 7139db6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions features/locales.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Feature: Localization

@auth
Scenario: Get timezones

When we get "/locales/timezones"
Then we get existing resource
"""
{"timezones": [
{
"id": "America/Edmonton",
"name": "Mountain Time",
"location": "Canada (Edmonton) Time"
}
]}
"""
1 change: 1 addition & 0 deletions superdesk/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ def local_to_utc_hour(hour):
'superdesk.attachments',
'superdesk.auth_server',
'apps.links',
'superdesk.locales',
]

#: Specify what modules should be enabled
Expand Down
31 changes: 31 additions & 0 deletions superdesk/locales.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

import pytz
import flask
import babel.dates as dates

from apps.auth import get_user


bp = flask.Blueprint('locales', __name__)


@bp.route('/locales/timezones')
def locales_view():
if not flask.current_app.auth.authorized([], 'locales', 'GET'):
flask.abort(401)
user = get_user()
lang = user.get('language', flask.current_app.config.get('DEFAULT_LANGUAGE', 'en')).replace('-', '_')
return flask.jsonify({
'timezones': [
{
'id': tz,
'name': dates.get_timezone_name(tz, locale=lang),
'location': dates.get_timezone_location(tz, locale=lang),
} for tz in pytz.common_timezones
]
})


def init_app(app):
bp.url_prefix = '/{}'.format(app.config['URL_PREFIX'].lstrip('/'))
app.register_blueprint(bp)

0 comments on commit 7139db6

Please sign in to comment.