Esempio n. 1
0
def google_authorized(resp, est):
    if resp is None:
        msg.flash(
            'Access denied: reason=%s error=%s' %
            (request.args['error_reason'], request.args['error_description']))
        return redirect(url_for('index'))
    if str(type(resp)) == "<class 'flask_oauthlib.client.OAuthException'>":
        msg.flash('Access denied: desc=%s error=%s' %
                  (resp.data['error_description'], resp.data['error']))
        return redirect(url_for('index'))
    session['google_token'] = (resp['access_token'], '')
    person = google.get('userinfo')
    # person.data = {
    #     u'family_name': last_name,
    #     u'name': full_name,
    #     u'picture': url,
    #     u'gender': u'male' or u'female',
    #     u'email': email_addr,
    #     u'link': google_plus_url,
    #     u'given_name': first_name,
    #     u'id': u'101149719268028298009',
    #     u'hd': domain_name,
    #     u'verified_email': True  }
    session.pop('_flashes', None)
    email = person.data[u'email']
    authid = person.data[u'id']
    # picture_url = person.data[u'picture']
    if est == "new":
        user = database.create_user_byOAuth(email, authid, "google")
        if msg.is_bad(user):
            msg.flash(user)
            return redirect(url_for('index'))
        account = account__database.create_account(user, person.data[u'name'])
        if msg.is_bad(account):
            msg.flash(account)
            return redirect(url_for('index'))
        login_user(user)
        msg.flash(
            msg.success(
                'Welcome, your name has been determined to be <b>{}</b>'.
                format(account.s_name),
                return_def="index"))
        return redirect(url_for('index'))
    user = database.read_user_byOAuth(email, authid, "google")
    if msg.is_bad(user):
        msg.flash(user)
        return redirect(url_for('index'))
    login_user(user)
    msg.flash(msg.success('Welcome back.', return_def="index"))
    return redirect(url_for('index'))
Esempio n. 2
0
def page_handler(page, source_def, key, **kwargs):
    if msg.is_bad(page.status):
        msg.flash(page.status)
        return redirect(url_for('index'))
    if page.admin_required:
        if not g.admin_flag:
            msg.flash('You must be an administrator.',
                      t="warning",
                      log_level=logging.WARNING)
            return redirect(url_for('index'))
    if page.login_required:
        if not current_user.is_authenticated:
            msg.flash('You must be logged in.',
                      t="warning",
                      log_level=logging.INFO)
            return redirect(url_for('index'))
    #
    # handle purposeful bypass
    #
    if hasattr(page, "fondum_bypass"):
        return page.fondum_bypass(**kwargs)
    #
    # form handling
    #
    if page.wtf:
        if page.wtf.is_submitted():
            if page.wtf.validate_on_submit():
                if not hasattr(page.wtf, "process_form"):
                    msg.flash('FONDUM error: no process_form method found',
                              t="bug")
                    return redirect(url_for(source_def))
                result = page.wtf.process_form(page.wtf, **kwargs)
                if isinstance(result, Response):
                    return result
                msg.flash(result)
                if result.return_def:
                    if result.return_def_parms:
                        return redirect(
                            url_for(result.return_def,
                                    **result.return_def_parms))
                    return redirect(url_for(result.return_def))
                return redirect(url_for(source_def, **kwargs))
            else:
                if hasattr(page.wtf, 'set_field_values'):
                    result = page.wtf.set_field_values(False, **kwargs)
                    if msg.is_msg(result):
                        msg.flash(result)
                        if result.return_def:
                            if result.return_def_parms:
                                return redirect(
                                    url_for(result.return_def,
                                            **result.return_def_parms))
                            return redirect(url_for(result.return_def))
        else:
            if hasattr(page.wtf, 'set_field_values'):
                result = page.wtf.set_field_values(True, **kwargs)
                if msg.is_msg(result):
                    msg.flash(result)
                    if result.return_def:
                        if result.return_def_parms:
                            return redirect(
                                url_for(result.return_def,
                                        **result.return_def_parms))
                        return redirect(url_for(result.return_def))
    #
    # generate html
    #
    article = database.read_article_byKey(key)
    html = parsing.generate_html(article, page)
    #
    #
    # logger.debug("Served Page /{}/".format(key))
    return render_template('page.html', page=page, key=key, html=html)
Esempio n. 3
0
def delete_product(key):
    product = read_product(key)
    if msg.is_bad(product):
        return msg.err("Product {} missing; possibly already deleted.".format(key))
    product.delete()
    return msg.success("Product {} deleted.".format(product.s_key))
Esempio n. 4
0
def upsert_article(key, wtf):
    article = read_article_byKey(key)
    if msg.is_bad(article):
        return create_article(key, wtf)
    return update_article(article, wtf)
Esempio n. 5
0
def load_user(user_id):
    user = database.read_user(user_id)
    if msg.is_bad(user):
        g.user.is_authenticated = False
    return user