<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

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='guest@guest.com').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('/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('/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('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 = 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)

# Main Page Building Routes:
@app.route('/')
@app.route('/index')
@oid.loginhandler
def build_index():
    return render_template('index.html', title='Insolvency_Solver')

@app.route('/settings')
def get_settings():
    return render_template('settings.html')

@app.route('/daily_summary')
def get_daily_summary_view():

    user = g.user
    daily_summary = get_daily_summary(user)

    return render_template('daily_summary.html', daily_summary=daily_summary)

@app.route('/transaction_metrics')
def transaction_metrics():
    return render_template('transaction_metrics.html')

@app.route('/calendar')
def calendar():
    return render_template('calendar.html')

###############
# POST ROUTES:
###############
@app.route('/change_nickname', methods=['POST'])
def change_nickname():
    user = g.user
    nickname = request.form['nickname']
    if len(nickname) == 0 or len(nickname) > 50:
        return jsonify({'message': 'noooooope'})

    result = update_user_nickname(user, nickname)
    return jsonify({'message': result})

@app.route('/change_info_display', methods=['POST'])
def show_hide_info():
    hiding = request.form['hidden']
    user = g.user

    # guests can't permanently hide info, because I say so.
    if user.is_guest():
        return jsonify({'success': False})

    logger.info("hiding: {0}, user: {1}".format(hiding, user))
    change_info_view(user, hiding)

    return jsonify({'success': True})

@app.route('/submit_monthly', methods=['POST'])
def submit_monthly():

    """receives a set of updated figures for monthly income & costs.  determines what our original ratio of
    spending vs. savings was, and attempts to apply that ratio to the new monthly net balance, in order to set a new
    daily credits vs. monthly spending fulcrum"""

    user = g.user

    # this needs to be able to deal with missing values, and validation of values coming in too
    # I can receive changes in bills, rent, other, and income

    updates_rates_dict = {str(k): int(str(v)) for (k, v) in request.form.iteritems() if k and v}
    logger.info('update received with values: {0}'.format(updates_rates_dict))

    for v in updates_rates_dict.values():
        if v > 100000:
            return jsonify({'message': "what are you tryin' to pull here, buddy?"})

    current_rates = get_current_rates(user)

    # fill out missing values in update_rates:
    for rate, value in current_rates.iteritems():
        if rate not in updates_rates_dict:
            updates_rates_dict[rate] = current_rates[rate]

    old_monthly_balance = current_rates['income_per_month'] - (current_rates['rent'] +
                                                               current_rates['bills'] +
                                                               current_rates['other_costs'])

    monthly_balance = updates_rates_dict['income_per_month'] - (updates_rates_dict['rent'] +
                                                                updates_rates_dict['bills'] +
                                                                updates_rates_dict['other_costs'])

    # discover the ratio of spending vs savings in our old rates:
    old_monthly_spending = current_rates['daily'] * 30
    current_credit_ratio = float(old_monthly_spending)/float(old_monthly_balance)

    if monthly_balance < 0:
        return jsonify({'message':
                        'monthly costs exceed monthly income. You wont be able to save any money with me :<'})

    # using our previous ratio, determine the new spending amount given the new monthly balance:
    new_monthly_spending = current_credit_ratio * monthly_balance
    updates_rates_dict['daily'] = int(new_monthly_spending / 30)
    updates_rates_dict['savings_per_month'] = int(monthly_balance - new_monthly_spending)

    # This can get moved higher now...
    if len(updates_rates_dict) == 0:
        return jsonify({'updating with the following': updates_rates_dict,
                        'current rates': current_rates,
                        'month balance': monthly_balance,
                        'current ratio': current_credit_ratio,
                        'new spending': new_monthly_spending})

    result = update_rates(user, updates_rates_dict)

    return redirect(url_for('get_settings'))

@app.route('/submit_savings', methods=['POST'])
def submit_savings():

    user = g.user
    updates_rates_dict = {str(k): int(str(v)) for (k, v) in request.form.iteritems() if k and v}
    logger.info('update received with values: {0}'.format(updates_rates_dict))

    for v in updates_rates_dict.values():
        if v > 100000:
            return jsonify({'message': "what are you tryin' to pull here, buddy?"})

    current_rates = get_current_rates(user)
    # fill out missing values in update_rates:
    for rate, value in current_rates.iteritems():
        if rate not in updates_rates_dict:
            updates_rates_dict[rate] = current_rates[rate]

    monthly_balance = current_rates['income_per_month'] - (current_rates['rent'] +
                                                           current_rates['bills'] +
                                                           current_rates['other_costs'])

    updates_rates_dict['savings_per_month'] = monthly_balance - (updates_rates_dict['daily'] * 30)

    result = update_rates(user, updates_rates_dict)

    return redirect(url_for('get_settings'))

@app.route('/submit_spending', methods=['POST'])
def submit_spending():
    user = g.user

    updates_rates_dict = {str(k): int(str(v)) for (k, v) in request.form.iteritems()}
    logger.info('update received with values: {0}'.format(updates_rates_dict))

    for v in updates_rates_dict.values():
        if v > 100000:
            return jsonify({'message': "what are you tryin' to pull here, buddy?"})

    result = update_rates(user, updates_rates_dict)

    return redirect(url_for('settings'))

@app.route('/submit_transaction', methods=['POST'])
def submit_transaction():

    """This is the main entry point for submitting a new transaction to the DB"""

    user = g.user

    logger.info('transaction submitted.')

    try:
        transaction_amount = request.form['transaction']

        if transaction_amount == '':  # this can die after date is finished.  value error will catch ''
            transaction_amount = 0
        else:
            transaction_amount = int(transaction_amount)

        if transaction_amount > 100000:
            return redirect(url_for('build_index'))

        purchase_type = request.form['purchase']
        transaction_date = request.form['transaction_date']
    except ValueError, e:
        logger.warn('value error on input: {0}'.format(str(e)))
        return redirect(url_for('build_index'))

    logger.info('received a new transaction, amount: {0}, type: {1}, date: {2}'.format(transaction_amount,
                                                                                purchase_type,
                                                                                transaction_date))

    if purchase_type:
        purchase_type = purchase_type.title()

    # parse transaction_date here.
    if transaction_date:
        parsed_date = datetime.strptime(transaction_date, '%d/%m/%Y')
    else:
        parsed_date = datetime.today()

    # move this back up after finished with testing of transaction date.
    if transaction_amount == 0:
        return redirect(url_for('build_index'))

    execute_transaction(user, transaction_amount, purchase_type, parsed_date)

    return redirect(url_for('build_index'))

#ROUTES FOR METRICS:
@app.route('/get_spending_data')
def get_spending_data():

    """this will be our new main function for getting all required data
    using filters for transaction chart section"""

    user = g.user

    filters = request.args.get('filters', '')
    filter_array = [str(x) for x in filters.split(',')]

    # filtered daily summary can just do it all now.
    daily_summary = get_filtered_summary(user, filter_array)

    transaction_summary = get_sum_category_per_day()
    transaction_categories = list(set([t['purchase_type'] for t in transaction_summary]))

    return jsonify({'transaction_summary': transaction_summary,
                    'daily_summary': daily_summary,
                    'categories': transaction_categories})

# TO BE DEPRECATED:

# @app.route('/get_transaction_summary')
# def get_transaction_summary():
#     """This returns a daily list of sum(amount) per category"""
#
#     transaction_summary = get_sum_category_per_day()
#     return jsonify({'transaction_summary': transaction_summary})

#
# @app.route('/get_transaction_metrics')
# def get_transaction_metrics():
#
#     start_date = request.args.get('start_date', None)
#     end_date = request.args.get('end_date', None)
#
#     if start_date != '0' and end_date != '0':
#
#         logger.info("got it!")
#         start = datetime.strptime(start_date, '%d/%m/%Y')
#         # delta = timedelta(days=1)
#         # start = start - delta
#         end = datetime.strptime(end_date, '%d/%m/%Y')
#     else:
#         start = None
#         end = None
#
#     transaction_list = get_recent_transactions(start, end)
#
#     transaction_categories = list(set([t['purchase_type'] for t in transaction_list]))
#     transaction_categories.sort()
#
#     return jsonify({"transactions": transaction_list, "categories": transaction_categories, "success": True})

#
# @app.route('/get_daily_metrics')
# def get_daily_metrics():
#
#     """this is used by main metrics page, and is set to get nixed."""
#
#     start_date = request.args.get('start_date', None)
#     end_date = request.args.get('end_date', None)
#     filters = request.args.get('filters', None)
#
#     logger.info('hey guess what???? I received: {0}, {1}'.format(start_date, end_date))
#     logger.info('hey guess what???? I received: {0}'.format(filters))
#
#     filter_array = [str(x) for x in filters.split(',')]
#
#     if len(filter_array):
#         logger.info('yerrrrp {0}'.format(filter_array))
#
#     if start_date != '0' and end_date != '0':
#         start = datetime.strptime(start_date, '%d/%m/%Y')
#         delta = timedelta(days=1)
#         start = start - delta
#         end = datetime.strptime(end_date, '%d/%m/%Y')
#     else:
#         start = None
#         end = None
#
#     if len(filter_array) and False:
#         daily_summary = get_filtered_summary(filter_array)
#     else:
#         daily_summary = get_daily_summary(start, end)
#
#     return jsonify({"summary": daily_summary, "success": True})

<commit_msg>i think we're good.  deprecated stuff deletoed.<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

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='guest@guest.com').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('/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('/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('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 = 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)

# Main Page Building Routes:
@app.route('/')
@app.route('/index')
@oid.loginhandler
def build_index():
    return render_template('index.html', title='Insolvency_Solver')

@app.route('/settings')
def get_settings():
    return render_template('settings.html')

@app.route('/daily_summary')
def get_daily_summary_view():

    user = g.user
    daily_summary = get_daily_summary(user)

    return render_template('daily_summary.html', daily_summary=daily_summary)

@app.route('/transaction_metrics')
def transaction_metrics():
    return render_template('transaction_metrics.html')

@app.route('/calendar')
def calendar():
    return render_template('calendar.html')

###############
# POST ROUTES:
###############
@app.route('/change_nickname', methods=['POST'])
def change_nickname():
    user = g.user
    nickname = request.form['nickname']
    if len(nickname) == 0 or len(nickname) > 50:
        return jsonify({'message': 'noooooope'})

    result = update_user_nickname(user, nickname)
    return jsonify({'message': result})

@app.route('/change_info_display', methods=['POST'])
def show_hide_info():
    hiding = request.form['hidden']
    user = g.user

    # guests can't permanently hide info, because I say so.
    if user.is_guest():
        return jsonify({'success': False})

    logger.info("hiding: {0}, user: {1}".format(hiding, user))
    change_info_view(user, hiding)

    return jsonify({'success': True})

@app.route('/submit_monthly', methods=['POST'])
def submit_monthly():

    """receives a set of updated figures for monthly income & costs.  determines what our original ratio of
    spending vs. savings was, and attempts to apply that ratio to the new monthly net balance, in order to set a new
    daily credits vs. monthly spending fulcrum"""

    user = g.user

    # this needs to be able to deal with missing values, and validation of values coming in too
    # I can receive changes in bills, rent, other, and income

    updates_rates_dict = {str(k): int(str(v)) for (k, v) in request.form.iteritems() if k and v}
    logger.info('update received with values: {0}'.format(updates_rates_dict))

    for v in updates_rates_dict.values():
        if v > 100000:
            return jsonify({'message': "what are you tryin' to pull here, buddy?"})

    current_rates = get_current_rates(user)

    # fill out missing values in update_rates:
    for rate, value in current_rates.iteritems():
        if rate not in updates_rates_dict:
            updates_rates_dict[rate] = current_rates[rate]

    old_monthly_balance = current_rates['income_per_month'] - (current_rates['rent'] +
                                                               current_rates['bills'] +
                                                               current_rates['other_costs'])

    monthly_balance = updates_rates_dict['income_per_month'] - (updates_rates_dict['rent'] +
                                                                updates_rates_dict['bills'] +
                                                                updates_rates_dict['other_costs'])

    # discover the ratio of spending vs savings in our old rates:
    old_monthly_spending = current_rates['daily'] * 30
    current_credit_ratio = float(old_monthly_spending)/float(old_monthly_balance)

    if monthly_balance < 0:
        return jsonify({'message':
                        'monthly costs exceed monthly income. You wont be able to save any money with me :<'})

    # using our previous ratio, determine the new spending amount given the new monthly balance:
    new_monthly_spending = current_credit_ratio * monthly_balance
    updates_rates_dict['daily'] = int(new_monthly_spending / 30)
    updates_rates_dict['savings_per_month'] = int(monthly_balance - new_monthly_spending)

    # This can get moved higher now...
    if len(updates_rates_dict) == 0:
        return jsonify({'updating with the following': updates_rates_dict,
                        'current rates': current_rates,
                        'month balance': monthly_balance,
                        'current ratio': current_credit_ratio,
                        'new spending': new_monthly_spending})

    result = update_rates(user, updates_rates_dict)

    return redirect(url_for('get_settings'))

@app.route('/submit_savings', methods=['POST'])
def submit_savings():

    user = g.user
    updates_rates_dict = {str(k): int(str(v)) for (k, v) in request.form.iteritems() if k and v}
    logger.info('update received with values: {0}'.format(updates_rates_dict))

    for v in updates_rates_dict.values():
        if v > 100000:
            return jsonify({'message': "what are you tryin' to pull here, buddy?"})

    current_rates = get_current_rates(user)
    # fill out missing values in update_rates:
    for rate, value in current_rates.iteritems():
        if rate not in updates_rates_dict:
            updates_rates_dict[rate] = current_rates[rate]

    monthly_balance = current_rates['income_per_month'] - (current_rates['rent'] +
                                                           current_rates['bills'] +
                                                           current_rates['other_costs'])

    updates_rates_dict['savings_per_month'] = monthly_balance - (updates_rates_dict['daily'] * 30)

    result = update_rates(user, updates_rates_dict)

    return redirect(url_for('get_settings'))

@app.route('/submit_spending', methods=['POST'])
def submit_spending():
    user = g.user

    updates_rates_dict = {str(k): int(str(v)) for (k, v) in request.form.iteritems()}
    logger.info('update received with values: {0}'.format(updates_rates_dict))

    for v in updates_rates_dict.values():
        if v > 100000:
            return jsonify({'message': "what are you tryin' to pull here, buddy?"})

    result = update_rates(user, updates_rates_dict)

    return redirect(url_for('settings'))

@app.route('/submit_transaction', methods=['POST'])
def submit_transaction():

    """This is the main entry point for submitting a new transaction to the DB"""

    user = g.user

    logger.info('transaction submitted.')

    try:
        transaction_amount = request.form['transaction']

        if transaction_amount == '':  # this can die after date is finished.  value error will catch ''
            transaction_amount = 0
        else:
            transaction_amount = int(transaction_amount)

        if transaction_amount > 100000:
            return redirect(url_for('build_index'))

        purchase_type = request.form['purchase']
        transaction_date = request.form['transaction_date']
    except ValueError, e:
        logger.warn('value error on input: {0}'.format(str(e)))
        return redirect(url_for('build_index'))

    logger.info('received a new transaction, amount: {0}, type: {1}, date: {2}'.format(transaction_amount,
                                                                                purchase_type,
                                                                                transaction_date))

    if purchase_type:
        purchase_type = purchase_type.title()

    # parse transaction_date here.
    if transaction_date:
        parsed_date = datetime.strptime(transaction_date, '%d/%m/%Y')
    else:
        parsed_date = datetime.today()

    # move this back up after finished with testing of transaction date.
    if transaction_amount == 0:
        return redirect(url_for('build_index'))

    execute_transaction(user, transaction_amount, purchase_type, parsed_date)

    return redirect(url_for('build_index'))

# ROUTES FOR METRICS:
@app.route('/get_spending_data')
def get_spending_data():

    """this will be our new main function for getting all required data
    using filters for transaction chart section"""

    user = g.user

    filters = request.args.get('filters', '')
    filter_array = [str(x) for x in filters.split(',')]

    # filtered daily summary can just do it all now.
    daily_summary = get_filtered_summary(user, filter_array)

    transaction_summary = get_sum_category_per_day()
    transaction_categories = list(set([t['purchase_type'] for t in transaction_summary]))

    return jsonify({'transaction_summary': transaction_summary,
                    'daily_summary': daily_summary,
                    'categories': transaction_categories})

<|endoftext|>


<commit_before>from django.template import TemplateSyntaxError, TemplateDoesNotExist, Variable
from django.template import Library, Node, TextNode
from django.template.loader import get_template, get_template_from_string, find_template_source
from django.conf import settings
from django.utils.safestring import mark_safe

register = Library()

class ExtendsError(Exception):
    pass

class BlockNode(Node):
    def __init__(self, name, nodelist, parent=None):
        self.name, self.nodelist, self.parent = name, nodelist, parent

    def __repr__(self):
        return "<Block Node: %s. Contents: %r>" % (self.name, self.nodelist)

    def render(self, context):
        context.push()
        # Save context in case of block.super().
        self.context = context
        context['block'] = self
        result = self.nodelist.render(context)
        context.pop()
        return result

    def super(self):
        if self.parent:
            return mark_safe(self.parent.render(self.context))
        return ''

    def add_parent(self, nodelist):
        if self.parent:
            self.parent.add_parent(nodelist)
        else:
            self.parent = BlockNode(self.name, nodelist)

class ExtendsNode(Node):
    must_be_first = True

    def __init__(self, nodelist, parent_name, parent_name_expr, template_dirs=None):
        self.nodelist = nodelist
        self.parent_name, self.parent_name_expr = parent_name, parent_name_expr
        self.template_dirs = template_dirs

    def __repr__(self):
        if self.parent_name_expr:
            return "<ExtendsNode: extends %s>" % self.parent_name_expr.token
        return '<ExtendsNode: extends "%s">' % self.parent_name

    def get_parent(self, context):
        if self.parent_name_expr:
            self.parent_name = self.parent_name_expr.resolve(context)
        parent = self.parent_name
        if not parent:
            error_msg = "Invalid template name in 'extends' tag: %r." % parent
            if self.parent_name_expr:
                error_msg += " Got this from the %r variable." % self.parent_name_expr #TODO nice repr.
            raise TemplateSyntaxError, error_msg
        if hasattr(parent, 'render'):
            return parent # parent is a Template object
        try:
            source, origin = find_template_source(parent, self.template_dirs)
        except TemplateDoesNotExist:
            raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent
        else:
            return get_template_from_string(source, origin, parent)

    def render(self, context):
        compiled_parent = self.get_parent(context)
        if len(compiled_parent.nodelist) > 1:
            n0, n1 = compiled_parent.nodelist[:2]
        else:
            n0, n1 = compiled_parent.nodelist[0], None
        parent_is_child = (isinstance(n0, ExtendsNode) or
                (isinstance(n0, TextNode) and isinstance(n1, ExtendsNode)))
        if parent_is_child:
            extend_node = int(not isinstance(n0, ExtendsNode))
        parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)])
        for block_node in self.nodelist.get_nodes_by_type(BlockNode):
            # Check for a BlockNode with this node's name, and replace it if found.
            try:
                parent_block = parent_blocks[block_node.name]
            except KeyError:
                # This BlockNode wasn't found in the parent template, but the
                # parent block might be defined in the parent's *parent*, so we
                # add this BlockNode to the parent's ExtendsNode nodelist, so
                # it'll be checked when the parent node's render() is called.
                if parent_is_child:
                    compiled_parent.nodelist[extend_node].nodelist.append(block_node)
            else:
                # Keep any existing parents and add a new one. Used by BlockNode.
                parent_block.parent = block_node.parent
                parent_block.add_parent(parent_block.nodelist)
                parent_block.nodelist = block_node.nodelist
        return compiled_parent.render(context)

class ConstantIncludeNode(Node):
    def __init__(self, template_path):
        try:
            t = get_template(template_path)
            self.template = t
        except:
            if settings.TEMPLATE_DEBUG:
                raise
            self.template = None

    def render(self, context):
        if self.template:
            return self.template.render(context)
        else:
            return ''

class IncludeNode(Node):
    def __init__(self, template_name):
        self.template_name = Variable(template_name)

    def render(self, context):
        try:
            template_name = self.template_name.resolve(context)
            t = get_template(template_name)
            return t.render(context)
        except TemplateSyntaxError, e:
            if settings.TEMPLATE_DEBUG:
                raise
            return ''
        except:
            return '' # Fail silently for invalid included templates.

def do_block(parser, token):
    """
    Define a block that can be overridden by child templates.
    """
    bits = token.contents.split()
    if len(bits) != 2:
        raise TemplateSyntaxError, "'%s' tag takes only one argument" % bits[0]
    block_name = bits[1]
    # Keep track of the names of BlockNodes found in this template, so we can
    # check for duplication.
    try:
        if block_name in parser.__loaded_blocks:
            raise TemplateSyntaxError, "'%s' tag with name '%s' appears more than once" % (bits[0], block_name)
        parser.__loaded_blocks.append(block_name)
    except AttributeError: # parser.__loaded_blocks isn't a list yet
        parser.__loaded_blocks = [block_name]
    nodelist = parser.parse(('endblock', 'endblock %s' % block_name))
    parser.delete_first_token()
    return BlockNode(block_name, nodelist)

def do_extends(parser, token):
    """
    Signal that this template extends a parent template.

    This tag may be used in two ways: ``{% extends "base" %}`` (with quotes)
    uses the literal value "base" as the name of the parent template to extend,
    or ``{% extends variable %}`` uses the value of ``variable`` as either the
    name of the parent template to extend (if it evaluates to a string) or as
    the parent tempate itelf (if it evaluates to a Template object).
    """
    bits = token.contents.split()
    if len(bits) != 2:
        raise TemplateSyntaxError, "'%s' takes one argument" % bits[0]
    parent_name, parent_name_expr = None, None
    if bits[1][0] in ('"', "'") and bits[1][-1] == bits[1][0]:
        parent_name = bits[1][1:-1]
    else:
        parent_name_expr = parser.compile_filter(bits[1])
    nodelist = parser.parse()
    if nodelist.get_nodes_by_type(ExtendsNode):
        raise TemplateSyntaxError, "'%s' cannot appear more than once in the same template" % bits[0]
    return ExtendsNode(nodelist, parent_name, parent_name_expr)

def do_include(parser, token):
    """
    Loads a template and renders it with the current context.

    Example::

        {% include "foo/some_include" %}
    """
    bits = token.contents.split()
    if len(bits) != 2:
        raise TemplateSyntaxError, "%r tag takes one argument: the name of the template to be included" % bits[0]
    path = bits[1]
    if path[0] in ('"', "'") and path[-1] == path[0]:
        return ConstantIncludeNode(path[1:-1])
    return IncludeNode(bits[1])

register.tag('block', do_block)
register.tag('extends', do_extends)
register.tag('include', do_include)
<commit_msg>Fixed a simple TODO item in one error path of the "extends" tag.<commit_after>from django.template import TemplateSyntaxError, TemplateDoesNotExist, Variable
from django.template import Library, Node, TextNode
from django.template.loader import get_template, get_template_from_string, find_template_source
from django.conf import settings
from django.utils.safestring import mark_safe

register = Library()

class ExtendsError(Exception):
    pass

class BlockNode(Node):
    def __init__(self, name, nodelist, parent=None):
        self.name, self.nodelist, self.parent = name, nodelist, parent

    def __repr__(self):
        return "<Block Node: %s. Contents: %r>" % (self.name, self.nodelist)

    def render(self, context):
        context.push()
        # Save context in case of block.super().
        self.context = context
        context['block'] = self
        result = self.nodelist.render(context)
        context.pop()
        return result

    def super(self):
        if self.parent:
            return mark_safe(self.parent.render(self.context))
        return ''

    def add_parent(self, nodelist):
        if self.parent:
            self.parent.add_parent(nodelist)
        else:
            self.parent = BlockNode(self.name, nodelist)

class ExtendsNode(Node):
    must_be_first = True

    def __init__(self, nodelist, parent_name, parent_name_expr, template_dirs=None):
        self.nodelist = nodelist
        self.parent_name, self.parent_name_expr = parent_name, parent_name_expr
        self.template_dirs = template_dirs

    def __repr__(self):
        if self.parent_name_expr:
            return "<ExtendsNode: extends %s>" % self.parent_name_expr.token
        return '<ExtendsNode: extends "%s">' % self.parent_name

    def get_parent(self, context):
        if self.parent_name_expr:
            self.parent_name = self.parent_name_expr.resolve(context)
        parent = self.parent_name
        if not parent:
            error_msg = "Invalid template name in 'extends' tag: %r." % parent
            if self.parent_name_expr:
                error_msg += " Got this from the '%s' variable." % self.parent_name_expr.token
            raise TemplateSyntaxError, error_msg
        if hasattr(parent, 'render'):
            return parent # parent is a Template object
        try:
            source, origin = find_template_source(parent, self.template_dirs)
        except TemplateDoesNotExist:
            raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent
        else:
            return get_template_from_string(source, origin, parent)

    def render(self, context):
        compiled_parent = self.get_parent(context)
        if len(compiled_parent.nodelist) > 1:
            n0, n1 = compiled_parent.nodelist[:2]
        else:
            n0, n1 = compiled_parent.nodelist[0], None
        parent_is_child = (isinstance(n0, ExtendsNode) or
                (isinstance(n0, TextNode) and isinstance(n1, ExtendsNode)))
        if parent_is_child:
            extend_node = int(not isinstance(n0, ExtendsNode))
        parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)])
        for block_node in self.nodelist.get_nodes_by_type(BlockNode):
            # Check for a BlockNode with this node's name, and replace it if found.
            try:
                parent_block = parent_blocks[block_node.name]
            except KeyError:
                # This BlockNode wasn't found in the parent template, but the
                # parent block might be defined in the parent's *parent*, so we
                # add this BlockNode to the parent's ExtendsNode nodelist, so
                # it'll be checked when the parent node's render() is called.
                if parent_is_child:
                    compiled_parent.nodelist[extend_node].nodelist.append(block_node)
            else:
                # Keep any existing parents and add a new one. Used by BlockNode.
                parent_block.parent = block_node.parent
                parent_block.add_parent(parent_block.nodelist)
                parent_block.nodelist = block_node.nodelist
        return compiled_parent.render(context)

class ConstantIncludeNode(Node):
    def __init__(self, template_path):
        try:
            t = get_template(template_path)
            self.template = t
        except:
            if settings.TEMPLATE_DEBUG:
                raise
            self.template = None

    def render(self, context):
        if self.template:
            return self.template.render(context)
        else:
            return ''

class IncludeNode(Node):
    def __init__(self, template_name):
        self.template_name = Variable(template_name)

    def render(self, context):
        try:
            template_name = self.template_name.resolve(context)
            t = get_template(template_name)
            return t.render(context)
        except TemplateSyntaxError, e:
            if settings.TEMPLATE_DEBUG:
                raise
            return ''
        except:
            return '' # Fail silently for invalid included templates.

def do_block(parser, token):
    """
    Define a block that can be overridden by child templates.
    """
    bits = token.contents.split()
    if len(bits) != 2:
        raise TemplateSyntaxError, "'%s' tag takes only one argument" % bits[0]
    block_name = bits[1]
    # Keep track of the names of BlockNodes found in this template, so we can
    # check for duplication.
    try:
        if block_name in parser.__loaded_blocks:
            raise TemplateSyntaxError, "'%s' tag with name '%s' appears more than once" % (bits[0], block_name)
        parser.__loaded_blocks.append(block_name)
    except AttributeError: # parser.__loaded_blocks isn't a list yet
        parser.__loaded_blocks = [block_name]
    nodelist = parser.parse(('endblock', 'endblock %s' % block_name))
    parser.delete_first_token()
    return BlockNode(block_name, nodelist)

def do_extends(parser, token):
    """
    Signal that this template extends a parent template.

    This tag may be used in two ways: ``{% extends "base" %}`` (with quotes)
    uses the literal value "base" as the name of the parent template to extend,
    or ``{% extends variable %}`` uses the value of ``variable`` as either the
    name of the parent template to extend (if it evaluates to a string) or as
    the parent tempate itelf (if it evaluates to a Template object).
    """
    bits = token.contents.split()
    if len(bits) != 2:
        raise TemplateSyntaxError, "'%s' takes one argument" % bits[0]
    parent_name, parent_name_expr = None, None
    if bits[1][0] in ('"', "'") and bits[1][-1] == bits[1][0]:
        parent_name = bits[1][1:-1]
    else:
        parent_name_expr = parser.compile_filter(bits[1])
    nodelist = parser.parse()
    if nodelist.get_nodes_by_type(ExtendsNode):
        raise TemplateSyntaxError, "'%s' cannot appear more than once in the same template" % bits[0]
    return ExtendsNode(nodelist, parent_name, parent_name_expr)

def do_include(parser, token):
    """
    Loads a template and renders it with the current context.

    Example::

        {% include "foo/some_include" %}
    """
    bits = token.contents.split()
    if len(bits) != 2:
        raise TemplateSyntaxError, "%r tag takes one argument: the name of the template to be included" % bits[0]
    path = bits[1]
    if path[0] in ('"', "'") and path[-1] == path[0]:
        return ConstantIncludeNode(path[1:-1])
    return IncludeNode(bits[1])

register.tag('block', do_block)
register.tag('extends', do_extends)
register.tag('include', do_include)
<|endoftext|>


<commit_before>from flask import jsonify, request
from app import app, models
from sqlalchemy import inspect

#UFC Badassery
from UFCML import ufcml

@app.route('/')
@app.route('/index')
@app.route('/version')
def api_index():
    return "Connected to octagon API version: 1.0"

@app.route('/fid', methods=['GET'])
def api_fid():
    fighter_name = request.args.get('name')
    session = models.loadsession()
    fid = session.query(models.Fighters).filter_by(name=fighter_name).first().fid
    return jsonify({'fid': 0}) # Does nothing right now

@app.route('/predict', methods=['GET'])
def api_ml():
    f1id = int(request.args.get('f1id'))
    f2id = int(request.args.get('f2id'))

    f1prob, f2prob, uncertainty = ufcml.predict(f1id, f2id)

    return jsonify({
        'f1prob': f1prob,
        'f2prob': f2prob,
        'uncertainty': uncertainty
        })

@app.route('/getFighters')
def api_name():
    session = models.loadsession()
    recs = {'fighters': []}

    for row in session.query(models.Fighters).all():
        recs['fighters'].append({'id': row.fid, 'name': row.name})

    return jsonify(recs)

<commit_msg>created a stats route<commit_after>from flask import jsonify, request
from app import app, models
from sqlalchemy import inspect

#UFC Badassery
from UFCML import ufcml

@app.route('/')
@app.route('/index')
@app.route('/version')
def api_index():
    return "Connected to octagon API version: 1.0"

@app.route('/stats', methods=['GET'])
def api_stats():
    fid = int(request.args.get('fid'))
    session = models.loadsession()
    f = session.query(models.Fighters).filter_by(fid=fid).first()

    stats = {
        'name':f.name,
        'fid':f.fid,
        'height':f.height_inches,
        'weight':f.weight_lbs,
        'association':f.association,
        'division':f.division,
        'country':f.country,
        'reach':f.reach_inches,
        'strike_offense_per_min':f.strike_offense_per_min,
        'strike_offense_accuracy':f.strike_offense_accuracy,
        'strike_defense_per_min':f.strike_defense_per_min,
        'strike_defense_accuracy':f.strike_defense_accuracy,
        'takedowns_per_fight':f.takedowns_per_fight,
        'takedowns_accuracy':f.takedowns_accuracy,
        'takedowns_defense':f.takedowns_defense,
        'submissions_per_fight':f.submissions_per_fight,
        'wins':f.wins,
        'losses':f.losses,
        'draws':f.draws,
        'total_fights':f.total_fights
    }

    return jsonify(stats) # Does nothing right now

@app.route('/predict', methods=['GET'])
def api_ml():
    f1id = int(request.args.get('f1id'))
    f2id = int(request.args.get('f2id'))

    f1prob, f2prob, uncertainty = ufcml.predict(f1id, f2id)

    return jsonify({
        'f1prob': f1prob,
        'f2prob': f2prob,
        'uncertainty': uncertainty
        })

@app.route('/getFighters')
def api_name():
    session = models.loadsession()
    recs = {'fighters': []}

    for row in session.query(models.Fighters).all():
        recs['fighters'].append({'id': row.fid, 'name': row.name})

    return jsonify(recs)

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# This code is distributed under the two-clause BSD license.
# Copyright (c) 2012-2013 Raphaël Barrois

"""Provide a AuthGroupeX authentication backend for Django."""

from __future__ import absolute_import

from .auth import AuthGroupeXBackend
from .auth import PERM_ADMIN, PERM_GROUP_ADMIN, PERM_GROUP_MEMBER, PERM_USER
from .exceptions import AuthGroupeXError, InvalidAuth
from .views import AuthGroupeXBaseView, AuthGroupeXUniqueView

__author__ = u"Raphaël Barrois <raphael.barrois+djauthgroupex@polytechnique.org>"
__version__ = '0.2.1'
<commit_msg>Version bump to 0.2.2<commit_after># -*- coding: utf-8 -*-
# This code is distributed under the two-clause BSD license.
# Copyright (c) 2012-2013 Raphaël Barrois

"""Provide a AuthGroupeX authentication backend for Django."""

from __future__ import absolute_import

from .auth import AuthGroupeXBackend
from .auth import PERM_ADMIN, PERM_GROUP_ADMIN, PERM_GROUP_MEMBER, PERM_USER
from .exceptions import AuthGroupeXError, InvalidAuth
from .views import AuthGroupeXBaseView, AuthGroupeXUniqueView

__author__ = u"Raphaël Barrois <raphael.barrois+djauthgroupex@polytechnique.org>"
__version__ = '0.2.2'
<|endoftext|>


<commit_before>import awslib
from app import app
from flask import render_template
from flask import send_from_directory
import json
from json import dumps
from os.path import join
from flask import make_response, request, redirect, url_for
import os, time, pickle
import traceback

bucket_name = os.environ.get('IPLIST_CONFIG_BUCKET')
s3path = os.environ.get('IPLIST_CONFIG_PATH')
nohttps = os.environ.get('NOHTTPS')

path = join('iplist_config', 'config.json')

if s3path == None:
    print ("No Env Labeled IPLIST_CONFIG_PATH")
elif bucket_name == None:
    print ("No bucket name specified")
else:
    awslib._get_file(bucket_name, s3path, path)

#####
# Caching parameters
#####
cache_timeout_period_in_seconds = 300
cache_root_directory = "/ip-range-cache"

try:
    os.makedirs(cache_root_directory)
except:
    pass

@app.route('/')
def handle_index():
    redir = None
    if nohttps == None:
        proto = request.headers.get("X-Forwarded-Proto")
        if not proto == "https":
            redir = _check_ssl(request.url)

    if not redir == None:
        return redir

    with open(path) as json_data:
        data = json.load(json_data)

    # creating altname list for to be deprecated url links
    altapps=[]
    for app in data['apps']:
        if app.get('altname'):
            altapps.append(app['altname'])

    return render_template("index.html", apps=[app['name'] for app in data['apps']], altapps=altapps )

@app.route('/healthcheck')
def handle_healthcheck():
    return "I'm still here. test1"

@app.route('/<appname>')
def handle_app(appname):
    verbose = False
    chosen_region = None
    query_string = request.query_string

    if not query_string == "":
        for query in query_string.split('&'):
            if "verbose" in query.lower():
                if query.endswith("1"):
                    verbose = True
            elif "region" in query.lower():
                chosen_region = query[7:]
    suffix = ".json"

    if verbose:
        suffix = ".verbose" + suffix

    if chosen_region:
        suffix = "." + chosen_region + suffix

    app_cache_file = os.path.join(cache_root_directory,appname.lower() + suffix)
    print(app_cache_file, "1")
    read_from_cache = True
    try:
        with open(app_cache_file, "r") as cache:
            cache_time = float(cache.readline().strip())
            current_time = time.time()
            if (current_time - cache_time) > cache_timeout_period_in_seconds:
                read_from_cache = False
        print(app_cache_file,"2")
    except IOError:
        read_from_cache = False

    print(read_from_cache, "1")
    if read_from_cache:
        print("Reading cached data for this request.")
    else:
        print("Cache is out of date. Refreshing for this request.")

        try:
            with open(path) as json_data:
                data = json.load(json_data)

            ret = {}

            if verbose:
                print (request.url)
            redir = None
            if nohttps == None:
                proto = request.headers.get("X-Forwarded-Proto")
                if not proto == "https":
                    redir = _check_ssl(request.url, verbose)
            if not redir == None:
                return redir

            for app in data['apps']:
                # create url link for both name and alternative name for ip-range apps
                if appname.lower() == app['name'].lower() or appname.lower() == str(app.get('altname')).lower():
                    app_config = app['config']

                    for config in app_config:

                        if config.get('s3filepath'):
                            datapath = config.get('localpath')
                            awslib._get_file(bucket_name, config['s3filepath'], datapath)
                            with open(datapath) as filedata:
                                output = json.load(filedata)
                            break
                        elif config.get('R53'):
                            ret = {}
                            for item in config['R53']:
                                ret[item['Name']] = {}
                                ret[item['Name']]['all_ips'] = []
                                ret[item['Name']]['all_ips'] = awslib._get_records_from_zone(item['HostedZoneId'], item['Pattern'], item['Domain'])
                            break

                        dnsname = config['dnsname']
                        bs_app = config['beanstalk_app_name']
                        region = config['region']

                        # only run next section if region equal chosen_region
                        if chosen_region:
                            if chosen_region != region:
                                continue

                        exclusions = config['exclusions']
                        eip_check = config.get('show_eip')
                        lb_check = config.get('show_lb_ip')
                        inst_check = config.get('show_inst_ip')
                        if ret.get(region) == None:
                            ret[region] = {}
                        lb_name = awslib._active_balancer(dnsname, region)

                        if ret[region].get('all_ips') == None:
                            ret[region]['all_ips'] = []

                        if not eip_check == None:
                            eips = awslib._list_eips(region, filter=exclusions)
                            if verbose:
                                if ret[region].get('eips') == None:
                                    ret[region]['eips'] = eips
                                else:
                                    ret[region]['eips'].extend(eips)

                            if eip_check:
                                ret[region]['all_ips'].extend(eips)

                        if not lb_check == None:
                            lb_url = awslib._environment_descr(bs_app, lb_name, region)
                            elb = awslib._balancer_ip(lb_url)

                            if verbose:
                                if ret[region].get('elb') == None:
                                    ret[region]['elb'] = elb
                                else:
                                    ret[region]['elb'].extend(elb)

                            if lb_check:
                                ret[region]['all_ips'].extend(elb)

                        if not inst_check == None:
                            inst_ips = awslib._instance_ip(lb_name, region)
                            if verbose:
                                if ret[region].get('instance_ips') == None:
                                    ret[region]['instance_ips'] = inst_ips
                                else:
                                    ret[region]['instance_ips'].extend(inst_ips)

                            if inst_check:
                                ret[region]['all_ips'].extend(inst_ips)

            if not ret:
                return redirect(url_for('handle_index'), code=302)
            else:
                #sort ip list in ret when it can
                ret = ip_list_sort(ret)
                _write_cache(app_cache_file,ret)
        except:
            print ("Error: Unable to load new information for app: " + str(appname))
            traceback.print_exc()

    with open(app_cache_file, "r") as cache:
        # read the first line as cache time
        cache_time = cache.readline()
        line = cache.readline()
        print(line)

    return jsonify(**eval(line))

def ip_list_sort(ret):
    """
    sort ips in the nested dict list
    :param ret:
    :return:
    """
    for region in ret:
        for ip_list in ret[region]:
            ret[region][ip_list].sort()
    return ret

@app.route('/favicon.ico')
def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')

def jsonify(status=200, indent=4, sort_keys=False, **kwargs):
    response = make_response(dumps(dict(**kwargs), indent=indent, sort_keys=sort_keys))
    response.headers['Content-Type'] = 'application/json; charset=utf-8'
    response.headers['mimetype'] = 'application/json'
    response_code = status
    return response

def _check_ssl(url, verbose=False):
    if verbose:
        print ("Current scheme: %s" % url[:5])
    if url[:5] == "https":
        return None
    else:
        return redirect("https" + url[4:], code=302)

def _write_cache(app_cache_file,data):

    with open(app_cache_file, "w+") as cache:
        cache.write(str(time.time()))
        cache.write("\n")
        cache.write(str(data))
<commit_msg>remove minor debug prints<commit_after>import awslib
from app import app
from flask import render_template
from flask import send_from_directory
import json
from json import dumps
from os.path import join
from flask import make_response, request, redirect, url_for
import os, time, pickle
import traceback

bucket_name = os.environ.get('IPLIST_CONFIG_BUCKET')
s3path = os.environ.get('IPLIST_CONFIG_PATH')
nohttps = os.environ.get('NOHTTPS')

path = join('iplist_config', 'config.json')

if s3path == None:
    print ("No Env Labeled IPLIST_CONFIG_PATH")
elif bucket_name == None:
    print ("No bucket name specified")
else:
    awslib._get_file(bucket_name, s3path, path)

#####
# Caching parameters
#####
cache_timeout_period_in_seconds = 300
cache_root_directory = "/ip-range-cache"

try:
    os.makedirs(cache_root_directory)
except:
    pass

@app.route('/')
def handle_index():
    redir = None
    if nohttps == None:
        proto = request.headers.get("X-Forwarded-Proto")
        if not proto == "https":
            redir = _check_ssl(request.url)

    if not redir == None:
        return redir

    with open(path) as json_data:
        data = json.load(json_data)

    # creating altname list for to be deprecated url links
    altapps=[]
    for app in data['apps']:
        if app.get('altname'):
            altapps.append(app['altname'])

    return render_template("index.html", apps=[app['name'] for app in data['apps']], altapps=altapps )

@app.route('/healthcheck')
def handle_healthcheck():
    return "I'm still here."

@app.route('/<appname>')
def handle_app(appname):
    verbose = False
    chosen_region = None
    query_string = request.query_string

    if not query_string == "":
        for query in query_string.split('&'):
            if "verbose" in query.lower():
                if query.endswith("1"):
                    verbose = True
            elif "region" in query.lower():
                chosen_region = query[7:]
    suffix = ".json"

    if verbose:
        suffix = ".verbose" + suffix

    if chosen_region:
        suffix = "." + chosen_region + suffix

    app_cache_file = os.path.join(cache_root_directory,appname.lower() + suffix)

    read_from_cache = True
    try:
        with open(app_cache_file, "r") as cache:
            cache_time = float(cache.readline().strip())
            current_time = time.time()
            if (current_time - cache_time) > cache_timeout_period_in_seconds:
                read_from_cache = False
    except IOError:
        read_from_cache = False

    if read_from_cache:
        print("Reading cached data for this request.")
    else:
        print("Cache is out of date. Refreshing for this request.")

        try:
            with open(path) as json_data:
                data = json.load(json_data)

            ret = {}

            if verbose:
                print (request.url)
            redir = None
            if nohttps == None:
                proto = request.headers.get("X-Forwarded-Proto")
                if not proto == "https":
                    redir = _check_ssl(request.url, verbose)
            if not redir == None:
                return redir

            for app in data['apps']:
                # create url link for both name and alternative name for ip-range apps
                if appname.lower() == app['name'].lower() or appname.lower() == str(app.get('altname')).lower():
                    app_config = app['config']

                    for config in app_config:

                        if config.get('s3filepath'):
                            datapath = config.get('localpath')
                            awslib._get_file(bucket_name, config['s3filepath'], datapath)
                            with open(datapath) as filedata:
                                output = json.load(filedata)
                            break
                        elif config.get('R53'):
                            ret = {}
                            for item in config['R53']:
                                ret[item['Name']] = {}
                                ret[item['Name']]['all_ips'] = []
                                ret[item['Name']]['all_ips'] = awslib._get_records_from_zone(item['HostedZoneId'], item['Pattern'], item['Domain'])
                            break

                        dnsname = config['dnsname']
                        bs_app = config['beanstalk_app_name']
                        region = config['region']

                        # only run next section if region equal chosen_region
                        if chosen_region:
                            if chosen_region != region:
                                continue

                        exclusions = config['exclusions']
                        eip_check = config.get('show_eip')
                        lb_check = config.get('show_lb_ip')
                        inst_check = config.get('show_inst_ip')
                        if ret.get(region) == None:
                            ret[region] = {}
                        lb_name = awslib._active_balancer(dnsname, region)

                        if ret[region].get('all_ips') == None:
                            ret[region]['all_ips'] = []

                        if not eip_check == None:
                            eips = awslib._list_eips(region, filter=exclusions)
                            if verbose:
                                if ret[region].get('eips') == None:
                                    ret[region]['eips'] = eips
                                else:
                                    ret[region]['eips'].extend(eips)

                            if eip_check:
                                ret[region]['all_ips'].extend(eips)

                        if not lb_check == None:
                            lb_url = awslib._environment_descr(bs_app, lb_name, region)
                            elb = awslib._balancer_ip(lb_url)

                            if verbose:
                                if ret[region].get('elb') == None:
                                    ret[region]['elb'] = elb
                                else:
                                    ret[region]['elb'].extend(elb)

                            if lb_check:
                                ret[region]['all_ips'].extend(elb)

                        if not inst_check == None:
                            inst_ips = awslib._instance_ip(lb_name, region)
                            if verbose:
                                if ret[region].get('instance_ips') == None:
                                    ret[region]['instance_ips'] = inst_ips
                                else:
                                    ret[region]['instance_ips'].extend(inst_ips)

                            if inst_check:
                                ret[region]['all_ips'].extend(inst_ips)

            if not ret:
                return redirect(url_for('handle_index'), code=302)
            else:
                #sort ip list in ret when it can
                ret = ip_list_sort(ret)
                _write_cache(app_cache_file,ret)
        except:
            print ("Error: Unable to load new information for app: " + str(appname))
            traceback.print_exc()

    with open(app_cache_file, "r") as cache:
        # read the first line as cache time
        cache_time = cache.readline()
        line = cache.readline()
        return jsonify(**eval(line))

def ip_list_sort(ret):
    """
    sort ips in the nested dict list
    :param ret:
    :return:
    """
    for region in ret:
        for ip_list in ret[region]:
            ret[region][ip_list].sort()
    return ret

@app.route('/favicon.ico')
def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')

def jsonify(status=200, indent=4, sort_keys=False, **kwargs):
    response = make_response(dumps(dict(**kwargs), indent=indent, sort_keys=sort_keys))
    response.headers['Content-Type'] = 'application/json; charset=utf-8'
    response.headers['mimetype'] = 'application/json'
    response_code = status
    return response

def _check_ssl(url, verbose=False):
    if verbose:
        print ("Current scheme: %s" % url[:5])
    if url[:5] == "https":
        return None
    else:
        return redirect("https" + url[4:], code=302)

def _write_cache(app_cache_file,data):

    with open(app_cache_file, "w+") as cache:
        cache.write(str(time.time()))
        cache.write("\n")
        cache.write(str(data))
<|endoftext|>


<commit_before># coding=utf-8

from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals

import os
import json
from time import gmtime, strftime

from fabric.api import env
from fabric.context_managers import cd, settings
from fabric.contrib.console import confirm
from fabric.decorators import task
from fabric.operations import os, run, local
from fabric.utils import abort
from color_printer import colors

from django_fab_deployer.exceptions import InvalidConfiguration, MissingConfiguration

DEPLOYMENT_CONFIG_FILE = "deploy.json"

def function_builder(target, options):
    def function(more_args=None):

        if "warn_on_deploy" in options and options["warn_on_deploy"]:
            if not confirm('Are you sure you want to work on *{0}* server?'.format(target.upper()), default=True):
                abort('Deployment cancelled')

        env.user = options["user"]
        env.hosts = [options["hosts"]]
        env.deploy_path = options["deploy_path"]
        env.project_name = options["project_name"]
        env.venv_path = options["venv_path"]
        env.celery_enabled = options["celery_enabled"]
        env.use_ssh_config = True

        if "key_filename" in options:
            env.key_filename = os.path.normpath(options["key_filename"])

    return function

def _prepare_hosts():
    try:
        with open(os.path.join(os.getcwd(), DEPLOYMENT_CONFIG_FILE), "r") as deploy_config_file:
            data = deploy_config_file.read()
    except IOError:
        raise MissingConfiguration(
            "Configuration file `{0}` was not found in `{1}`".format(DEPLOYMENT_CONFIG_FILE, os.getcwd())
        )

    try:
        deployment_data = json.loads(data)
    except ValueError:
        raise InvalidConfiguration()

    for target, options in deployment_data.items():
        globals()[target] = task(name=target)(function_builder(target, options))

_prepare_hosts()

def venv_run(command_to_run):
    run('source %s' % env.venv_path + ' && ' + command_to_run)

@task
def deploy(upgrade=False, *args, **kwargs):
    local("python src/manage.py validate_templates")
    local("python src/manage.py check")

    with cd(env.deploy_path):
        # Create backup
        backup()

        # Source code
        colors.blue("Pulling from git")
        run('git reset --hard')
        run('git pull --no-edit origin master')

        # Dependencies
        colors.blue("Installing bower dependencies")

        with settings(warn_only=True):  # Bower may not be installed
            run('bower prune')  # Uninstalls local extraneous packages.
            run('bower %s --config.interactive=false' % ('update' if upgrade else 'install'))

        colors.blue("Installing pip dependencies")
        venv_run('pip install --no-input --exists-action=i -r requirements/production.txt --use-wheel %s' % (
            '--upgrade' if upgrade else ''))

        # Django tasks
        colors.blue("Running Django commands")
        venv_run('python src/manage.py collectstatic --noinput')
        venv_run('python src/manage.py migrate')
        venv_run('python src/manage.py compress')

        venv_run('python src/manage.py clearsessions')
        venv_run('python src/manage.py clear_cache')
        venv_run('python src/manage.py clean_pyc')
        venv_run('python src/manage.py compilemessages')

        # Restart processes
        colors.blue("Restarting Gunicorn")

        run('supervisorctl restart {0}:{0}_gunicorn'.format(env.project_name))

        if env.celery_enabled:
            colors.blue("Restarting Celery")

            run('supervisorctl restart {0}:{0}_celeryd'.format(env.project_name))
            run('supervisorctl restart {0}:{0}_celerybeat'.format(env.project_name))

        run('supervisorctl status | grep "{0}"'.format(env.project_name))

        colors.green("Done.")

@task
def backup(*args, **kwargs):
    with cd(env.deploy_path):
        colors.blue("Creating backup")

        run("mkdir -p data/deployment_backup")

        now_time = strftime("%Y-%m-%d_%H.%M.%S", gmtime())
        venv_run(
            "python src/manage.py dumpdata --format json --all --indent=3 --output data/deployment_backup/%s-dump.json" % now_time)

        colors.green("Done.")

@task
def update_python_tools(*args, **kwargs):
    with cd(env.deploy_path):
        colors.blue("Updating Python tools")

        venv_run('easy_install --upgrade pip')
        venv_run('pip install --no-input --exists-action=i --use-wheel --upgrade setuptools wheel')

        colors.green("Done.")

@task
def restart(*args, **kwargs):
    with cd(env.deploy_path):
        colors.blue("Restarting all processes in group")

        run('supervisorctl restart {0}:*'.format(env.project_name))
        run('supervisorctl status | grep "{0}"'.format(env.project_name))

        colors.green("Done.")
<commit_msg>Introduce __all__.<commit_after># coding=utf-8

from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals

__all__ = [
    'venv_run',
    'deploy',
    'backup',
    'update_python_tools',
    'restart'
]

import os
import json
from time import gmtime, strftime

from fabric.api import env
from fabric.context_managers import cd, settings
from fabric.contrib.console import confirm
from fabric.decorators import task
from fabric.operations import os, run, local
from fabric.utils import abort
from color_printer import colors

from .exceptions import InvalidConfiguration, MissingConfiguration

DEPLOYMENT_CONFIG_FILE = "deploy.json"
DEFAULT_SOURCE_BRANCH = "master"

def function_builder(target, options):
    def function(more_args=None):

        if "warn_on_deploy" in options and options["warn_on_deploy"]:
            if not confirm('Are you sure you want to work on *{0}* server?'.format(target.upper()), default=True):
                abort('Deployment cancelled')

        env.user = options["user"]
        env.hosts = [options["hosts"]]
        env.deploy_path = options["deploy_path"]
        env.project_name = options["project_name"]
        env.venv_path = options["venv_path"]
        env.celery_enabled = options["celery_enabled"]
        env.use_ssh_config = True
        env.source_branch = options.get('source_branch', DEFAULT_SOURCE_BRANCH)

        if "key_filename" in options:
            env.key_filename = os.path.normpath(options["key_filename"])

    return function

def _prepare_hosts():
    try:
        with open(os.path.join(os.getcwd(), DEPLOYMENT_CONFIG_FILE), "r") as deploy_config_file:
            data = deploy_config_file.read()
    except IOError:
        raise MissingConfiguration(
            "Configuration file `{0}` was not found in `{1}`".format(DEPLOYMENT_CONFIG_FILE, os.getcwd())
        )

    try:
        deployment_data = json.loads(data)
    except ValueError:
        raise InvalidConfiguration()

    for target, options in deployment_data.items():
        __all__.append(target)
        globals()[target] = task(name=target)(function_builder(target, options))

_prepare_hosts()

def venv_run(command_to_run):
    run('source %s' % env.venv_path + ' && ' + command_to_run)

@task
def deploy(upgrade=False, *args, **kwargs):
    local("python src/manage.py validate_templates")
    local("python src/manage.py check")

    with cd(env.deploy_path):
        # Create backup
        backup()

        # Source code
        colors.blue("Pulling from git")
        run('git reset --hard')
        run('git pull --no-edit origin {0}'.format(env.source_branch))

        # Dependencies
        colors.blue("Installing bower dependencies")

        with settings(warn_only=True):  # Bower may not be installed
            run('bower prune')  # Uninstalls local extraneous packages.
            run('bower %s --config.interactive=false' % ('update' if upgrade else 'install'))

        colors.blue("Installing pip dependencies")
        venv_run('pip install --no-input --exists-action=i -r requirements/production.txt --use-wheel %s' % (
            '--upgrade' if upgrade else ''))

        # Django tasks
        colors.blue("Running Django commands")
        venv_run('python src/manage.py collectstatic --noinput')
        venv_run('python src/manage.py migrate')
        venv_run('python src/manage.py compress')

        venv_run('python src/manage.py clearsessions')
        venv_run('python src/manage.py clear_cache')
        venv_run('python src/manage.py clean_pyc')
        venv_run('python src/manage.py compilemessages')

        # Restart processes
        colors.blue("Restarting Gunicorn")
        colors.blue("Restarting Gunicorn")
        run('supervisorctl restart {0}:{0}_gunicorn'.format(env.project_name))

        if env.celery_enabled:
            colors.blue("Restarting Celery")

            run('supervisorctl restart {0}:{0}_celeryd'.format(env.project_name))
            run('supervisorctl restart {0}:{0}_celerybeat'.format(env.project_name))

        run('supervisorctl status | grep "{0}"'.format(env.project_name))

        colors.green("Done.")

@task
def backup(*args, **kwargs):
    with cd(env.deploy_path):
        colors.blue("Creating backup")

        run("mkdir -p data/deployment_backup")

        now_time = strftime("%Y-%m-%d_%H.%M.%S", gmtime())
        venv_run(
            "python src/manage.py dumpdata --format json --all --indent=3 --output data/deployment_backup/%s-dump.json" % now_time)

        colors.green("Done.")

@task
def update_python_tools(*args, **kwargs):
    with cd(env.deploy_path):
        colors.blue("Updating Python tools")

        venv_run('easy_install --upgrade pip')
        venv_run('pip install --no-input --exists-action=i --use-wheel --upgrade setuptools wheel')

        colors.green("Done.")

@task
def restart(*args, **kwargs):
    with cd(env.deploy_path):
        colors.blue("Restarting all processes in group")

        run('supervisorctl restart {0}:*'.format(env.project_name))
        run('supervisorctl status | grep "{0}"'.format(env.project_name))

        colors.green("Done.")
<|endoftext|>


<commit_before>from flask import render_template, send_file

from app import app
from app.models import Post

@app.errorhandler(403)
def forbidden(e):
    return render_template('403.html'), 403

@app.errorhandler(404)
def not_found(e):
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
    return render_template('500.html'), 500

@app.route('/')
@app.route('/page/<int:page>')
def blog(page=1):
    """View the blog."""
    posts = Post.query.filter_by(visible=True) \
                      .order_by(Post.published.desc())
    if posts:
        pagination = posts.paginate(page=page, per_page=Post.PER_PAGE)
    return render_template('blog.html', pagination=pagination)

@app.route('/archive')
def archive():
    """View an overview of all visible posts."""
    posts = Post.query.filter_by(visible=True) \
                      .order_by(Post.published.desc())
    return render_template('archive.html', posts=posts)

@app.route('/<path:slug>', methods=['GET', 'POST'])
def detail(slug):
    """View details of post with specified slug."""
    post = Post.query.filter_by(visible=True, slug=slug) \
                     .first_or_404()
    return render_template('detail.html', post=post)

@app.route('/admin')
def admin():
    return send_file('static/admin/index.html')
<commit_msg>Implement query with pagination differently<commit_after>from flask import render_template, send_file

from app import app
from app.models import Post

@app.errorhandler(403)
def forbidden(e):
    return render_template('403.html'), 403

@app.errorhandler(404)
def not_found(e):
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
    return render_template('500.html'), 500

@app.route('/')
@app.route('/page/<int:page>')
def blog(page=1):
    """View the blog."""
    pagination = Post.query.filter_by(visible=True) \
                           .order_by(Post.published.desc()) \
                           .paginate(page, Post.PER_PAGE, False)
    return render_template('blog.html', pagination=pagination)

@app.route('/archive')
def archive():
    """View an overview of all visible posts."""
    posts = Post.query.filter_by(visible=True) \
                      .order_by(Post.published.desc())
    return render_template('archive.html', posts=posts)

@app.route('/<path:slug>', methods=['GET', 'POST'])
def detail(slug):
    """View details of post with specified slug."""
    post = Post.query.filter_by(visible=True, slug=slug) \
                     .first_or_404()
    return render_template('detail.html', post=post)

@app.route('/admin')
def admin():
    return send_file('static/admin/index.html')
<|endoftext|>


<commit_before>import io
import json
import sys, os
from ConfigParser import SafeConfigParser
from twitter import *
from time import sleep
import math

# get list of user_ids or screen_names to fetch
def get_users(listfile):
    users = set()

    # get the list from the file we specified
    listfile = open(listfile,'r')

    # add each line to the users set
    for num, user in enumerate(listfile):
    	users.add(str(user.rstrip()))

    listfile.close() # clean up

    return users # a set of user_ids or screen_names

# query Twitter API by user_id or screen_name and dump results to JSON file
def get_user_timelines(users, outfolder, api, user_id = "False"):
    print("Getting %s user timeline(s). Storing files in %s." % (str(len(users)), outfolder))
    for user in users:
        n_tweets = 0
        is_protected = "False"
        try:
            if user_id == "True":
                user_info = api.users.show(user_id = user)
            else:
                user_info = api.users.show(screen_name = user)
                n_tweets = user_info['statuses_count']
                is_protected = str(user_info['protected'])
        except TwitterHTTPError as e:
            if e.e.code == 401 or e.e.code == 400: # Not authorized
                print("Not Authorized - Check your Twitter settings.\n Exiting.")
                print(str(e))
                sys.exit()
            elif e.e.code == 404: # Not found
                print("Oops, %s has no tweets or the account info is wrong. Moving on." % user)
                continue
        # logged in ok and found the user
        if is_protected == "True": # Users' tweets are protected, account is private
            print("Oops, tweets for %s are protected. Moving on." % user)
            continue
        elif n_tweets == 0: # User has no tweets
            print("%s has no tweets to get. Moving on." % user)
            continue
        else: # Account is public, one or more tweets to collect
            print("Getting %s tweets for %s. (Or ~3200, whichever is lower.)" % (n_tweets, user))
            n_loops = int(math.ceil(n_tweets/200.0)) # Twitter lets us get 200 at a time, max 3200
        if n_loops > 15:
            n_loops = 15
        try: # make a JSON file named user.page_number.json and put it in the folder we specified

            for i_loop in range(0, n_loops):
                outfilename = ".".join([user,str(i_loop),'json'])
                outfilename = "".join([outfolder,outfilename])
                outfile = io.open(outfilename, mode='wt', encoding='utf8')
                try:
                    if user_id == "True": # if we're searching by user_id
                        tweets = api.statuses.user_timeline(user_id = user, count = 200, page = i_loop+1)
                    else: # if we're searching by screen_name or handle
                        tweets = api.statuses.user_timeline(screen_name = user, count = 200, page = i_loop+1)
                except TwitterHTTPError as e:
                    if e.e.code == 429: # Rate limit exceeded
                        print("Rate limit exceeded. Sleeping for 15 minutes.")
                        sleep(60 * 15)
                if tweets: # we got tweets, lets dump 'em to JSON
                    outfile.write(json.dumps(tweets,ensure_ascii=False, encoding='utf8'))
                    outfile.write(u'\n')
                    outfile.flush()
                outfile.close() # clean up
            print("Done collecting.")
        except: # something went wrong, could be that Twitter's down, log it and we'll look later
            for i in sys.exc_info():
                print(i)

# Main function
if __name__ == '__main__' :

    config = SafeConfigParser()
    script_dir = os.path.dirname(__file__)
    script_dir = os.path.dirname(os.path.abspath(__file__))
    config_file = os.path.join(script_dir, '../settings.cfg')
    config.read(config_file)

    # tell script where to put the JSON files returned
    listfile = config.get('files','listfile')
    outfolder = config.get('files','outfolder')

    # should we search by user_id? default is no, use screen_name/handle
    user_id = config.get('options','user_id')

    # connect to the API using OAuth credentials
    api = Twitter(api_version='1.1', auth=OAuth(config.get('twitter', 'access_token'),
       config.get('twitter', 'access_token_secret'),
       config.get('twitter', 'consumer_key'),
       config.get('twitter', 'consumer_secret')))

    # find out who we're searching for
    users = get_users(listfile)

    # go get their tweets
    get_user_timelines(users, outfolder, api, user_id)

sys.exit()

<commit_msg>Update to retrieve extended tweets<commit_after>import io
import json
import sys, os
from ConfigParser import SafeConfigParser
from twitter import *
from time import sleep
import math

# get list of user_ids or screen_names to fetch
def get_users(listfile):
    users = set()

    # get the list from the file we specified
    listfile = open(listfile,'r')

    # add each line to the users set
    for num, user in enumerate(listfile):
    	users.add(str(user.rstrip()))

    listfile.close() # clean up

    return users # a set of user_ids or screen_names

# query Twitter API by user_id or screen_name and dump results to JSON file
def get_user_timelines(users, outfolder, api, user_id = "False"):
    print("Getting %s user timeline(s). Storing files in %s." % (str(len(users)), outfolder))
    for user in users:
        n_tweets = 0
        is_protected = "False"
        try:
            if user_id == "True":
                user_info = api.users.show(user_id = user)
            else:
                user_info = api.users.show(screen_name = user)
                n_tweets = user_info['statuses_count']
                is_protected = str(user_info['protected'])
        except TwitterHTTPError as e:
            if e.e.code == 401 or e.e.code == 400: # Not authorized
                print("Not Authorized - Check your Twitter settings.\n Exiting.")
                print(str(e))
                sys.exit()
            elif e.e.code == 404: # Not found
                print("Oops, %s has no tweets or the account info is wrong. Moving on." % user)
                continue
        # logged in ok and found the user
        if is_protected == "True": # Users' tweets are protected, account is private
            print("Oops, tweets for %s are protected. Moving on." % user)
            continue
        elif n_tweets == 0: # User has no tweets
            print("%s has no tweets to get. Moving on." % user)
            continue
        else: # Account is public, one or more tweets to collect
            print("Getting %s tweets for %s. (Or ~3200, whichever is lower.)" % (n_tweets, user))
            n_loops = int(math.ceil(n_tweets/200.0)) # Twitter lets us get 200 at a time, max 3200
        if n_loops > 15:
            n_loops = 15
        try: # make a JSON file named user.page_number.json and put it in the folder we specified

            for i_loop in range(0, n_loops):
                outfilename = ".".join([user,str(i_loop),'json'])
                outfilename = "".join([outfolder,outfilename])
                outfile = io.open(outfilename, mode='wt', encoding='utf8')
                try:
                    if user_id == "True": # if we're searching by user_id
                        tweets = api.statuses.user_timeline(tweet_mode = "extended", user_id = user, count = 200, page = i_loop+1)
                    else: # if we're searching by screen_name or handle
                        tweets = api.statuses.user_timeline(tweet_mode = "extended", screen_name = user, count = 200, page = i_loop+1)
                except TwitterHTTPError as e:
                    if e.e.code == 429: # Rate limit exceeded
                        print("Rate limit exceeded. Sleeping for 15 minutes.")
                        sleep(60 * 15)
                if tweets: # we got tweets, lets dump 'em to JSON
                    outfile.write(json.dumps(tweets,ensure_ascii=False, encoding='utf8'))
                    outfile.write(u'\n')
                    outfile.flush()
                outfile.close() # clean up
            print("Done collecting.")
        except: # something went wrong, could be that Twitter's down, log it and we'll look later
            for i in sys.exc_info():
                print(i)

# Main function
if __name__ == '__main__' :

    config = SafeConfigParser()
    script_dir = os.path.dirname(__file__)
    script_dir = os.path.dirname(os.path.abspath(__file__))
    config_file = os.path.join(script_dir, '../settings.cfg')
    config.read(config_file)

    # redirect print statements to log file
    old_stdout = sys.stdout
    log_file = open(config.get('files','logfile'),"a+")

    sys.stdout = log_file

    # tell script where to put the JSON files returned
    listfile = config.get('files','listfile')
    outfolder = config.get('files','outfolder')

    # should we search by user_id? default is no, use screen_name/handle
    user_id = config.get('options','user_id')

    # connect to the API using OAuth credentials
    api = Twitter(api_version='1.1', auth=OAuth(config.get('twitter', 'access_token'),
       config.get('twitter', 'access_token_secret'),
       config.get('twitter', 'consumer_key'),
       config.get('twitter', 'consumer_secret')))

    # find out who we're searching for
    users = get_users(listfile)

    # go get their tweets
    get_user_timelines(users, outfolder, api, user_id)

    # stop redirecting pring statements
    sys.stdout = old_stdout

    log_file.close()

sys.exit()

<|endoftext|>