<commit_before>import mimeparse

from django.http.response import HttpResponse, HttpResponseRedirect
from django.template.loader import render_to_string
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _

from pyston.utils import set_rest_context_to_request
from pyston.utils.helpers import str_to_class
from pyston.converters import (
    get_converter, get_supported_mime_types, get_converter_name_from_request, get_default_converters
)
from pyston.resource import BaseResource
from pyston.conf import settings as pyston_settings

from is_core.config import settings
from is_core.utils import str_to_class


def ui_response_exception_factory(request, response_code, title, message, response_class=HttpResponse):
    if isinstance(message, (list, tuple)):
        message = ', '.join(message)

    return response_class(
        render_to_string(
            ('{}.html'.format(response_code), 'error.html'),
            {'response_code': response_code, 'title': title, 'message': message},
            request=request
        ),
        status=response_code
    )


def rest_response_exception_factory(request, response_code, title, message, response_class=HttpResponse):
    message = (
        ', '.join([force_text(val) for val in message])
        if isinstance(message, (list, tuple))
        else force_text(message)
    ) if message else force_text(title)
    set_rest_context_to_request(request, BaseResource.DEFAULT_REST_CONTEXT_MAPPING)
    converter_name = get_converter_name_from_request(request)
    converter = get_converter(converter_name)
    response = response_class(status=response_code, content_type=converter.content_type)

    rest_error_response = str_to_class(pyston_settings.ERROR_RESPONSE_CLASS)

    converter.encode_to_stream(response, rest_error_response(msg=message, code=response_code).result)
    return response


def ui_rest_response_exception_factory(request, response_code, title, message, response_class=HttpResponse):
    rest_mime_types = list(get_supported_mime_types(get_default_converters()))
    ui_mime_types = ['text/html', 'application/xhtml+xml']

    best_match = mimeparse.best_match(rest_mime_types + ui_mime_types, request.META.get('HTTP_ACCEPT', 'text/html'))

    if best_match in ui_mime_types:
        return ui_response_exception_factory(request, response_code, title, message, response_class)
    else:
        return rest_response_exception_factory(request, response_code, title, message, response_class)<commit_msg>Added optional attribute to the rest response exception factory method<commit_after>import mimeparse

from django.http.response import HttpResponse, HttpResponseRedirect
from django.template.loader import render_to_string
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _

from pyston.utils import set_rest_context_to_request
from pyston.utils.helpers import str_to_class
from pyston.converters import (
    get_converter, get_supported_mime_types, get_converter_name_from_request, get_default_converters
)
from pyston.resource import BaseResource
from pyston.conf import settings as pyston_settings

from is_core.config import settings
from is_core.utils import str_to_class


def ui_response_exception_factory(request, response_code, title, message, response_class=HttpResponse):
    if isinstance(message, (list, tuple)):
        message = ', '.join(message)

    return response_class(
        render_to_string(
            ('{}.html'.format(response_code), 'error.html'),
            {'response_code': response_code, 'title': title, 'message': message},
            request=request
        ),
        status=response_code
    )


def rest_response_exception_factory(request, response_code, title, message, response_class=HttpResponse,
                                    rest_error_response_class=None):
    message = (
        ', '.join([force_text(val) for val in message])
        if isinstance(message, (list, tuple))
        else force_text(message)
    ) if message else force_text(title)
    set_rest_context_to_request(request, BaseResource.DEFAULT_REST_CONTEXT_MAPPING)
    converter_name = get_converter_name_from_request(request)
    converter = get_converter(converter_name)
    response = response_class(status=response_code, content_type=converter.content_type)

    rest_error_response_class = rest_error_response_class or str_to_class(pyston_settings.ERROR_RESPONSE_CLASS)

    converter.encode_to_stream(response, rest_error_response_class(msg=message, code=response_code).result)
    return response


def ui_rest_response_exception_factory(request, response_code, title, message, response_class=HttpResponse):
    rest_mime_types = list(get_supported_mime_types(get_default_converters()))
    ui_mime_types = ['text/html', 'application/xhtml+xml']

    best_match = mimeparse.best_match(rest_mime_types + ui_mime_types, request.META.get('HTTP_ACCEPT', 'text/html'))

    if best_match in ui_mime_types:
        return ui_response_exception_factory(request, response_code, title, message, response_class)
    else:
        return rest_response_exception_factory(request, response_code, title, message, response_class)
{'fraction_non_alphanumeric': 0.07745398773006135, 'fraction_numerical': 0.0, 'mean_word_length': 5.247904191616766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7564157', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1493, 'n_words': 309}