Changeset 8bae554 in OpenWorkouts-current for ow/views


Ignore:
Timestamp:
Feb 21, 2019, 9:54:45 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
d411dae
Parents:
38171c6
Message:

(#67) Allow users to send again the verification link (up to 3 times)
to the email address they provided when signing up.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/views/user.py

    r38171c6 r8bae554  
    5050    renderer='ow:templates/login.pt')
    5151def login(context, request):
    52     message = ''
    53     email = ''
     52    # messages is a dict of pre-defined messages we would need to show to the
     53    # user when coming back to the login page after certain actions
     54    messages = {
     55        'already-verified': _('User has been verified already'),
     56        'link-sent': _('Verification link sent, please check your inbox'),
     57        'max-tokens-sent': _(
     58            'We already sent you the verification link more than three times')
     59    }
     60    message = request.GET.get('message', '')
     61    if message:
     62        message = messages.get(message, '')
     63    email = request.GET.get('email', '')
    5464    password = ''
    5565    return_to = request.params.get('return_to')
    5666    redirect_url = return_to or request.resource_url(request.root)
     67    # If the user still has to verify the account, this will be set to the
     68    # proper link to re-send the verification email
     69    resend_verify_link = None
    5770
    5871    if 'submit' in request.POST:
     
    7083            else:
    7184                message = _('You have to verify your account first')
     85                resend_verify_link = request.resource_url(
     86                    user, 'resend-verification-link'
     87                )
    7288        else:
    7389            message = _('Wrong email address')
     
    7793        'email': email,
    7894        'password': password,
    79         'redirect_url': redirect_url
     95        'redirect_url': redirect_url,
     96        'resend_verify_link': resend_verify_link
    8097    }
    8198
     
    103120        # send a verification link to the user email address
    104121        send_verification_email(request, user)
     122        user.verification_tokens_sent += 1
    105123        # Send to login
    106124        return HTTPFound(location=request.resource_url(context))
     
    135153    # if we can not verify the user, show a page with some info about it
    136154    return {}
     155
     156
     157@view_config(
     158    context=User,
     159    name="resend-verification-link")
     160def resend_verification_link(context, request):
     161    """
     162    Send an email with the verification link, only if the user has not
     163    been verified yet
     164    """
     165    # the message to be shown when the user gets back to the login page
     166    query = {'message': 'already-verified'}
     167    if not context.verified:
     168        tokens_sent = getattr(context, 'verification_tokens_sent', 0)
     169        if tokens_sent > 3:
     170            # we already sent the token 3 times, we don't send it anymore
     171            query = {'message': 'max-tokens-sent', 'email': context.email}
     172        else:
     173            if context.verification_token is None:
     174                # for some reason the verification token is not there, get one
     175                context.verification_token = get_verification_token()
     176            send_verification_email(request, context)
     177            context.verification_tokens_sent = tokens_sent + 1
     178            query = {'message': 'link-sent', 'email': context.email}
     179    # Send to login
     180    url = request.resource_url(request.root, 'login', query=query)
     181    return HTTPFound(location=url)
    137182
    138183
Note: See TracChangeset for help on using the changeset viewer.