def service_name_change_confirm(service_id):
    if 'service_name_change' not in session:
        flash("The change you made was not saved. Please try again.", 'error')
        return redirect(
            url_for('main.service_name_change', service_id=service_id))

    # Validate password for form
    def _check_password(pwd):
        return user_api_client.verify_password(current_user.id, pwd)

    form = ConfirmPasswordForm(_check_password)

    if form.validate_on_submit():
        try:
            current_service.update(name=session['service_name_change'],
                                   email_from=email_safe(
                                       session['service_name_change']))
        except HTTPError as e:
            error_msg = "Duplicate service name '{}'".format(
                session['service_name_change'])
            if e.status_code == 400 and error_msg in e.message['name']:
                # Redirect the user back to the change service name screen
                flash('This service name is already in use', 'error')
                return redirect(
                    url_for('main.service_name_change', service_id=service_id))
            else:
                raise e
        else:
            session.pop('service_name_change')
            return redirect(url_for('.service_settings',
                                    service_id=service_id))
    return render_template('views/service-settings/confirm.html',
                           heading='Change your service name',
                           form=form)
def service_name_change(service_id):
    form = RenameServiceForm()

    if request.method == 'GET':
        form.name.data = current_service.name

    if form.validate_on_submit():

        if form.name.data == current_service.name:
            return redirect(url_for('.service_settings',
                                    service_id=service_id))

        unique_name = service_api_client.is_service_name_unique(
            service_id, form.name.data, email_safe(form.name.data))

        if not unique_name:
            form.name.errors.append("This service name is already in use")
            return render_template('views/service-settings/name.html',
                                   form=form)

        session['service_name_change'] = form.name.data
        return redirect(
            url_for('.service_name_change_confirm', service_id=service_id))

    if current_service.organisation_type == 'local':
        return render_template(
            'views/service-settings/name-local.html',
            form=form,
        )

    return render_template(
        'views/service-settings/name.html',
        form=form,
    )
Пример #3
0
def add_service():
    default_organisation_type = current_user.default_organisation_type
    if default_organisation_type == 'nhs':
        form = CreateNhsServiceForm()
        default_organisation_type = None
    else:
        form = CreateServiceForm(organisation_type=default_organisation_type)
    heading = 'About your service'

    if form.validate_on_submit():
        email_from = email_safe(form.name.data)
        service_name = form.name.data
        service_description = form.service_description.data

        service_id, error = _create_service(
            service_name,
            service_description,
            default_organisation_type or form.organisation_type.data,
            email_from,
            form,
        )
        if error:
            return render_template('views/add-service.html',
                                   form=form,
                                   heading=heading)
        if len(
                service_api_client.get_active_services({
                    'user_id':
                    session['user_id']
                }).get('data', [])) > 1:
            return redirect(
                url_for('main.service_dashboard', service_id=service_id))

        example_sms_template = _create_example_template(service_id)

        return redirect(
            url_for('main.begin_tour',
                    service_id=service_id,
                    template_id=example_sms_template['data']['id']))
    else:
        if default_organisation_type == 'local':
            return render_template(
                'views/add-service-local.html',
                form=form,
                heading=heading,
                default_organisation_type=default_organisation_type,
            )

        return render_template(
            'views/add-service.html',
            form=form,
            heading=heading,
            default_organisation_type=default_organisation_type,
        )
Пример #4
0
def create_api_key(service_id):
    form = CreateKeyForm(current_service.api_keys)
    form.key_type.choices = [
        (KEY_TYPE_NORMAL, 'Live – sends to anyone'),
        (KEY_TYPE_TEAM, 'Team and guest list – limits who you can send to'),
        (KEY_TYPE_TEST, 'Test – pretends to send messages'),
    ]
    # preserve order of items extended by starting with empty dicts
    form.key_type.param_extensions = {'items': [{}, {}]}
    if current_service.trial_mode:
        form.key_type.param_extensions['items'][0] = {
            'disabled': True,
            'hint': {
                'html':
                Markup(
                    'Not available because your service is in '
                    '<a class="govuk-link govuk-link--no-visited-state" href="/features/trial-mode">trial mode</a>'
                )
            }
        }
    if current_service.has_permission('letter'):
        form.key_type.param_extensions['items'][1]['hint'] = {
            'text': 'Cannot be used to send letters'
        }
    if form.validate_on_submit():
        if current_service.trial_mode and form.key_type.data == KEY_TYPE_NORMAL:
            abort(400)
        secret = api_key_api_client.create_api_key(service_id=service_id,
                                                   key_name=form.key_name.data,
                                                   key_type=form.key_type.data)
        return render_template('views/api/keys/show.html',
                               secret=secret,
                               service_id=service_id,
                               key_name=email_safe(form.key_name.data,
                                                   whitespace='_'))
    return render_template('views/api/keys/create.html', form=form)
def test_email_safe_return_dot_separated_email_domain(service_name,
                                                      safe_email):
    assert email_safe(service_name) == safe_email