<commit_before>import logging
from functools import wraps
import re
import math

import flask
from flask import request, redirect, url_for, Blueprint
from flask.ext.security import current_user
from flask.ext.security.decorators import _check_token, _check_http_auth
from werkzeug.exceptions import HTTPException
from sqlalchemy import desc
from sqlalchemy.orm import lazyload, joinedload
from sqlalchemy.orm.exc import NoResultFound

from pmg import db, app
from pmg.search import Search
from pmg.models import *  # noqa
from pmg.models.base import resource_slugs
import pmg.models.serializers as serializers

logger = logging.getLogger(__name__)

api = Blueprint('api', __name__)


def load_user():
    login_mechanisms = {
        'token': lambda: _check_token(),
        'basic': lambda: _check_http_auth(),
        'session': lambda: current_user.is_authenticated()
    }

    def wrapper(fn):
        @wraps(fn)
        def decorated_view(*args, **kwargs):
            for mechanism in login_mechanisms.itervalues():
                if mechanism():
                    break
            return fn(*args, **kwargs)
        return decorated_view
    return wrapper


class ApiException(HTTPException):

    """
    Class for handling all of our expected API errors.
    """

    def __init__(self, status_code, message):
        super(ApiException, self).__init__(message)
        self.code = status_code

    def to_dict(self):
        rv = {
            "code": self.code,
            "message": self.description
        }
        return rv

    def get_response(self, environ=None):
        response = flask.jsonify(self.to_dict())
        response.status_code = self.code
        response.headers['Access-Control-Allow-Origin'] = "*"
        return response


def get_filters():
    filters = []
    args = request.args.to_dict()

    for key in args:
        if "filter" in key:
            fieldname = re.search("filter\[(.*)\]", key).group(1)
            value = args[key]

            if fieldname:
                if fieldname.endswith('_id'):
                    try:
                        value = int(value)
                    except ValueError:
                        continue
                filters.append({fieldname: value})

    return filters


def api_resource_list(resource, resource_id, base_query):
    # validate paging parameters
    page = 0
    per_page = app.config['RESULTS_PER_PAGE']
    if request.args.get('page'):
        try:
            page = int(request.args.get('page'))
        except ValueError:
            raise ApiException(422, "Please specify a valid 'page'.")

    for f in get_filters():
        base_query = base_query.filter_by(**f)

    if resource_id:
        try:
            queryset = base_query.filter_by(id=resource_id).one()
            count = 1
        except NoResultFound:
            raise ApiException(404, "Not found")
    else:
        queryset = base_query.limit(per_page).offset(page * per_page).all()
        count = base_query.count()

    next = None
    if count > (page + 1) * per_page:
        args = request.args.to_dict()
        args.update(request.view_args)
        args['page'] = page + 1
        # TODO: this isn't great, it allows users to pass in keyword params just by passing<commit_msg>Support custom per_page param in the API<commit_after>import logging
from functools import wraps
import re
import math

import flask
from flask import request, redirect, url_for, Blueprint
from flask.ext.security import current_user
from flask.ext.security.decorators import _check_token, _check_http_auth
from werkzeug.exceptions import HTTPException
from sqlalchemy import desc
from sqlalchemy.orm import lazyload, joinedload
from sqlalchemy.orm.exc import NoResultFound

from pmg import db, app
from pmg.search import Search
from pmg.models import *  # noqa
from pmg.models.base import resource_slugs
import pmg.models.serializers as serializers

logger = logging.getLogger(__name__)

api = Blueprint('api', __name__)


def load_user():
    login_mechanisms = {
        'token': lambda: _check_token(),
        'basic': lambda: _check_http_auth(),
        'session': lambda: current_user.is_authenticated()
    }

    def wrapper(fn):
        @wraps(fn)
        def decorated_view(*args, **kwargs):
            for mechanism in login_mechanisms.itervalues():
                if mechanism():
                    break
            return fn(*args, **kwargs)
        return decorated_view
    return wrapper


class ApiException(HTTPException):

    """
    Class for handling all of our expected API errors.
    """

    def __init__(self, status_code, message):
        super(ApiException, self).__init__(message)
        self.code = status_code

    def to_dict(self):
        rv = {
            "code": self.code,
            "message": self.description
        }
        return rv

    def get_response(self, environ=None):
        response = flask.jsonify(self.to_dict())
        response.status_code = self.code
        response.headers['Access-Control-Allow-Origin'] = "*"
        return response


def get_filters():
    filters = []
    args = request.args.to_dict()

    for key in args:
        if "filter" in key:
            fieldname = re.search("filter\[(.*)\]", key).group(1)
            value = args[key]

            if fieldname:
                if fieldname.endswith('_id'):
                    try:
                        value = int(value)
                    except ValueError:
                        continue
                filters.append({fieldname: value})

    return filters


def api_resource_list(resource, resource_id, base_query):
    # validate paging parameters
    per_page = app.config['RESULTS_PER_PAGE']
    try:
        per_page = max(min(per_page, int(request.args.get('per_page', per_page))), 1)
    except ValueError:
        pass

    try:
        page = int(request.args.get('page', 0))
    except ValueError:
        raise ApiException(422, "Please specify a valid 'page'.")

    for f in get_filters():
        base_query = base_query.filter_by(**f)

    if resource_id:
        try:
            queryset = base_query.filter_by(id=resource_id).one()
            count = 1
        except NoResultFound:
            raise ApiException(404, "Not found")
    else:
        queryset = base_query.limit(per_page).offset(page * per_page).all()
        count = base_query.count()

    next = None
    if count > (page + 1) * per_page:
        args = request.args.to_dict()
        args.update(request.view_args)
        args['page'] = page + 1
        # TODO: this isn't great, it allows users to pass in keyword params just by passing
{'fraction_non_alphanumeric': 0.09633303276224828, 'fraction_numerical': 0.0034565674782085962, 'mean_word_length': 2.3713272543059776, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, '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': '4345861', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1868, 'n_words': 513}