<commit_before># -*- coding: utf-8 -*-

from __future__ import absolute_import
import urlparse
import re
from flask import request, url_for, json, Response

__jsoncallback_re = re.compile(r'^[a-z$_][0-9a-z$_]*$', re.I)


#TODO: This needs tests
def get_next_url(referrer=False, external=False):
    """
    Get the next URL to redirect to. Don't return external URLs unless
    explicitly asked for. This is to protect the site from being an unwitting
    redirector to external URLs.
    """
    next_url = request.args.get('next', '')
    if not external:
        if next_url.startswith('http:') or next_url.startswith('https:') or next_url.startswith('//'):
            # Do the domains match?
            if urlparse.urlsplit(next_url).hostname != urlparse.urlsplit(request.url).hostname:
                next_url = ''
    if referrer:
        return next_url or request.referrer or url_for('index')
    else:
        return next_url or url_for('index')


#TODO: This needs tests
def jsonp(*args, **kw):
    """
    Returns a JSON response with a callback wrapper, if asked for.
    """
    data = json.dumps(dict(*args, **kw),
        indent=None if request.is_xhr else 2)
    callback = request.args.get('callback', request.args.get('jsonp'))
    if callback and __jsoncallback_re.search(callback) is not None:
        data = u'%s(' % callback + data + u');'
        mimetype = 'application/javascript'
    else:
        mimetype = 'application/json'
    return Response(data, mimetype=mimetype)
<commit_msg>Nifty new load_model decorator to make view logic simpler.<commit_after># -*- coding: utf-8 -*-

from __future__ import absolute_import
from functools import wraps
import urlparse
import re
from flask import request, url_for, json, Response

__jsoncallback_re = re.compile(r'^[a-z$_][0-9a-z$_]*$', re.I)


#TODO: This needs tests
def get_next_url(referrer=False, external=False):
    """
    Get the next URL to redirect to. Don't return external URLs unless
    explicitly asked for. This is to protect the site from being an unwitting
    redirector to external URLs.
    """
    next_url = request.args.get('next', '')
    if not external:
        if next_url.startswith('http:') or next_url.startswith('https:') or next_url.startswith('//'):
            # Do the domains match?
            if urlparse.urlsplit(next_url).hostname != urlparse.urlsplit(request.url).hostname:
                next_url = ''
    if referrer:
        return next_url or request.referrer or url_for('index')
    else:
        return next_url or url_for('index')


#TODO: This needs tests
def jsonp(*args, **kw):
    """
    Returns a JSON response with a callback wrapper, if asked for.
    """
    data = json.dumps(dict(*args, **kw),
        indent=None if request.is_xhr else 2)
    callback = request.args.get('callback', request.args.get('jsonp'))
    if callback and __jsoncallback_re.search(callback) is not None:
        data = u'%s(' % callback + data + u');'
        mimetype = 'application/javascript'
    else:
        mimetype = 'application/json'
    return Response(data, mimetype=mimetype)


def load_model(model, attributes=None, parameter=None, workflow=False):
    """
    Decorator to load a model given a parameter.
    """
    if isinstance(model, list):
        chain = model
    else:
        if attributes is None or parameter is None:
            raise ValueError('attributes and parameter are needed to load a model.')
        chain = [[model, attributes, parameter]]

    def inner(f):
        @wraps(f)
        def decorated_function(**kw):
            result = {}
            for model, attributes, parameter in chain:
                query = model.query
                for k, v in attributes.items():
                    query = query.filter_by(**{k: result.get(v, kw.get(v))})
                item = query.first_or_404()
                result[parameter] = item
            if workflow:
                # Get workflow for the last item in the chain
                wf = item.workflow()
                return f(wf)
            else:
                return f(**result)
        return decorated_function
    return inner

{'fraction_non_alphanumeric': 0.1123703882324572, 'fraction_numerical': 0.002652519893899204, 'mean_word_length': 2.6322241681260947, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '1406510', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1218, 'n_words': 378}