Пример #1
0
def reminder_view():
    """View to send reminder email to user.

    :returns: JSON Response containing status of the process
    :rtype: JSONResponse
    """
    message = dict()

    email = str(request.form['email']).strip()
    user = get_user_by_email(email)

    if user is None:
        message['type'] = 'Error'
        message['email'] = 'Email is not registered in our database.'
        return Response(json.dumps(message), mimetype='application/json')

    # Send Email Confirmation:
    subject = '%s - User Map Edit Link' % APP.config['PROJECT_NAME']
    #noinspection PyUnresolvedReferences
    body = render_template(
        'text/registration_confirmation_email.txt',
        project_name=APP.config['PROJECT_NAME'],
        url=APP.config['PUBLIC_URL'],
        user=user)
    send_async_mail(
        sender=MAIL_ADMIN,
        recipients=[email],
        subject=subject,
        text_body=body, html_body='')

    message['type'] = 'Success'
    return Response(json.dumps(message), mimetype='application/json')
Пример #2
0
def reminder_view():
    """View to send reminder email to user.

    :returns: JSON Response containing status of the process
    :rtype: JSONResponse
    """
    message = dict()

    email = str(request.form['email']).strip()
    user = get_user_by_email(email)

    if user is None:
        message['type'] = 'Error'
        message['email'] = 'Email is not registered in our database.'
        return Response(json.dumps(message), mimetype='application/json')

    # Send Email Confirmation:
    subject = '%s - User Map Edit Link' % APP.config['PROJECT_NAME']
    #noinspection PyUnresolvedReferences
    body = render_template('text/registration_confirmation_email.txt',
                           project_name=APP.config['PROJECT_NAME'],
                           url=APP.config['PUBLIC_URL'],
                           user=user)
    send_async_mail(sender=MAIL_ADMIN,
                    recipients=[email],
                    subject=subject,
                    text_body=body,
                    html_body='')

    message['type'] = 'Success'
    return Response(json.dumps(message), mimetype='application/json')
Пример #3
0
def add_user_view():
    """Controller to add a user.

    Handle post request via ajax and add the user to the user.db

    :returns: A new json response as in users.json.
    :rtype: HttpResponse

    .. note:: JavaScript on client must update the map on ajax completion
        callback.
    """
    # return any errors as json - see http://flask.pocoo.org/snippets/83/
    for code in default_exceptions.iterkeys():
        APP.error_handler_spec[None][code] = make_json_error

    # Get data from form
    name = str(request.form['name']).strip()
    email = str(request.form['email']).strip()
    website = str(request.form['website'])
    role = int(request.form['role'])
    email_updates = str(request.form['email_updates'])
    latitude = str(request.form['latitude'])
    longitude = str(request.form['longitude'])

    # Validate the data:
    message = {}
    if not is_required_valid(name):
        message['name'] = 'Name is required'
    if not is_email_address_valid(email):
        message['email'] = 'Email address is not valid'
    if not is_required_valid(email):
        message['email'] = 'Email is required'
    if role not in [0, 1, 2]:
        message['role'] = 'Role must be checked'
    elif not is_boolean(email_updates):
        message['email_updates'] = 'Notification must be checked'

    # Check if the email has been registered by other user:
    user = get_user_by_email(email)
    if user is not None:
        message['email'] = 'Email has been registered by other user.'

    # Process data
    if len(message) != 0:
        message['type'] = 'Error'
        return Response(json.dumps(message), mimetype='application/json')
    else:
        # Modify the data:
        if email_updates == 'true':
            email_updates = True
        else:
            email_updates = False

        if len(website.strip()) != 0 and 'http' not in website:
            website = 'http://%s' % website

        # Create model for user and add user
        guid = add_user(
            name=name,
            email=email,
            website=website,
            role=int(role),
            email_updates=bool(email_updates),
            latitude=float(latitude),
            longitude=float(longitude))

    # Prepare json for added user
    added_user = get_user(guid)

    # Send Email Confirmation:
    subject = '%s User Map Registration' % APP.config['PROJECT_NAME']
    #noinspection PyUnresolvedReferences
    body = render_template(
        'text/registration_confirmation_email.txt',
        project_name=APP.config['PROJECT_NAME'],
        url=APP.config['PUBLIC_URL'],
        user=added_user)
    recipient = added_user['email']
    send_async_mail(
        sender=MAIL_ADMIN,
        recipients=[recipient],
        subject=subject,
        text_body=body,
        html_body='')

    #noinspection PyUnresolvedReferences
    added_user_json = render_template('json/users.json', users=[added_user])
    # Return Response
    return Response(added_user_json, mimetype='application/json')
Пример #4
0
def add_user_view():
    """Controller to add a user.

    Handle post request via ajax and add the user to the user.db

    :returns: A new json response as in users.json.
    :rtype: HttpResponse

    .. note:: JavaScript on client must update the map on ajax completion
        callback.
    """
    # return any errors as json - see http://flask.pocoo.org/snippets/83/
    for code in default_exceptions.iterkeys():
        APP.error_handler_spec[None][code] = make_json_error

    # Get data from form
    name = str(request.form['name']).strip()
    email = str(request.form['email']).strip()
    website = str(request.form['website'])
    role = int(request.form['role'])
    email_updates = str(request.form['email_updates'])
    latitude = str(request.form['latitude'])
    longitude = str(request.form['longitude'])

    # Validate the data:
    message = {}
    if not is_required_valid(name):
        message['name'] = 'Name is required'
    if not is_email_address_valid(email):
        message['email'] = 'Email address is not valid'
    if not is_required_valid(email):
        message['email'] = 'Email is required'
    if role not in [0, 1, 2]:
        message['role'] = 'Role must be checked'
    elif not is_boolean(email_updates):
        message['email_updates'] = 'Notification must be checked'

    # Check if the email has been registered by other user:
    user = get_user_by_email(email)
    if user is not None:
        message['email'] = 'Email has been registered by other user.'

    # Process data
    if len(message) != 0:
        message['type'] = 'Error'
        return Response(json.dumps(message), mimetype='application/json')
    else:
        # Modify the data:
        if email_updates == 'true':
            email_updates = True
        else:
            email_updates = False

        if len(website.strip()) != 0 and 'http' not in website:
            website = 'http://%s' % website

        # Create model for user and add user
        guid = add_user(name=name,
                        email=email,
                        website=website,
                        role=int(role),
                        email_updates=bool(email_updates),
                        latitude=float(latitude),
                        longitude=float(longitude))

    # Prepare json for added user
    added_user = get_user(guid)

    # Send Email Confirmation:
    subject = '%s User Map Registration' % APP.config['PROJECT_NAME']
    #noinspection PyUnresolvedReferences
    body = render_template('text/registration_confirmation_email.txt',
                           project_name=APP.config['PROJECT_NAME'],
                           url=APP.config['PUBLIC_URL'],
                           user=added_user)
    recipient = added_user['email']
    send_async_mail(sender=MAIL_ADMIN,
                    recipients=[recipient],
                    subject=subject,
                    text_body=body,
                    html_body='')

    #noinspection PyUnresolvedReferences
    added_user_json = render_template('json/users.json', users=[added_user])
    # Return Response
    return Response(added_user_json, mimetype='application/json')
Пример #5
0
 def test_get_user_by_email(self):
     """Test for getting user function."""
     guid = add_user(**self.user_to_add)
     self.assertIsNotNone(guid)
     user = get_user_by_email(self.user_to_add['email'])
     self.assertEqual('Akbar', user['name'])
Пример #6
0
 def test_get_user_by_email(self):
     """Test for getting user function."""
     guid = add_user(**self.user_to_add)
     self.assertIsNotNone(guid)
     user = get_user_by_email(self.user_to_add['email'])
     self.assertEqual('Akbar', user['name'])