Changeset fd6da93 in OpenWorkouts-current for ow/views/user.py


Ignore:
Timestamp:
Feb 26, 2019, 11:11:43 AM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
a4e4799
Parents:
d411dae
Message:

(#56) Add support for different locale/language:

  • Let users choose their lang/locale in the edit profile page
  • Set the currently selected locale as a cookie (following pyramid docs on how to set the locale using the default locale negotiator)
  • Save the locale setting for each user as an attribute on the User model
  • Set the proper locale as a cookie on login
  • Unset the locale cookie on logout

Default available locales for now are en (english) and es (spanish)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/views/user.py

    rd411dae rfd6da93  
    2222from ..models.root import OpenWorkouts
    2323from ..views.renderers import OWFormRenderer
    24 from ..utilities import timedelta_to_hms, get_verification_token
     24from ..utilities import (
     25    timedelta_to_hms,
     26    get_verification_token,
     27    get_available_locale_names
     28)
    2529from ..mail import send_verification_email
    2630
     
    7680                password = request.POST.get('password', None)
    7781                if password is not None and user.check_password(password):
     82                    # look for the value of locale for this user, to set the
     83                    # LOCALE cookie, so the UI appears on the pre-selected lang
     84                    default_locale = request.registry.settings.get(
     85                        'pyramid.default_locale_name')
     86                    locale = getattr(user, 'locale', default_locale)
     87                    request.response.set_cookie('_LOCALE_', locale)
     88                    # log in the user and send back to the place he wanted to
     89                    # visit
    7890                    headers = remember(request, str(user.uid))
     91                    request.response.headers.extend(headers)
    7992                    redirect_url = return_to or request.resource_url(user)
    80                     return HTTPFound(location=redirect_url, headers=headers)
     93                    return HTTPFound(location=redirect_url,
     94                                     headers=request.response.headers)
    8195                else:
    8296                    message = _('Wrong password')
     
    100114@view_config(context=OpenWorkouts, name='logout')
    101115def logout(context, request):
     116    request.response.delete_cookie('_LOCALE_')
    102117    headers = forget(request)
    103     return HTTPFound(location=request.resource_url(context), headers=headers)
     118    request.response.headers.extend(headers)
     119    return HTTPFound(location=request.resource_url(context),
     120                     headers=request.response.headers)
    104121
    105122
     
    319336    renderer='ow:templates/edit_profile.pt')
    320337def edit_profile(context, request):
     338    default_locale = request.registry.settings.get(
     339        'pyramid.default_locale_name')
     340    available_locale_names = get_available_locale_names()
     341    current_locale = request.cookies.get('_LOCALE_', default_locale)
    321342    # if not given a file there is an empty byte in POST, which breaks
    322343    # our blob storage validator.
     
    340361        # reindex
    341362        request.root.reindex(context)
     363        # set the cookie for the locale/lang
     364        request.response.set_cookie('_LOCALE_', form.data['locale'])
     365        current_locale = form.data['locale']
    342366        # Saved, send the user to the public view of her profile
    343         return HTTPFound(location=request.resource_url(context, 'profile'))
     367        return HTTPFound(location=request.resource_url(context, 'profile'),
     368                         headers=request.response.headers)
    344369
    345370    # prevent crashes on the form
     
    348373
    349374    return {'form': OWFormRenderer(form),
    350             'timezones': common_timezones}
     375            'timezones': common_timezones,
     376            'available_locale_names': available_locale_names,
     377            'current_locale': current_locale}
    351378
    352379
Note: See TracChangeset for help on using the changeset viewer.