<commit_before>from __future__ import print_function
from app import app
from flask import render_template, request, jsonify, redirect, url_for
from flask import g
from rates_controller import get_current_rates, update_rates
from user_controller import add_user, change_info_view, update_user_nickname
from controller import get_daily_summary
from transaction_controller import get_recent_transactions, get_filtered_summary
from transaction_controller import execute_transaction, get_sum_category_per_day
from datetime import datetime, timedelta
from flask.ext.login import login_user, logout_user, current_user
from models import User
from app import lm, oid
from utilities import execute_git_log
from oauth import OAuthSignIn

import logging
from app import setup_logger
logger = logging.getLogger(__name__)
setup_logger(logger)
logger.setLevel(logging.INFO)


@app.before_request
def before_request_happens():
    g.user = current_user

    if g.user.is_anonymous():
        user = User.query.filter_by(email='<EMAIL>').first()
        g.user = user

    remote_address = request.remote_addr

    if app.debug or (not app.debug and remote_address != '127.0.0.1'):
        logger.info('NEW REQUEST: current user is: {0}.  From Ip: {1}'.format(g.user, remote_address))

    g.commit = execute_git_log()


#####
# Functions for handling user login:
#
# ----> This needs to be completely replaced because google does not like openid anymore.
# ----> Attempting to work off of this guy: http://psa.matiasaguirre.net/docs/configuration/flask.html
#####
@app.route('/authorize/<provider>')
def oauth_authorize(provider):
    if not current_user.is_anonymous():
        return redirect(url_for('build_index'))
    oauth = OAuthSignIn.get_provider(provider)
    return oauth.authorize()


@app.route('/callback/<provider>')
def oauth_callback(provider):
    if not current_user.is_anonymous():
        return redirect(url_for('build_index'))

    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()

    if social_id is None:
        logger.error('Authentication failed.')
        return redirect(url_for('build_index'))

    user = User.query.filter_by(social_id=social_id).first()

    if not user:
        user = add_user(social_id, username, email)

    if user:
        login_user(user, True)

    return redirect(url_for('build_index'))



@lm.user_loader
def load_user(u_id):
    return User.query.get(int(u_id))


@app.route('/login_user', methods=['GET', 'POST'])
@oid.loginhandler
def login_user_function():

    logger.info('I am attempting to login now...')

    try:
        url = request.form["url"]
    except Exception, e:

        return jsonify({'there were so many errors': str(e)})

    logger.info('getting oid results now, with url: {0}'.format(url))
    oid_results = oid.try_login(url, ask_for=['nickname', 'email'])

    return oid_results


@oid.after_login
def after_login_function(resp):

    logger.info('running after_login function now...')

    # user = g.user

    if resp.email is None or resp.email == "":
        logger.warn('Invalid login. Please try again.')
        return redirect(url_for('build_index'))

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

    # if totally new user:
    # if user is None or user.is_authenticated() is False:
    if user is None:
        logger.info('response: {0}'.format(resp))
        add_user(resp)
        user = User.query.filter_by(email=resp.email).first()

    logger.info('Now attempting to log in now: {0}'.format(user))

    login_user(user, remember=True)

    return redirect(request.args.get('next') or url_for('build_index'))


@app.route('/logout')
def logout_view():
    logout_user()
    logger.info("successful logout for user: {0}".format(g.user))

    # rates = get_current_rates()
    # transactions = get_recent_transactions()
    # return render_template('index.html', title='Insolvency_Solver',
    #                        rates=rates, transactions=transactions)
    return jsonify({'message': 'look how logged out you are!'})


# let's put all of this rates crap in an app.context_processor as available functions
# then change the templates to just use those when they need them.
#
#
@app.context_processor
def template_functions():

    user = g.user

    # rates and transactions should get placed in a context processor...
    # probably the same with our commit object?
    rates = get_current_rates(user)
    rates['monthly_balance'] = rates['income_per_month'] - (rates['rent'] + rates['bills'] + rates['other_costs'])
    rates['max_spending'] = rates['monthly_balance']/30

    transactions = get_recent_transactions(user)
    transaction_categories = list(set([t['purchase_type'] for t in transactions]))
    transaction_categories.sort()

    return dict(rates=rates, transactions=transactions, u=user, transaction_categories=transaction_categories)
<commit_msg>created dev app. everything still seems good on login.<commit_after>from __future__ import print_function
from app import app
from flask import render_template, request, jsonify, redirect, url_for
from flask import g
from rates_controller import get_current_rates, update_rates
from user_controller import add_user, change_info_view, update_user_nickname
from controller import get_daily_summary
from transaction_controller import get_recent_transactions, get_filtered_summary
from transaction_controller import execute_transaction, get_sum_category_per_day
from datetime import datetime, timedelta
from flask.ext.login import login_user, logout_user, current_user
from models import User
from app import lm, oid
from utilities import execute_git_log
from oauth import OAuthSignIn

import logging
from app import setup_logger
logger = logging.getLogger(__name__)
setup_logger(logger)
logger.setLevel(logging.INFO)


@app.before_request
def before_request_happens():
    g.user = current_user

    if g.user.is_anonymous():
        user = User.query.filter_by(email='<EMAIL>').first()
        g.user = user

    remote_address = request.remote_addr

    if app.debug or (not app.debug and remote_address != '127.0.0.1'):
        logger.info('NEW REQUEST: current user is: {0}.  From Ip: {1}'.format(g.user, remote_address))

    g.commit = execute_git_log()


#####
# Functions for handling user login:
#####
@lm.user_loader
def load_user(u_id):
    return User.query.get(int(u_id))


@app.route('/authorize/<provider>')
def oauth_authorize(provider):
    if not current_user.is_anonymous():
        return redirect(url_for('build_index'))
    oauth = OAuthSignIn.get_provider(provider)
    return oauth.authorize()


@app.route('/callback/<provider>')
def oauth_callback(provider):
    if not current_user.is_anonymous():
        return redirect(url_for('build_index'))

    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()

    if social_id is None:
        logger.error('Authentication failed.')
        return redirect(url_for('build_index'))

    user = User.query.filter_by(social_id=social_id).first()

    if not user:
        user = add_user(social_id, username, email)

    if user:
        login_user(user, True)

    return redirect(url_for('build_index'))


@app.route('/logout')
def logout_view():
    logout_user()
    logger.info("successful logout for user: {0}".format(g.user))

    # rates = get_current_rates()
    # transactions = get_recent_transactions()
    # return render_template('index.html', title='Insolvency_Solver',
    #                        rates=rates, transactions=transactions)
    # return jsonify({'message': 'look how logged out you are!'})
    return redirect(url_for('build_index'))


# let's put all of this rates crap in an app.context_processor as available functions
# then change the templates to just use those when they need them.
@app.context_processor
def template_functions():

    user = g.user

    # rates and transactions should get placed in a context processor...
    # probably the same with our commit object?
    rates = get_current_rates(user)
    rates['monthly_balance'] = rates['income_per_month'] - (rates['rent'] + rates['bills'] + rates['other_costs'])
    rates['max_spending'] = rates['monthly_balance']/30

    transactions = get_recent_transactions(user)
    transaction_categories = list(set([t['purchase_type'] for t in transactions]))
    transaction_categories.sort()

    return dict(rates=rates, transactions=transactions, u=user, transaction_categories=transaction_categories)

{'fraction_non_alphanumeric': 0.10811128066674493, 'fraction_numerical': 0.0029346167390538794, 'mean_word_length': 4.34504391468005, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '705204', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2503, 'n_words': 669}