def submit_single_signup():
    user = from_response(request)
    if user['user_type'] != 'buyer':
        return abort(400)

    fields = ['name', 'email_address', 'employment_status', 'user_type']
    errors = validate_form_data(user, fields)
    if not is_government_email(user['email_address']):
        errors['email_address'] = {"government_email": True}
    if errors:
        return single_signup(user, errors)

    if user['employment_status'] == 'employee':
        token = generate_buyer_creation_token(user['name'],
                                              user['email_address'])
        send_buyer_account_activation_email(
            name=user["name"],
            email_address=user["email_address"],
            token=token)
        return render_template('auth/buyer-signup-email-sent.html',
                               email_address=user["email_address"])

    assert user['employment_status'] == 'contractor'

    form = auth_forms.BuyerInviteRequestForm(request.form)
    return render_template_with_csrf('auth/buyer-invite-request.html',
                                     form=form)
def application_update(id, step=None):
    old_application = data_api_client.get_application(id)
    if not can_user_view_application(old_application):
        abort(403, 'Not authorised to access application')
    if is_application_submitted(old_application) and not current_user.has_role('admin'):
        return redirect(url_for('.submit_application', id=id))

    json = request.content_type == 'application/json'
    form_data = from_response(request)
    application = form_data['application'] if json else form_data

    try:
        del application['status']
    except KeyError:
        pass

    result = data_api_client.update_application(id, application)

    if json:
        try:
            appdata = result['application']
            del appdata['links']
        except KeyError:
            pass
        return jsonify(result)
    else:
        return redirect(url_for('.render_application', id=id, step=form_data['next_step_slug']))
def application_update(id, step=None):
    old_application = data_api_client.get_application(id)
    if not can_user_view_application(old_application):
        abort(403, 'Not authorised to access application')
    if is_application_submitted(
            old_application) and not current_user.has_role('admin'):
        return redirect(url_for('.submit_application', id=id))

    json = request.content_type == 'application/json'
    form_data = from_response(request)
    application = form_data['application'] if json else form_data

    try:
        del application['status']
    except KeyError:
        pass

    result = data_api_client.update_application(id, application)

    if json:
        try:
            appdata = result['application']
            del appdata['links']
        except KeyError:
            pass
        return jsonify(result)
    else:
        return redirect(
            url_for('.render_application',
                    id=id,
                    step=form_data['next_step_slug']))
 def test_extract_form_response(self):
     data = MultiDict([('a', '1'), ('b[]', '2'), ('b[]', '3'), ("c.d", '4')])
     with self.flask.test_request_context('/test', method='POST',
                                          data=data):
         response_data = from_response(request)
         assert 'a' in response_data
         assert response_data['a'] == data['a']
         assert 'b' in response_data
         assert 'b[]' not in response_data
         assert response_data['b'] == ['2', '3']
         assert 'c' in response_data
         assert 'd' in response_data['c']
         assert response_data['c']['d'] == '4'
 def test_extract_json_response(self):
     data = MultiDict([('a', '1'), ('b[]', '2'), ('b[]', '3'), ("c.d", '4')])
     data_json = '{"a": "1", "b": ["2","3"], "c": {"d": "4"}}'
     with self.flask.test_request_context('/test', method='POST',
                                          data=data_json, content_type="application/json"):
         response_data = from_response(request)
         assert 'a' in response_data
         assert response_data['a'] == data['a']
         assert 'b' in response_data
         assert 'b[]' not in response_data
         assert response_data['b'] == ['2', '3']
         assert 'c' in response_data
         assert 'd' in response_data['c']
         assert response_data['c']['d'] == '4'
def create_application(token):
    token_data = decode_user_token(token.encode())
    form_data = from_response(request)

    fields = [('password', 10)]
    errors = validate_form_data(form_data, fields)
    if errors:
        return render_create_application(token, form_data, errors)

    id = token_data.get('id')
    create_app = not id

    if create_app:
        application = data_api_client.create_application({
            'status':
            'saved',
            'framework':
            'digital-marketplace'
        })
        id = application['application']['id']

    user = data_api_client.create_user({
        'name':
        token_data['name'],
        'password':
        form_data['password'],
        'emailAddress':
        token_data['email_address'],
        'role':
        'applicant',
        'application_id':
        id
    })

    user = User.from_json(user)
    login_user(user)

    if create_app:
        notification_message = 'Application Id:{}\nBy: {} ({})'.format(
            id, token_data['name'], token_data['email_address'])

        notify_team('A new seller has started an application',
                    notification_message)

    return redirect(
        url_for('main.render_application',
                id=id,
                step=('start' if create_app else 'submit')))
def send_seller_signup_email():
    user = from_response(request)
    if user.get('user_type') != 'seller':
        return abort(400)

    fields = ['name', 'email_address']
    errors = validate_form_data(user, fields)
    if errors:
        return start_seller_signup(user, errors)

    duplicate = data_api_client.req.users().checkduplicates().post(data={"email_address": user['email_address']})
    if duplicate.get('duplicate'):
        return render_template('auth/seller-signup-email-sent.html', email_address=user['email_address'])

    token = generate_application_invitation_token(user)
    url = url_for('main.render_create_application', token=token, _external=True)
    email_body = render_template(
        'emails/create_seller_user_email.html',
        url=url,
        user=user,
    )

    try:
        send_email(
            user['email_address'],
            email_body,
            current_app.config['INVITE_EMAIL_SUBJECT'],
            current_app.config['INVITE_EMAIL_FROM'],
            current_app.config['INVITE_EMAIL_NAME']
        )
    except EmailError as e:
        rollbar.report_exc_info()
        current_app.logger.error(
            'Invitation email failed to send. '
            'error {error}',
            extra={'error': six.text_type(e)}
        )
        abort(503, 'Failed to send user invite reset')

    return render_template('auth/seller-signup-email-sent.html', email_address=user['email_address'])
def create_application(token):
    token_data = decode_user_token(token.encode())
    form_data = from_response(request)

    fields = [('password', 10)]
    errors = validate_form_data(form_data, fields)
    if errors:
        return render_create_application(token, form_data, errors)

    id = token_data.get('id')
    create_app = not id

    if create_app:
        application = data_api_client.create_application({'status': 'saved', 'framework': 'digital-marketplace'})
        id = application['application']['id']

    user = data_api_client.create_user({
        'name': token_data['name'],
        'password': form_data['password'],
        'emailAddress': token_data['email_address'],
        'role': 'applicant',
        'application_id': id
    })

    user = User.from_json(user)
    login_user(user)

    if create_app:
        notification_message = 'Application Id:{}\nBy: {} ({})'.format(
            id,
            token_data['name'],
            token_data['email_address']
        )

        notify_team('A new seller has started an application', notification_message)

    return redirect(url_for('main.render_application', id=id, step=('start' if create_app else 'submit')))
Exemplo n.º 9
0
def collaborate_create_project_submit():
    project = from_response(request)

    fields = ['title', 'client', 'stage']

    basename = url_for('.collaborate_create_project')
    errors = validate_form_data(project, fields)
    if errors:
        form = DmForm()
        rendered_component = render_component('bundles/Collaborate/ProjectFormWidget.js', {
            'form_options': {
                'csrf_token': form.csrf_token.current_token,
                'errors': errors
            },
            'projectForm': project,
            'basename': basename
        })

        return render_template(
            '_react.html',
            breadcrumb_items=[
                {'link': url_for('main.index'), 'label': 'Home'},
                {'link': url_for('main.collaborate'), 'label': 'Collaborate'},
                {'label': 'Add project'}
            ],
            component=rendered_component
        )

    try:
        project = data_api_client.req.projects().post(data={'project': project})['project']

        rendered_component = render_component('bundles/Collaborate/ProjectSubmitConfirmationWidget.js', {})

        return render_template(
            '_react.html',
            breadcrumb_items=[
                {'link': url_for('main.index'), 'label': 'Home'},
                {'link': url_for('main.collaborate'), 'label': 'Collaborate'},
                {'label': 'Add project'}
            ],
            component=rendered_component
        )

    except APIError as e:
        form = DmForm()
        flash('', 'error')
        rendered_component = render_component('bundles/Collaborate/ProjectFormWidget.js', {
            'form_options': {
                'csrf_token': form.csrf_token.current_token
            },
            'projectForm': project,
            'basename': basename
        })

        return render_template(
            '_react.html',
            breadcrumb_items=[
                {'link': url_for('main.index'), 'label': 'Home'},
                {'link': url_for('main.collaborate'), 'label': 'Collaborate'},
                {'label': 'Add project'}
            ],
            component=rendered_component
        )
def collaborate_create_project_submit():
    project = from_response(request)

    fields = ['title', 'client', 'stage']

    basename = url_for('.collaborate_create_project')
    errors = validate_form_data(project, fields)
    if errors:
        form = DmForm()
        rendered_component = render_component('bundles/Collaborate/ProjectFormWidget.js', {
            'form_options': {
                'csrf_token': form.csrf_token.current_token,
                'errors': errors
            },
            'projectForm': project,
            'basename': basename
        })

        return render_template(
            '_react.html',
            breadcrumb_items=[
                {'link': url_for('main.index'), 'label': 'Home'},
                {'link': url_for('main.collaborate'), 'label': 'Collaborate'},
                {'label': 'Add project'}
            ],
            component=rendered_component
        )

    try:
        project = data_api_client.req.projects().post(data={'project': project})['project']

        rendered_component = render_component('bundles/Collaborate/ProjectSubmitConfirmationWidget.js', {})

        return render_template(
            '_react.html',
            breadcrumb_items=[
                {'link': url_for('main.index'), 'label': 'Home'},
                {'link': url_for('main.collaborate'), 'label': 'Collaborate'},
                {'label': 'Add project'}
            ],
            component=rendered_component
        )

    except APIError as e:
        form = DmForm()
        flash('', 'error')
        rendered_component = render_component('bundles/Collaborate/ProjectFormWidget.js', {
            'form_options': {
                'csrf_token': form.csrf_token.current_token
            },
            'projectForm': project,
            'basename': basename
        })

        return render_template(
            '_react.html',
            breadcrumb_items=[
                {'link': url_for('main.index'), 'label': 'Home'},
                {'link': url_for('main.collaborate'), 'label': 'Collaborate'},
                {'label': 'Add project'}
            ],
            component=rendered_component
        )