<commit_before>from rest_framework.response import Response
from rest_framework.decorators import api_view
from ac_mediator.exceptions import *
from api.request_distributor import get_request_distributor
from api.response_aggregator import get_response_aggregator
from services.management import get_available_services
from services.acservice.constants import *
from django.conf import settings
from accounts.models import Account


request_distributor = get_request_distributor()
response_aggregator = get_response_aggregator()


def get_request_context(request):
    user_account_id = None
    dev_account_id = None
    if hasattr(request, 'successful_authenticator') and request.successful_authenticator:
        # In production API calls this should be always the case as otherwise API returns 401 before reaching here
        user_account_id = request.user.id
        dev_account_id = request.auth.application.user.id
    else:
        # In integration tests or local development it can happen that successful_authenticator does not exist
        # In that case we provide 'fake' context so tests can be carried out correctly
        if request.user.is_authenticated():
            user_account_id = request.user.id
            dev_account_id = user_account_id
        else:
            if settings.DEBUG and settings.ALLOW_UNAUTHENTICATED_API_REQUESTS_ON_DEBUG:
                user_account_id = Account.objects.all()[0].id
                dev_account_id = user_account_id
    return {
        'user_account_id': user_account_id,
        'dev_account_id': dev_account_id,
        # NOTE: we use id's instead of the objects so that the dict can be serialized and passed to Celery
    }


def parse_request_distributor_query_params(request):
    include = request.GET.get(QUERY_PARAM_INCLUDE, None)
    if include is not None:
        include = include.split(',')
    exclude = request.GET.get(QUERY_PARAM_EXCLUDE, None)
    if exclude is not None:
        exclude = exclude.split(',')
    wait_until_complete = \
        request.GET.get(QUERY_PARAM_WAIT_UNTIL_COMPLETE, False)  # This option is left intentionally undocumented
    return {
        QUERY_PARAM_INCLUDE: include,
        QUERY_PARAM_EXCLUDE: exclude,
        'wait_until_complete': wait_until_complete,
    }


def parse_common_search_query_params(request):
    fields = request.GET.get(QUERY_PARAM_FIELDS, None)
    if fields is None:
        fields = MINIMUM_RESOURCE_DESCRIPTION_FIELDS
    else:
        if fields == '*':
            fields = ALL_RESOURCE_DESCRIPTION_FIELDS
        else:
            fields = fields.split(',')
    size = request.GET.get(QUERY_PARAM_SIZE, 15)
    try:
        size = int(size)
    except ValueError:
        raise ACAPIBadRequest("Invalid '{0}' value".format(QUERY_PARAM_SIZE))
    page = request.GET.get(QUERY_PARAM_PAGE, None)
    if page is not None:
        try:
            page = int(page)
        except ValueError:
            raise ACAPIBadRequest("Invalid '{0}' value".format(QUERY_PARAM_PAGE))
        if page < 1:
            raise ACAPIBadRequest("Invalid '{0}' value (must be greater than 0)".format(QUERY_PARAM_PAGE))
    return {
        QUERY_PARAM_SIZE: size,
        QUERY_PARAM_PAGE: page,
        QUERY_PARAM_FIELDS: fields,
    }


def get_service_name_from_acid(acid):
    return acid.split(ACID_SEPARATOR_CHAR)[0]  # Derive service name from ACID


@api_view(['GET'])
def invalid_url(request):
    raise ACAPIInvalidUrl<commit_msg>Fix permission with invalid_url view<commit_after>from rest_framework.response import Response
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from ac_mediator.exceptions import *
from api.request_distributor import get_request_distributor
from api.response_aggregator import get_response_aggregator
from services.management import get_available_services
from services.acservice.constants import *
from django.conf import settings
from accounts.models import Account


request_distributor = get_request_distributor()
response_aggregator = get_response_aggregator()


def get_request_context(request):
    user_account_id = None
    dev_account_id = None
    if hasattr(request, 'successful_authenticator') and request.successful_authenticator:
        # In production API calls this should be always the case as otherwise API returns 401 before reaching here
        user_account_id = request.user.id
        dev_account_id = request.auth.application.user.id
    else:
        # In integration tests or local development it can happen that successful_authenticator does not exist
        # In that case we provide 'fake' context so tests can be carried out correctly
        if request.user.is_authenticated():
            user_account_id = request.user.id
            dev_account_id = user_account_id
        else:
            if settings.DEBUG and settings.ALLOW_UNAUTHENTICATED_API_REQUESTS_ON_DEBUG:
                user_account_id = Account.objects.all()[0].id
                dev_account_id = user_account_id
    return {
        'user_account_id': user_account_id,
        'dev_account_id': dev_account_id,
        # NOTE: we use id's instead of the objects so that the dict can be serialized and passed to Celery
    }


def parse_request_distributor_query_params(request):
    include = request.GET.get(QUERY_PARAM_INCLUDE, None)
    if include is not None:
        include = include.split(',')
    exclude = request.GET.get(QUERY_PARAM_EXCLUDE, None)
    if exclude is not None:
        exclude = exclude.split(',')
    wait_until_complete = \
        request.GET.get(QUERY_PARAM_WAIT_UNTIL_COMPLETE, False)  # This option is left intentionally undocumented
    return {
        QUERY_PARAM_INCLUDE: include,
        QUERY_PARAM_EXCLUDE: exclude,
        'wait_until_complete': wait_until_complete,
    }


def parse_common_search_query_params(request):
    fields = request.GET.get(QUERY_PARAM_FIELDS, None)
    if fields is None:
        fields = MINIMUM_RESOURCE_DESCRIPTION_FIELDS
    else:
        if fields == '*':
            fields = ALL_RESOURCE_DESCRIPTION_FIELDS
        else:
            fields = fields.split(',')
    size = request.GET.get(QUERY_PARAM_SIZE, 15)
    try:
        size = int(size)
    except ValueError:
        raise ACAPIBadRequest("Invalid '{0}' value".format(QUERY_PARAM_SIZE))
    page = request.GET.get(QUERY_PARAM_PAGE, None)
    if page is not None:
        try:
            page = int(page)
        except ValueError:
            raise ACAPIBadRequest("Invalid '{0}' value".format(QUERY_PARAM_PAGE))
        if page < 1:
            raise ACAPIBadRequest("Invalid '{0}' value (must be greater than 0)".format(QUERY_PARAM_PAGE))
    return {
        QUERY_PARAM_SIZE: size,
        QUERY_PARAM_PAGE: page,
        QUERY_PARAM_FIELDS: fields,
    }


def get_service_name_from_acid(acid):
    return acid.split(ACID_SEPARATOR_CHAR)[0]  # Derive service name from ACID


@api_view(['GET', 'POST'])
@permission_classes((AllowAny, ))
def invalid_url(request):
    raise ACAPIInvalidUrl
{'fraction_non_alphanumeric': 0.07099333994615276, 'fraction_numerical': 0.003400878560294743, 'mean_word_length': 3.362175525339926, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6138375', 'n_tokens_mistral': 2101, 'n_tokens_neox': 2024, 'n_words': 526}