<commit_before>from __future__ import absolute_import

from django import get_version
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.http import Http404, HttpResponse, HttpResponseBadRequest
from django.db.models.loading import get_model

from .base import mimetype_map
from .datasets import SimpleDataset


def export(request, queryset=None, model=None, headers=None, file_type='xls',
           filename='export'):
    if queryset is None:
        queryset = model.objects.all()

    dataset = SimpleDataset(queryset, headers=headers)
    filename = '%s.%s' % (filename, file_type)
    if not hasattr(dataset, file_type):
        raise Http404

    response_kwargs = {}
    key = 'content_type' if get_version().split('.')[1] > 6 else 'mimetype'
    response_kwargs[key] = mimetype_map.get(
        file_type, 'application/octet-stream')

    response = HttpResponse(getattr(dataset, file_type), **response_kwargs)

    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    return response


def generic_export(request, model_name=None):
    """
    Generic view configured through settings.TABLIB_MODELS

    Usage:
        1. Add the view to ``urlpatterns`` in ``urls.py``::
            url(r'export/(?P<model_name>[^/]+)/$', "django_tablib.views.generic_export"),
        2. Create the ``settings.TABLIB_MODELS`` dictionary using model names
           as keys the allowed lookup operators as values, if any::

           TABLIB_MODELS = {
               'myapp.simple': None,
               'myapp.related': {'simple__title': ('exact', 'iexact')},
           }
        3. Open ``/export/myapp.simple`` or
           ``/export/myapp.related/?simple__title__iexact=test``
    """

    if model_name not in settings.TABLIB_MODELS:
        raise Http404()

    model = get_model(*model_name.split(".", 2))
    if not model:
        raise ImproperlyConfigured(
            "Model %s is in settings.TABLIB_MODELS but"
            " could not be loaded" % model_name)

    qs = model._default_manager.all()
<commit_msg>Remove deprecated package<commit_after>from __future__ import absolute_import

from django import get_version
from django.apps import apps
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.http import Http404, HttpResponse, HttpResponseBadRequest

from .base import mimetype_map
from .datasets import SimpleDataset


def export(request, queryset=None, model=None, headers=None, file_type='xls',
           filename='export'):
    if queryset is None:
        queryset = model.objects.all()

    dataset = SimpleDataset(queryset, headers=headers)
    filename = '%s.%s' % (filename, file_type)
    if not hasattr(dataset, file_type):
        raise Http404

    response_kwargs = {}
    key = 'content_type' if get_version().split('.')[1] > 6 else 'mimetype'
    response_kwargs[key] = mimetype_map.get(
        file_type, 'application/octet-stream')

    response = HttpResponse(getattr(dataset, file_type), **response_kwargs)

    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    return response


def generic_export(request, model_name=None):
    """
    Generic view configured through settings.TABLIB_MODELS

    Usage:
        1. Add the view to ``urlpatterns`` in ``urls.py``::
            url(r'export/(?P<model_name>[^/]+)/$', "django_tablib.views.generic_export"),
        2. Create the ``settings.TABLIB_MODELS`` dictionary using model names
           as keys the allowed lookup operators as values, if any::

           TABLIB_MODELS = {
               'myapp.simple': None,
               'myapp.related': {'simple__title': ('exact', 'iexact')},
           }
        3. Open ``/export/myapp.simple`` or
           ``/export/myapp.related/?simple__title__iexact=test``
    """

    if model_name not in settings.TABLIB_MODELS:
        raise Http404()

    model = apps.get_model(*model_name.split(".", 2))
    if not model:
        raise ImproperlyConfigured(
            "Model %s is in settings.TABLIB_MODELS but"
            " could not be loaded" % model_name)

    qs = model._default_manager.all()

{'fraction_non_alphanumeric': 0.11302270011947431, 'fraction_numerical': 0.007168458781362007, 'mean_word_length': 3.211267605633803, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5179372', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1212, 'n_words': 320}