Example #1
0
        def _display_page(href):
            title = SpaComponents.NOUPDATE
            page = SpaComponents.NOUPDATE

            url = urlsplit(href)

            pathname = re.sub(r"\/$", '', url.path)

            if pathname is None or pathname == '':
                pathname = '/'

            log.info('display_page href=%s', href)

            try:

                if pathname in blueprint_routes:
                    route_ctx = blueprint_routes[pathname]
                    route_ctx.url = url
                else:
                    raise Route404(f'Unknown route {pathname}')

                # If route has access guard in place call it

                if not route_ctx.access or route_ctx.access(route_ctx):
                    page = get_layout(route_ctx)
                    title = route_ctx.title

            except Exception as ex:
                msg = ex.message if hasattr(ex, 'message') else "???"
                log.info('Error pathname %s : %s', pathname, msg)
                page_content = self.show404(message=msg)

            return title, page
Example #2
0
 def _update_graph(tickers):
     global current_tickers
     log.info('_update_graph, stock_ticker_dropdown.input: %s', tickers)
     if tickers is None:
         tickers = current_tickers
     current_tickers = tickers
     return update_graph(tickers)
Example #3
0
            def _location_cb(href):
                nonlocal _value
                log.info('input %s: href=%s (%s)', input.id, href,
                         self.get_pathname())
                qs = self.querystring_args(href)
                if qs and name in qs:
                    _value = qs[name][0]

                log.info('input %s: value %s', input.id, self.value)

                return _value
Example #4
0
    def login(self, email, password, remember):
        log.info('login [email: %s, password: %s, remember: %s]', email,
                 password, remember)

        user = self.User.query.filter_by(email=email).first()

        if not user or not check_password_hash(user.password, password):
            return False

        login_user(user, remember=remember)
        return True
Example #5
0
 def _update_url(tickers):
     log.info('_update_url, stock_ticker_dropdown.input: %s', tickers)
     href = SpaComponents.NOUPDATE
     title = SpaComponents.NOUPDATE
     if tickers is not None:
         href = demo.url_for('ticker')
         urlargs = '+'.join(tickers)
         search = f'?{querystring_name}={urlargs}'
         href += search
         title = ctx.title = 'Ticker: ' + ','.join(tickers)
     return href, title
Example #6
0
    def forgot(self, email):

        log.info('forgot [email: %s]', email)

        user = self.User.query.filter_by(email=email).first(
        )  # if this returns a user, then the email already exists in database

        if user is None:
            return False

        self.code = randomCode(4)
        self.email = email

        log.info('code: %s', self.code)

        mailer = TemplateMailer(FORGOT_TEMPLATE, {'code': self.code})
        mailer.send(mail_options.sender, email, 'Password verification',
                    self.is_test())

        return True
Example #7
0
    def register(self, name, email, password, terms):
        log.info('register [name: %s, email: %s, password: %s, terms: %s]',
                 name, email, password, terms)

        user = self.User.query.filter_by(email=email).first(
        )  # if this returns a user, then the email already exists in database

        if user:
            return False

        verification_record = VerificationRecord(name, email, password)
        self.verification_cache[email] = verification_record

        mailer = TemplateMailer(VERIFICATION_TEMPLATE, {
            'name': name,
            'code': verification_record.code
        })
        mailer.send(mail_options.sender, email, 'Password verification',
                    self.is_test())

        return True
Example #8
0
    def send(self, sender, receiver, subject, test_mode=True):

        # When testing no email is sent. Instead we just print the
        # email that would have been sent

        if test_mode:
            args = locals()
            email = pystache.render(header_template + self.email_msg, args)
            print(
                'Test mode: email send is dissabled.\nThe following email would have been sent:\n'
            )
            print(email)
            return email

        try:
            msg = MIMEText(self.email_msg)
            msg['Subject'] = subject
            msg['From'] = sender
            msg['To'] = receiver

            log.info('sending email to %s ...', receiver)
            with self.smt_transport as server:
                # server.ehlo()
                # server.starttls()
                server.login(options.auth.user, options.auth.password)
                server.send_message(msg)
                log.info('Done')

        except Exception as ex:
            log.info('email send failed %s', ex)
Example #9
0
 def _logout_cb(pathname):
     log.info('_logout_cb pathname=%s', pathname)
     if pathname == admin.url_for('logout'):
         app.login_manager.logout_user()
         return admin.url_for('user.profile')
     return SpaComponents.NOUPDATE
Example #10
0
 def logout_user(self):
     log.info('logout_user')
     logout_user()
Example #11
0
def ticker(ctx):

    log.info('/ticker')

    # http://localhost:8050/finance_explorer?tickers=TSLA

    querystring_name = 'tickers'

    stock_ticker_dropdown = dcc.Dropdown(
        id='stock_ticker',
        value=ctx.get_url_query_values(querystring_name),
        # name=querystring_name,
        options=[{
            'label': s[0],
            'value': str(s[1])
        } for s in zip(df.Stock.unique(), df.Stock.unique())],
        multi=True,
    )

    graphs = html.Div(id='graphs')

    # Callback to create the charts for requested tickers

    @demo.callback(graphs.output.children, [stock_ticker_dropdown.input.value])
    def _update_graph(tickers):
        global current_tickers
        log.info('_update_graph, stock_ticker_dropdown.input: %s', tickers)
        if tickers is None:
            tickers = current_tickers
        current_tickers = tickers
        return update_graph(tickers)

    # Callback to update the browser search-bar with the
    # selected tickers

    location = dhc.Location(id='redirect', refresh=False)
    title = dhc.PageTitle(title=ctx.title, id='ticker_title')

    @demo.callback([location.output.href, title.output.title],
                   [stock_ticker_dropdown.input.value])
    def _update_url(tickers):
        log.info('_update_url, stock_ticker_dropdown.input: %s', tickers)
        href = SpaComponents.NOUPDATE
        title = SpaComponents.NOUPDATE
        if tickers is not None:
            href = demo.url_for('ticker')
            urlargs = '+'.join(tickers)
            search = f'?{querystring_name}={urlargs}'
            href += search
            title = ctx.title = 'Ticker: ' + ','.join(tickers)
        return href, title

    # Layout the page

    return html.Div([
        location, title,
        html.H2('Finance Explorer'),
        html.Br(), stock_ticker_dropdown,
        html.Br(), graphs
    ],
                    className="container")
Example #12
0
 def _cb(clicks):
     red = SpaComponents.NOUPDATE
     log.info('btn clicks=%s', clicks)
     if clicks:
         red = test.url_for('page1')
     return red