コード例 #1
0
 def test_is_boolean_valid(self):
     """Test that is_boolean parses correctly."""
     phrase = ''
     self.assertFalse(is_boolean(phrase))
     phrase = 'terre'
     self.assertFalse(is_boolean(phrase))
     phrase = 'False'
     self.assertTrue(is_boolean(phrase))
     phrase = 'True'
     self.assertTrue(is_boolean(phrase))
     phrase = 'false'
     self.assertTrue(is_boolean(phrase))
     phrase = 'true'
     self.assertTrue(is_boolean(phrase))
コード例 #2
0
 def test_is_boolean_valid(self):
     """Test that is_boolean parses correctly."""
     phrase = ''
     self.assertFalse(is_boolean(phrase))
     phrase = 'terre'
     self.assertFalse(is_boolean(phrase))
     phrase = 'False'
     self.assertTrue(is_boolean(phrase))
     phrase = 'True'
     self.assertTrue(is_boolean(phrase))
     phrase = 'false'
     self.assertTrue(is_boolean(phrase))
     phrase = 'true'
     self.assertTrue(is_boolean(phrase))
コード例 #3
0
ファイル: views.py プロジェクト: akbargumbira/flask_user_map
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
ファイル: views.py プロジェクト: akbargumbira/flask_user_map
def edit_user_controller():
    """Controller to edit a user.

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

    :returns: A new json response containing status of editing
    :rtype: HttpResponse
    """
    # 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
    guid = str(request.form['guid'])
    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'

    # 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

    # Process data
    if len(message) != 0:
        message['type'] = 'Error'
        return Response(json.dumps(message), mimetype='application/json')
    else:
        # Edit User
        guid = edit_user(
            guid=guid,
            name=name,
            email=email,
            website=website,
            role=int(role),
            email_updates=bool(email_updates),
            latitude=float(latitude),
            longitude=float(longitude))

    edited_user = get_user(guid)
    #noinspection PyUnresolvedReferences
    edited_user_json = render_template('json/user.json', user=edited_user)
    #noinspection PyUnresolvedReferences
    edited_user_popup_content = render_template(
        'html/user_info_popup_content.html', user=edited_user
    )
    edited_user_response = dict()
    edited_user_response['edited_user'] = edited_user_json
    edited_user_response['edited_user_popup'] = edited_user_popup_content
    # Return Response
    return Response(
        json.dumps(edited_user_response),
        mimetype='application/json')
コード例 #5
0
ファイル: views.py プロジェクト: kriwil/id-python-members
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')
コード例 #6
0
ファイル: views.py プロジェクト: kriwil/id-python-members
def edit_user_controller():
    """Controller to edit a user.

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

    :returns: A new json response containing status of editing
    :rtype: HttpResponse
    """
    # 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
    guid = str(request.form['guid'])
    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'

    # 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

    # Process data
    if len(message) != 0:
        message['type'] = 'Error'
        return Response(json.dumps(message), mimetype='application/json')
    else:
        # Edit User
        guid = edit_user(guid=guid,
                         name=name,
                         email=email,
                         website=website,
                         role=int(role),
                         email_updates=bool(email_updates),
                         latitude=float(latitude),
                         longitude=float(longitude))

    edited_user = get_user(guid)
    #noinspection PyUnresolvedReferences
    edited_user_json = render_template('json/user.json', user=edited_user)
    #noinspection PyUnresolvedReferences
    edited_user_popup_content = render_template(
        'html/user_info_popup_content.html', user=edited_user)
    edited_user_response = dict()
    edited_user_response['edited_user'] = edited_user_json
    edited_user_response['edited_user_popup'] = edited_user_popup_content
    # Return Response
    return Response(json.dumps(edited_user_response),
                    mimetype='application/json')