Changeset 1d92bf2 in OpenWorkouts-current for ow/views


Ignore:
Timestamp:
Dec 16, 2018, 1:07:04 AM (5 years ago)
Author:
borja <borja@…>
Branches:
current, feature/docs, master
Children:
6560b8f
Parents:
929097a
Message:

(#37) Allow login using email address instead of username:

  • Use user uids as keys in the root folder for referencing user objects (instead of username)
  • Use uids for referencing users all over the place (auth, permissions, traversal urls, etc)
  • Replaced the username concept with nickname. This nickname will be used as a shortcut to access "public profile" pages for users
  • Reworked lots of basic methods in the OpenWorkouts root object (s/username/nickname, marked as properties some methods like users, emails, etc)
  • Added new add_user() and delete_user() helpers to the OpenWorkouts root object
  • Fixed bug in the dashboard redirect view, causing an endless loop if an authenticated user does not exist anymore when loading a page.
  • Lots of tests fixes, adaptations and catch up.
Location:
ow/views
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ow/views/root.py

    r929097a r1d92bf2  
    11from pyramid.view import view_config
    2 from pyramid_simpleform import Form
     2from pyramid_simpleform import Form, State
    33from pyramid_simpleform.renderers import FormRenderer
    44from pyramid.httpexceptions import HTTPFound
     
    1818    Show a list of all the users to admins
    1919    """
    20     users = context.users()
     20    users = context.users
    2121    return {'users': users}
    2222
     
    3131    Form to add a user
    3232    """
    33     form = Form(request, schema=UserAddSchema())
     33    state = State(emails=context.lowercase_emails,
     34                  names=context.lowercase_nicknames)
     35
     36    form = Form(request, schema=UserAddSchema(), state=state)
    3437
    3538    if 'submit' in request.POST and form.validate():
    36         uid = request.POST['uid']
    37         user = form.bind(User(), exclude=['uid'])
    38         context[uid] = user
     39        user = form.bind(User())
     40        context[str(user.uid)] = user
    3941        return HTTPFound(location=request.resource_url(context, 'userlist'))
    4042
  • ow/views/user.py

    r929097a r1d92bf2  
    2525    """
    2626    if request.authenticated_userid:
    27         user = request.root.get_user(request.authenticated_userid)
    28         return HTTPFound(location=request.resource_url(user))
     27        user = request.root.get_user_by_uid(request.authenticated_userid)
     28        if user:
     29            return HTTPFound(location=request.resource_url(user))
     30        else:
     31            # an authenticated user session, for an user that does not exist
     32            # anymore, logout!
     33            return HTTPFound(location=request.resource_url(context, 'logout'))
    2934    return HTTPFound(location=request.resource_url(context, 'login'))
    3035
     
    3641def login(context, request):
    3742    message = ''
    38     username = ''
     43    email = ''
    3944    password = ''
    4045    return_to = request.params.get('return_to')
     
    4247
    4348    if 'submit' in request.POST:
    44         username = request.POST.get('username', None)
    45         if username in request.root.all_usernames():
    46             user = request.root[username]
     49        email = request.POST.get('email', None)
     50        user = context.get_user_by_email(email)
     51        if user:
    4752            password = request.POST.get('password', None)
    4853            if password is not None and user.check_password(password):
    49                 headers = remember(request, username)
     54                headers = remember(request, str(user.uid))
     55                redirect_url = return_to or request.resource_url(user)
    5056                return HTTPFound(location=redirect_url, headers=headers)
    5157            else:
    52                 message = u'Bad password'
     58                message = _('Wrong password')
    5359        else:
    54             message = u'Bad username'
     60            message = _('Wrong email address')
    5561
    5662    return {
    5763        'message': message,
    58         'username': username,
     64        'email': email,
    5965        'password': password,
    6066        'redirect_url': redirect_url
     
    7379    renderer='ow:templates/signup.pt')
    7480def signup(context, request):
    75     state = State(emails=context.lowercase_emails(),
    76                   names=context.lowercase_usernames())
     81    state = State(emails=context.lowercase_emails,
     82                  names=context.lowercase_nicknames)
    7783    form = Form(request, schema=SignUpSchema(), state=state)
    7884
    7985    if 'submit' in request.POST and form.validate():
    80         username = request.POST['username']
    81         user = form.bind(User(), exclude=['username', 'password_confirm'])
    82         context[username] = user
     86        user = form.bind(User(), exclude=['password_confirm'])
     87        context.add_user(user)
    8388        # Send to login
    8489        return HTTPFound(location=request.resource_url(context))
Note: See TracChangeset for help on using the changeset viewer.