Esempio n. 1
0
class TownForm(Form):
    """ First step when starting a new town. """

    # the name is limited to 63 characters because it has to fit into a
    # subdomain which may not exceed that length
    name = StringField(
        label=_("Town Name"),
        description=_("The name of your town (real or fictitious)"),
        validators=[validators.InputRequired(),
                    validators.Length(max=63)],
        render_kw={
            'autofocus': '',
            'class_': 'autocomplete',
            'data-source': 'town-names'
        })

    user = EmailField(
        label=_("E-Mail"),
        description=_("Your e-mail address"),
        validators=[validators.InputRequired(),
                    validators.Email()])

    color = ColorField(label=_("Primary Color"),
                       validators=[validators.InputRequired()],
                       default='#005ba1')
Esempio n. 2
0
    def add_town(self, name, user, color, request):
        current_schema = self.app.session_manager.current_schema
        password = random_password(16)

        try:
            schema = self.get_schema(name)
            custom_config = self.config['configuration']

            self.app.session_manager.set_current_schema(schema)
            session = self.app.session_manager.session()

            if session.query(Organisation).first():
                raise AlreadyExistsError

            with self.app.temporary_depot(schema, **custom_config):
                create_new_organisation(self.app, name=name, reply_to=user)

            org = session.query(Organisation).first()
            org.theme_options['primary-color'] = color

            users = UserCollection(self.app.session_manager.session())
            assert not users.query().first()

            users.add(user, password, 'admin')

            title = request.translate(_("Welcome to OneGov Cloud"))
            welcome_mail = render_template(
                'mail_welcome.pt', request, {
                    'url': 'https://{}'.format(self.get_domain(name)),
                    'mail': user,
                    'layout': MailLayout(self, request),
                    'title': title,
                    'org': name
                })

            self.app.es_perform_reindex()
            self.app.send_transactional_email(subject=title,
                                              receivers=(user, ),
                                              content=welcome_mail,
                                              reply_to='*****@*****.**')

        finally:
            self.app.session_manager.set_current_schema(current_schema)

        return {
            'info': [
                (_("Username"), user),
                (_("Password"), password),
            ],
            'url': 'https://{}'.format(self.get_domain(name))
        }
Esempio n. 3
0
    def add_town(self, name, user, color, request):
        current_schema = self.app.session_manager.current_schema
        password = random_password(16)

        try:
            self.app.session_manager.set_current_schema(self.get_schema(name))
            session = self.app.session_manager.session()

            if session.query(Organisation).first():
                raise AlreadyExistsError

            create_new_organisation(self.app, name=name, reply_to=user)

            org = session.query(Organisation).first()
            org.theme_options['primary-color'] = color

            users = UserCollection(self.app.session_manager.session())
            assert not users.query().first()

            users.add(user, password, 'admin')

            title = request.translate(_("Welcome to OneGov Cloud"))
            welcome_mail = render_template('mail_welcome.pt', request, {
                'url': 'https://{}'.format(self.get_domain(name)),
                'mail': user,
                'layout': MailLayout(self, request),
                'title': title,
                'org': name
            })

            self.app.send_email(
                subject=title,
                receivers=(user, ),
                content=welcome_mail,
                reply_to='*****@*****.**'
            )

        finally:
            self.app.session_manager.set_current_schema(current_schema)

        return {
            'info': [
                (_("Username"), user),
                (_("Password"), password),
            ],
            'url': 'https://{}'.format(self.get_domain(name))
        }
Esempio n. 4
0
def view_assistant(self, request, form):

    # usually this is morepath's job, but we're doing some funny things here ;)
    if form is None and request.method == 'POST':
        return HTTPMethodNotAllowed()

    response = self.current_step.handle_view(request, form)

    if isinstance(response, dict):
        assert 'layout' not in response
        response['layout'] = DefaultLayout(self, request)

        if 'form' not in response:
            response['form'] = form

        if self.is_last_step:
            response['button_text'] = _("Launch")
        else:
            response['button_text'] = _("Continue")

    return response
Esempio n. 5
0
def view_assistant(self, request, form):

    # usually this is morepath's job, but we're doing some funny things here ;)
    if form is None and request.method == 'POST':
        return HTTPMethodNotAllowed()

    response = self.current_step.handle_view(request, form)

    if isinstance(response, dict):
        assert 'layout' not in response
        response['layout'] = DefaultLayout(self, request)

        if 'form' not in response:
            response['form'] = form

        if self.is_last_step:
            response['button_text'] = _("Launch")
        else:
            response['button_text'] = _("Continue")

    return response
Esempio n. 6
0
    def first_step(self, request, form):

        if form.submitted(request):
            request.browser_session['name'] = form.data['name']
            request.browser_session['user'] = form.data['user']
            request.browser_session['color'] = form.data['color'].get_hex()

            return morepath.redirect(request.link(self.for_next_step()))

        form.name.data = request.browser_session.get('name', form.name.data)
        form.user.data = request.browser_session.get('user', form.user.data)
        form.color.data = request.browser_session.get('color', form.color.data)

        return {
            'name': 'town-start',
            'title': _("Online Counter for Towns Demo"),
            'bullets': (
                _("Start using the online counter for your town immediately."),
                _("Setup takes less than one minute."),
                _("Free with no commitment.")
            ),
        }
Esempio n. 7
0
    def first_step(self, request, form):

        if form.submitted(request):
            request.browser_session['name'] = form.data['name']
            request.browser_session['user'] = form.data['user']
            request.browser_session['color'] = form.data['color'].get_hex()

            return morepath.redirect(request.link(self.for_next_step()))

        form.name.data = request.browser_session.get('name', form.name.data)
        form.user.data = request.browser_session.get('user', form.user.data)
        form.color.data = request.browser_session.get('color', form.color.data)

        return {
            'name':
            'town-start',
            'title':
            _("Online Counter for Towns Demo"),
            'bullets':
            (_("Start using the online counter for your town immediately."),
             _("Setup takes less than one minute."),
             _("Free with no commitment.")),
        }
Esempio n. 8
0
    def last_step(self, request, form):

        for key in ('name', 'user', 'color'):
            if not request.browser_session.has(key):
                return morepath.redirect(request.link(self.for_prev_step()))

        name = request.browser_session['name']
        user = request.browser_session['user']
        color = request.browser_session['color']

        if form.submitted(request):
            try:
                product = self.add_town(name, user, color, request)
                error = None
            except AlreadyExistsError:
                product = None
                error = _(
                    "This town exists already and can't be created. Is it "
                    "your town but you did not create it? Please contact us.")
            else:
                self.app.send_zulip(
                    subject='OneGov Onboarding',
                    content='\n'.join(
                        (f"A new OneGov Cloud instance was started by {user}:",
                         f"[{name}]({product['url']})")))
            finally:
                del request.browser_session['name']
                del request.browser_session['user']
                del request.browser_session['color']

            if error:
                return {
                    'name': 'town-error',
                    'title': _("Online Counter for Towns Demo"),
                    'error': error,
                    'form': None
                }
            else:
                return {
                    'name':
                    'town-success',
                    'title':
                    _("Online Counter for Towns Demo"),
                    'product':
                    product,
                    'message':
                    _("Success! Have a look at your new website!"),
                    'warning':
                    _("Please write down your username and password "
                      "before you continue. "),
                    'form':
                    None
                }

        return {
            'name':
            'town-ready',
            'title':
            _("Online Counter for Towns Demo"),
            'message':
            _("We are ready to launch! Click continue once you're ready."),
            'preview': {
                'name': name,
                'user': user,
                'domain': self.get_domain(name),
                'color': color
            },
            'cancel':
            request.link(self.for_prev_step())
        }
Esempio n. 9
0
    def last_step(self, request, form):

        for key in ('name', 'user', 'color'):
            if not request.browser_session.has(key):
                return morepath.redirect(request.link(self.for_prev_step()))

        name = request.browser_session['name']
        user = request.browser_session['user']
        color = request.browser_session['color']

        if form.submitted(request):
            try:
                product = self.add_town(name, user, color, request)
                error = None
            except AlreadyExistsError:
                product = None
                error = _(
                    "This town exists already and can't be created. Is it "
                    "your town but you did not create it? Please contact us."
                )
            else:
                self.app.notify_hipchat(
                    (
                        'A new OneGov Cloud instance was started by {}: '
                        '<a href="{}">{}</a>'
                    ).format(user, product['url'], name)
                )
            finally:
                del request.browser_session['name']
                del request.browser_session['user']
                del request.browser_session['color']

            if error:
                return {
                    'name': 'town-error',
                    'title': _("Online Counter for Towns Demo"),
                    'error': error,
                    'form': None
                }
            else:
                return {
                    'name': 'town-success',
                    'title': _("Online Counter for Towns Demo"),
                    'product': product,
                    'message': _("Success! Have a look at your new website!"),
                    'warning': _(
                        "Please write down your username and password "
                        "before you continue. "
                    ),
                    'form': None
                }

        return {
            'name': 'town-ready',
            'title': _("Online Counter for Towns Demo"),
            'message': _(
                "We are ready to launch! Click continue once you're ready."
            ),
            'preview': {
                'name': name,
                'user': user,
                'domain': self.get_domain(name),
                'color': color
            },
            'cancel': request.link(self.for_prev_step())
        }