<commit_before>import requests, os, json, logging, re, datetime
import analytics
from util import local_sleep
from totalimpactwebapp import util

from flask import request, send_file, abort, make_response, g, redirect
from flask import render_template
from flask import render_template_string
from flask.ext.login import login_user, logout_user, current_user, login_required

from totalimpactwebapp import app
from totalimpactwebapp import db
from totalimpactwebapp import login_manager
from totalimpactwebapp import product
from totalimpactwebapp import cache

from totalimpactwebapp.password_reset import send_reset_token
from totalimpactwebapp.password_reset import reset_password_from_token
from totalimpactwebapp.password_reset import reset_password
from totalimpactwebapp.password_reset import PasswordResetError

from totalimpactwebapp.profile import Profile
from totalimpactwebapp.profile import create_profile_from_slug
from totalimpactwebapp.profile import get_profile_from_id
from totalimpactwebapp.profile import delete_profile
from totalimpactwebapp.profile import remove_duplicates_from_profile
from totalimpactwebapp.profile import EmailExistsError
from totalimpactwebapp.profile import delete_products_from_profile

from totalimpactwebapp.cards_factory import *
from totalimpactwebapp import emailer
from totalimpactwebapp import configs

from totalimpactwebapp.util import camel_to_snake_case
from totalimpactwebapp import views_helpers
from totalimpactwebapp import welcome_email
from totalimpactwebapp import event_monitoring
from totalimpactwebapp import notification_report

from totalimpactwebapp.reference_set import RefsetBuilder

import newrelic.agent
from sqlalchemy import orm

logger = logging.getLogger("tiwebapp.views")
analytics.init(os.getenv("SEGMENTIO_PYTHON_KEY"), log_level=logging.INFO)












###############################################################################
#
#   CONVENIENCE FUNCTIONS
#
###############################################################################



def json_resp_from_thing(thing):
    my_dict = util.todict(thing)
    json_str = json.dumps(my_dict, sort_keys=True, indent=4)

    if request.path.endswith(".json") and (os.getenv("FLASK_DEBUG", False) == "True"):
        logger.info(u"rendering output through debug_api.html template")
        resp = make_response(render_template(
            'debug_api.html',
            data=json_str))
        resp.mimetype = "text/html"
        return views_helpers.bust_caches(resp)

    resp = make_response(json_str, 200)
    resp.mimetype = "application/json"
    return views_helpers.bust_caches(resp)




def abort_json(status_code, msg):
    body_dict = {
        "HTTP_status_code": status_code,
        "message": msg,
        "error": True
    }

    resp_string = json.dumps(body_dict, sort_keys=True, indent=4)
    resp = make_response(resp_string, status_code)
    resp.mimetype = "application/json"

    abort(resp)


def get_user_for_response(id, request, expunge=True):
    id_type = unicode(request.args.get("id_type", "url_slug"))

    try:
        logged_in = unicode(getattr(current_user, id_type)) == id
    except AttributeError:
        logged_in = False

    retrieved_user = get_profile_from_id(id, id_type, show_secrets=logged_in)

    if retrieved_user is None:
        logger.debug(u"in get_user_for_response, user {id} doesn't exist".format(
            id=id))
        abort(404, "That user doesn't exist.")

    g.profile_slug = retrieved_user.url_slug

    if expunge and os.getenv("EXPUNGE", "False")=="True":
        logger.debug(u"expunging")

        db.session.expunge_all()

    return retrieved_user




def make_js_response(template_name, **kwargs):
    rendered = render_template(template_name, **kwargs)
    resp = make_response(rendered)
    resp.headers["Content-Type"] = "application/javascript; charset=utf-8"
    return resp


def has_admin_authorization():
    return request.args.get("key", "") == os.getenv("API_ADMIN_KEY")


def abort_if_user_not_logged_in(profile):
    allowed = True
    try:
        if current_user.id != profile.id:
            abort_json(401, "You can't do this because it's not your profile.")
    except AttributeError:
        abort_json(405, "You can't do this because you're not logged in.")


def is_logged_in(profile):
    try:
        return current_user.id != profile.id
    except AttributeError:
        return False



def current_user_owns_tiid(tiid):
    profile = db.session.query(Profile).get(int(current_user.id))
    db.session.expunge(profile)
    return tiid in profile.tiids





###############################################################################
#
#   BEFORE AND AFTER REQUESTS
#
###############################################################################

@login_manager.user_loader
def load_user(profile_id):
    # load just the profile table, and don't keep it hooked up to sqlalchemy
    profile = db.session.query(Profile).options(orm.noload('*')).get(int(profile_id))
    db.session.expunge(profile)
    return profile



@app.before_request
def redirect_to_https():
    try:
        if request.headers["X-Forwarded-Proto"] == "https":
            pass
        else:
            return redirect(request.url.replace("http://", "https://"), 301)  # permanent
    except KeyError:
        #logger.debug(u"There's no X-Forwarded-Proto header; assuming localhost, serving http.")
        pass

<commit_msg>catch error if no profile<commit_after>import requests, os, json, logging, re, datetime
import analytics
from util import local_sleep
from totalimpactwebapp import util

from flask import request, send_file, abort, make_response, g, redirect
from flask import render_template
from flask import render_template_string
from flask.ext.login import login_user, logout_user, current_user, login_required

from totalimpactwebapp import app
from totalimpactwebapp import db
from totalimpactwebapp import login_manager
from totalimpactwebapp import product
from totalimpactwebapp import cache

from totalimpactwebapp.password_reset import send_reset_token
from totalimpactwebapp.password_reset import reset_password_from_token
from totalimpactwebapp.password_reset import reset_password
from totalimpactwebapp.password_reset import PasswordResetError

from totalimpactwebapp.profile import Profile
from totalimpactwebapp.profile import create_profile_from_slug
from totalimpactwebapp.profile import get_profile_from_id
from totalimpactwebapp.profile import delete_profile
from totalimpactwebapp.profile import remove_duplicates_from_profile
from totalimpactwebapp.profile import EmailExistsError
from totalimpactwebapp.profile import delete_products_from_profile

from totalimpactwebapp.cards_factory import *
from totalimpactwebapp import emailer
from totalimpactwebapp import configs

from totalimpactwebapp.util import camel_to_snake_case
from totalimpactwebapp import views_helpers
from totalimpactwebapp import welcome_email
from totalimpactwebapp import event_monitoring
from totalimpactwebapp import notification_report

from totalimpactwebapp.reference_set import RefsetBuilder

import newrelic.agent
from sqlalchemy import orm

logger = logging.getLogger("tiwebapp.views")
analytics.init(os.getenv("SEGMENTIO_PYTHON_KEY"), log_level=logging.INFO)












###############################################################################
#
#   CONVENIENCE FUNCTIONS
#
###############################################################################



def json_resp_from_thing(thing):
    my_dict = util.todict(thing)
    json_str = json.dumps(my_dict, sort_keys=True, indent=4)

    if request.path.endswith(".json") and (os.getenv("FLASK_DEBUG", False) == "True"):
        logger.info(u"rendering output through debug_api.html template")
        resp = make_response(render_template(
            'debug_api.html',
            data=json_str))
        resp.mimetype = "text/html"
        return views_helpers.bust_caches(resp)

    resp = make_response(json_str, 200)
    resp.mimetype = "application/json"
    return views_helpers.bust_caches(resp)




def abort_json(status_code, msg):
    body_dict = {
        "HTTP_status_code": status_code,
        "message": msg,
        "error": True
    }

    resp_string = json.dumps(body_dict, sort_keys=True, indent=4)
    resp = make_response(resp_string, status_code)
    resp.mimetype = "application/json"

    abort(resp)


def get_user_for_response(id, request, expunge=True):
    id_type = unicode(request.args.get("id_type", "url_slug"))

    try:
        logged_in = unicode(getattr(current_user, id_type)) == id
    except AttributeError:
        logged_in = False

    retrieved_user = get_profile_from_id(id, id_type, show_secrets=logged_in)

    if retrieved_user is None:
        logger.debug(u"in get_user_for_response, user {id} doesn't exist".format(
            id=id))
        abort(404, "That user doesn't exist.")

    g.profile_slug = retrieved_user.url_slug

    if expunge and os.getenv("EXPUNGE", "False")=="True":
        logger.debug(u"expunging")

        db.session.expunge_all()

    return retrieved_user




def make_js_response(template_name, **kwargs):
    rendered = render_template(template_name, **kwargs)
    resp = make_response(rendered)
    resp.headers["Content-Type"] = "application/javascript; charset=utf-8"
    return resp


def has_admin_authorization():
    return request.args.get("key", "") == os.getenv("API_ADMIN_KEY")


def abort_if_user_not_logged_in(profile):
    allowed = True
    try:
        if current_user.id != profile.id:
            abort_json(401, "You can't do this because it's not your profile.")
    except AttributeError:
        abort_json(405, "You can't do this because you're not logged in.")


def is_logged_in(profile):
    try:
        return current_user.id != profile.id
    except AttributeError:
        return False



def current_user_owns_tiid(tiid):
    profile = db.session.query(Profile).get(int(current_user.id))
    db.session.expunge(profile)
    return tiid in profile.tiids





###############################################################################
#
#   BEFORE AND AFTER REQUESTS
#
###############################################################################

@login_manager.user_loader
def load_user(profile_id):
    # load just the profile table, and don't keep it hooked up to sqlalchemy
    profile = db.session.query(Profile).options(orm.noload('*')).get(int(profile_id))
    if profile:
        db.session.expunge(profile)
    return profile



@app.before_request
def redirect_to_https():
    try:
        if request.headers["X-Forwarded-Proto"] == "https":
            pass
        else:
            return redirect(request.url.replace("http://", "https://"), 301)  # permanent
    except KeyError:
        #logger.debug(u"There's no X-Forwarded-Proto header; assuming localhost, serving http.")
        pass


{'fraction_non_alphanumeric': 0.13703024747937673, 'fraction_numerical': 0.003299725022914757, 'mean_word_length': 4.569678407350689, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5279948', 'n_tokens_mistral': 3310, 'n_tokens_neox': 3122, 'n_words': 746}