<commit_before>import uuid
from functools import wraps

from flask import request, jsonify, Response, abort
from flask.views import MethodView
from sqlalchemy import desc, inspect, Integer
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.dialects import postgresql
from cornflake import fields, serializers
from cornflake.exceptions import ValidationError

from radar.auth.sessions import current_user
from radar.database import db
from radar.exceptions import PermissionDenied, NotFound, BadRequest
from radar.utils import snake_case_keys, camel_case_keys


def parse_args(serializer_class, args=None):
    if args is None:
        args = request.args

    # Remove empty arguments
    args = {k: v for k, v in args.items() if len(v.strip()) > 0}

    # Camel case to snake case
    args = snake_case_keys(args)

    context = {'user': current_user._get_current_object()}

    serializer = serializer_class(data=args, context=context)
    serializer.is_valid(raise_exception=True)
    data = serializer.validated_data

    return data


class ApiView(MethodView):
    def dispatch_request(self, *args, **kwargs):
        try:
            return super(ApiView, self).dispatch_request(*args, **kwargs)
        except BadRequest:
            abort(400)
        except PermissionDenied:
            abort(403)
        except NotFound:
            abort(404)
        except ValidationError as e:
            print e.errors
            errors = camel_case_keys(e.errors)
            return jsonify(errors=errors), 422


class PermissionViewMixin(object):
    permission_classes = []

    def check_permissions(self):
        for permission in self.get_permissions():
            if not permission.has_permission(request, current_user):
                print 'Denied by', permission
                raise PermissionDenied()

    def check_object_permissions(self, obj):
        for permission in self.get_permissions():
            if not permission.has_object_permission(request, current_user, obj):
                print 'Denied by', permission
                raise PermissionDenied()

    def get_permission_classes(self):
        return list(self.permission_classes)

    def get_permissions(self):
        return [permission() for permission in self.get_permission_classes()]

    def dispatch_request(self, *args, **kwargs):
        try:
            self.check_permissions()
        except PermissionDenied:
            abort(403)

        return super(PermissionViewMixin, self).dispatch_request(*args, **kwargs)


class SerializerViewMixin(object):
    serializer_class = None

    def get_serializer_class(self):
        return self.serializer_class

    def get_serializer(self, instance=None, data=None, partial=False):
        if data is not None:
            data = snake_case_keys(data)

        context = self.get_context()
        serializer_class = self.get_serializer_class()
        serializer = serializer_class(instance=instance, data=data, context=context, partial=partial)
        return serializer

    def get_context(self):
        return {'user': current_user._get_current_object()}


class BaseView(SerializerViewMixin, PermissionViewMixin, ApiView):
    pass


class SortRequestSerializer(serializers.Serializer):
    sort = fields.StringField(required=False)


class PaginationRequestSerializer(serializers.Serializer):
    page = fields.IntegerField(required=False, default=1)
    per_page = fields.IntegerField(required=False)


class PaginationResponseSerializer(serializers.Serializer):
    page = fields.IntegerField()
    per_page = fields.IntegerField()
    count = fields.IntegerField()


class ModelView(SerializerViewMixin, PermissionViewMixin, ApiView):
    sort_fields = {}
    model_class = None

    def filter_query(self, query):
        return query

    def get_sort_args(self):
        args = parse_args(SortRequestSerializer)

        sort = args['sort']

        if sort:
            if sort[0] == '-':
                return sort[1:], True
            else:
                return sort, False
        else:
            return None, False

    def sort_query(self, query):
        sort, reverse = self.get_sort_args()

        if sort is not None:
            sort_fields = self.get_sort_fields()

            if isinstance(sort_fields, dict):
                expression = sort_fields.get(sort)
            elif sort in sort_fields:
                model_class = self.get_model_class()
                expression = getattr(model_class, sort)
            else:
                expression = None

            if expression is not None:
                if reverse:
                    expression = desc(expression)

                query = query.order_by(expression)

        return query<commit_msg>Transform sort keys<commit_after>import uuid
from functools import wraps

from flask import request, jsonify, Response, abort
from flask.views import MethodView
from sqlalchemy import desc, inspect, Integer
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.dialects import postgresql
from cornflake import fields, serializers
from cornflake.exceptions import ValidationError

from radar.auth.sessions import current_user
from radar.database import db
from radar.exceptions import PermissionDenied, NotFound, BadRequest
from radar.utils import snake_case_keys, camel_case_keys, snake_case


def parse_args(serializer_class, args=None):
    if args is None:
        args = request.args

    # Remove empty arguments
    args = {k: v for k, v in args.items() if len(v.strip()) > 0}

    # Camel case to snake case
    args = snake_case_keys(args)

    context = {'user': current_user._get_current_object()}

    serializer = serializer_class(data=args, context=context)
    serializer.is_valid(raise_exception=True)
    data = serializer.validated_data

    return data


class ApiView(MethodView):
    def dispatch_request(self, *args, **kwargs):
        try:
            return super(ApiView, self).dispatch_request(*args, **kwargs)
        except BadRequest:
            abort(400)
        except PermissionDenied:
            abort(403)
        except NotFound:
            abort(404)
        except ValidationError as e:
            print e.errors
            errors = camel_case_keys(e.errors)
            return jsonify(errors=errors), 422


class PermissionViewMixin(object):
    permission_classes = []

    def check_permissions(self):
        for permission in self.get_permissions():
            if not permission.has_permission(request, current_user):
                print 'Denied by', permission
                raise PermissionDenied()

    def check_object_permissions(self, obj):
        for permission in self.get_permissions():
            if not permission.has_object_permission(request, current_user, obj):
                print 'Denied by', permission
                raise PermissionDenied()

    def get_permission_classes(self):
        return list(self.permission_classes)

    def get_permissions(self):
        return [permission() for permission in self.get_permission_classes()]

    def dispatch_request(self, *args, **kwargs):
        try:
            self.check_permissions()
        except PermissionDenied:
            abort(403)

        return super(PermissionViewMixin, self).dispatch_request(*args, **kwargs)


class SerializerViewMixin(object):
    serializer_class = None

    def get_serializer_class(self):
        return self.serializer_class

    def get_serializer(self, instance=None, data=None, partial=False):
        if data is not None:
            data = snake_case_keys(data)

        context = self.get_context()
        serializer_class = self.get_serializer_class()
        serializer = serializer_class(instance=instance, data=data, context=context, partial=partial)
        return serializer

    def get_context(self):
        return {'user': current_user._get_current_object()}


class BaseView(SerializerViewMixin, PermissionViewMixin, ApiView):
    pass


class SortRequestSerializer(serializers.Serializer):
    sort = fields.StringField(required=False)


class PaginationRequestSerializer(serializers.Serializer):
    page = fields.IntegerField(required=False, default=1)
    per_page = fields.IntegerField(required=False)


class PaginationResponseSerializer(serializers.Serializer):
    page = fields.IntegerField()
    per_page = fields.IntegerField()
    count = fields.IntegerField()


class ModelView(SerializerViewMixin, PermissionViewMixin, ApiView):
    sort_fields = {}
    model_class = None

    def filter_query(self, query):
        return query

    def get_sort_args(self):
        args = parse_args(SortRequestSerializer)

        sort = args['sort']

        if sort:
            if sort[0] == '-':
                reverse = True
                sort = sort[1:]
            else:
                reverse = False

            return snake_case(sort), reverse
        else:
            return None, False

    def sort_query(self, query):
        sort, reverse = self.get_sort_args()

        if sort is not None:
            sort_fields = self.get_sort_fields()

            if isinstance(sort_fields, dict):
                expression = sort_fields.get(sort)
            elif sort in sort_fields:
                model_class = self.get_model_class()
                expression = getattr(model_class, sort)
            else:
                expression = None

            if expression is not None:
                if reverse:
                    expression = desc(expression)

                query = query.order_by(expression)

        return query
{'fraction_non_alphanumeric': 0.07827809047321242, 'fraction_numerical': 0.003960808838857619, 'mean_word_length': 2.73491630984819, 'pattern_counts': {'":': 0, '<': 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': '331039', 'n_tokens_mistral': 2659, 'n_tokens_neox': 2517, 'n_words': 663}