<commit_before># coding: utf-8
from __future__ import absolute_import
import inspect

from celery import Task
from kombu.utils import symbol_by_name
from django.db.models.base import Model
from rest_framework.serializers import ModelSerializer

from . import config
from .app import rpc
from .exceptions import RestFrameworkError


class ModelTask(Task):
    """ Base task for operating with django models.
    """
    abstract = True

    def __call__(self, model,  *args, **kwargs):
        """ Prepare context for calling task function.
        """
        self.request.model = self._import_model(model)
        args = [model] + list(args)
        return self.run(*args, **kwargs)

    @staticmethod
    def _import_model(model_name):
        """ Import class by full name, check type and return.
        """
        sym = symbol_by_name(model_name)
        if inspect.isclass(sym) and issubclass(sym, Model):
            return sym
        raise TypeError(
            "Symbol '{}' is not a Django model".format(model_name))

    @staticmethod
    def _create_queryset(model):
        """ Construct queryset by params.
        """
        return model.objects.all()

    def _create_serializer_class(self, model_class):
        """ Return REST framework serializer class for model.
        """
        class GenericModelSerializer(ModelSerializer):
            class Meta:
                model = model_class

            def get_identity(self, data):
                pk_name = self.Meta.model._meta.pk.attname
                try:
                    return data.get('pk', data.get(pk_name, None))
                except AttributeError:
                    return None

        return GenericModelSerializer

    @property
    def model(self):
        return self.request.model

    @property
    def pk_name(self):
        return self.model._meta.pk.attname

    @property
    def default_queryset(self):
        return self._create_queryset(self.model)

    @property
    def serializer_class(self):
        return self._create_serializer_class(self.model)


@rpc.task(name='celery_rpc.filter', bind=True, base=ModelTask)
def filter(self, model, filters=None, offset=0,
           limit=config.FILTER_LIMIT, fields=None,  exclude=[],
           depth=0, manager='objects', database=None, *args, **kwargs):
    """ Filter Django models and return serialized queryset.

    :param model: full name of model class like 'app.models:Model'
    :param filters: filter supported by model manager like {'pk__in': [1,2,3]}
    :param offset: offset of first item in the queryset (by default 0)
    :param limit: max number of result list (by default 1000)
    :return: list of serialized model data

    """
    filters = filters if isinstance(filters, dict) else {}
    qs = self.default_queryset.filter(**filters)[offset:offset+limit]
    return self.serializer_class(instance=qs, many=True).data


class ModelChangeTask(ModelTask):
    """ Abstract task provides ability to changing model state.
    """
    def get_instance(self, data):
        """ Prepare instance (or several instances) to changes.

        :param data: data for changing model
        :return: (Model instance or queryset, many flag)
        Many flag is True,

        """
        pk_name = self.pk_name
        get_pk_value = lambda item: item.get('pk', item.get(pk_name))<commit_msg>Set abstract = True for ModelChangeTask<commit_after># coding: utf-8
from __future__ import absolute_import
import inspect

from celery import Task
from kombu.utils import symbol_by_name
from django.db.models.base import Model
from rest_framework.serializers import ModelSerializer

from . import config
from .app import rpc
from .exceptions import RestFrameworkError


class ModelTask(Task):
    """ Base task for operating with django models.
    """
    abstract = True

    def __call__(self, model,  *args, **kwargs):
        """ Prepare context for calling task function.
        """
        self.request.model = self._import_model(model)
        args = [model] + list(args)
        return self.run(*args, **kwargs)

    @staticmethod
    def _import_model(model_name):
        """ Import class by full name, check type and return.
        """
        sym = symbol_by_name(model_name)
        if inspect.isclass(sym) and issubclass(sym, Model):
            return sym
        raise TypeError(
            "Symbol '{}' is not a Django model".format(model_name))

    @staticmethod
    def _create_queryset(model):
        """ Construct queryset by params.
        """
        return model.objects.all()

    def _create_serializer_class(self, model_class):
        """ Return REST framework serializer class for model.
        """
        class GenericModelSerializer(ModelSerializer):
            class Meta:
                model = model_class

            def get_identity(self, data):
                pk_name = self.Meta.model._meta.pk.attname
                try:
                    return data.get('pk', data.get(pk_name, None))
                except AttributeError:
                    return None

        return GenericModelSerializer

    @property
    def model(self):
        return self.request.model

    @property
    def pk_name(self):
        return self.model._meta.pk.attname

    @property
    def default_queryset(self):
        return self._create_queryset(self.model)

    @property
    def serializer_class(self):
        return self._create_serializer_class(self.model)


@rpc.task(name='celery_rpc.filter', bind=True, base=ModelTask)
def filter(self, model, filters=None, offset=0,
           limit=config.FILTER_LIMIT, fields=None,  exclude=[],
           depth=0, manager='objects', database=None, *args, **kwargs):
    """ Filter Django models and return serialized queryset.

    :param model: full name of model class like 'app.models:Model'
    :param filters: filter supported by model manager like {'pk__in': [1,2,3]}
    :param offset: offset of first item in the queryset (by default 0)
    :param limit: max number of result list (by default 1000)
    :return: list of serialized model data

    """
    filters = filters if isinstance(filters, dict) else {}
    qs = self.default_queryset.filter(**filters)[offset:offset+limit]
    return self.serializer_class(instance=qs, many=True).data


class ModelChangeTask(ModelTask):
    """ Abstract task provides ability to changing model state.
    """
    abstract = True

    def get_instance(self, data):
        """ Prepare instance (or several instances) to changes.

        :param data: data for changing model
        :return: (Model instance or queryset, many flag)
        Many flag is True,

        """
        pk_name = self.pk_name
        get_pk_value = lambda item: item.get('pk', item.get(pk_name))
{'fraction_non_alphanumeric': 0.09492828626349253, 'fraction_numerical': 0.0032529942333284047, 'mean_word_length': 2.7083333333333335, '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': '4967970', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1820, 'n_words': 567}