<commit_before>from django.conf import settings

from rest_framework import serializers
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.settings import api_settings

from rest_framework_jsonp.renderers import JSONPRenderer

from elasticsearch_dsl.query import Q

from django_filters import rest_framework as filters

from froide.helper.api_utils import (
    SearchFacetListSerializer, OpenRefineReconciliationMixin,
)
from froide.helper.search import SearchQuerySetWrapper
from froide.helper.search.api_views import ESQueryMixin
from froide.georegion.models import GeoRegion

from .models import (PublicBody, Category, Jurisdiction, FoiLaw,
                     Classification)
from .documents import PublicBodyDocument


class JurisdictionSerializer(serializers.HyperlinkedModelSerializer):
    resource_uri = serializers.HyperlinkedIdentityField(
        view_name='api:jurisdiction-detail',
        lookup_field='pk'
    )
    region = serializers.HyperlinkedRelatedField(
        view_name='api:georegion-detail',
        lookup_field='pk',
        read_only=True
    )
    site_url = serializers.CharField(source='get_absolute_domain_url')

    class Meta:
        model = Jurisdiction
        depth = 0
        fields = (
            'resource_uri', 'id', 'name', 'rank', 'description', 'slug',
            'site_url', 'region'
        )


class JurisdictionViewSet(viewsets.ReadOnlyModelViewSet):
    serializer_class = JurisdictionSerializer
    queryset = Jurisdiction.objects.all()


class SimpleFoiLawSerializer(serializers.HyperlinkedModelSerializer):
    resource_uri = serializers.HyperlinkedIdentityField(
        view_name='api:law-detail',
        lookup_field='pk'
    )
    jurisdiction = serializers.HyperlinkedRelatedField(
        view_name='api:jurisdiction-detail',
        lookup_field='pk',
        read_only=True
    )
    mediator = serializers.HyperlinkedRelatedField(
        view_name='api:publicbody-detail',
        lookup_field='pk',
        read_only=True
    )
    site_url = serializers.CharField(source='get_absolute_domain_url')

    class Meta:
        model = FoiLaw
        depth = 0
        fields = (
            'resource_uri', 'id', 'name', 'slug', 'description',
            'long_description', 'law_type',
            'created', 'updated', 'request_note', 'request_note_html', 'meta',
            'site_url', 'jurisdiction', 'email_only', 'mediator',
            'priority', 'url', 'max_response_time', 'email_only',
            'requires_signature', 'max_response_time_unit',
            'letter_start', 'letter_end'
        )

    def to_representation(self, instance):
        """Activate language based on request query param."""
        request = self.context.get('request')
        if request:
            lang = request.GET.get('language', settings.LANGUAGE_CODE)
            instance.set_current_language(lang)
        ret = super().to_representation(instance)
        return ret


class FoiLawSerializer(SimpleFoiLawSerializer):
    combined = serializers.HyperlinkedRelatedField(
        view_name='api:law-detail',
        lookup_field='pk',
        read_only=True,
        many=True
    )

    class Meta(SimpleFoiLawSerializer.Meta):
        fields = SimpleFoiLawSerializer.Meta.fields + (<commit_msg>Return API site_url in selected language<commit_after>from django.conf import settings
from django.utils import translation

from rest_framework import serializers
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.settings import api_settings

from rest_framework_jsonp.renderers import JSONPRenderer

from elasticsearch_dsl.query import Q

from django_filters import rest_framework as filters

from froide.helper.api_utils import (
    SearchFacetListSerializer, OpenRefineReconciliationMixin,
)
from froide.helper.search import SearchQuerySetWrapper
from froide.helper.search.api_views import ESQueryMixin
from froide.georegion.models import GeoRegion

from .models import (PublicBody, Category, Jurisdiction, FoiLaw,
                     Classification)
from .documents import PublicBodyDocument


def get_language_from_query(request):
    if request:
        return request.GET.get('language', settings.LANGUAGE_CODE)
    return settings.LANGUAGE_CODE


class JurisdictionSerializer(serializers.HyperlinkedModelSerializer):
    resource_uri = serializers.HyperlinkedIdentityField(
        view_name='api:jurisdiction-detail',
        lookup_field='pk'
    )
    region = serializers.HyperlinkedRelatedField(
        view_name='api:georegion-detail',
        lookup_field='pk',
        read_only=True
    )
    site_url = serializers.CharField(source='get_absolute_domain_url')

    class Meta:
        model = Jurisdiction
        depth = 0
        fields = (
            'resource_uri', 'id', 'name', 'rank', 'description', 'slug',
            'site_url', 'region'
        )


class JurisdictionViewSet(viewsets.ReadOnlyModelViewSet):
    serializer_class = JurisdictionSerializer
    queryset = Jurisdiction.objects.all()


class SimpleFoiLawSerializer(serializers.HyperlinkedModelSerializer):
    resource_uri = serializers.HyperlinkedIdentityField(
        view_name='api:law-detail',
        lookup_field='pk'
    )
    jurisdiction = serializers.HyperlinkedRelatedField(
        view_name='api:jurisdiction-detail',
        lookup_field='pk',
        read_only=True
    )
    mediator = serializers.HyperlinkedRelatedField(
        view_name='api:publicbody-detail',
        lookup_field='pk',
        read_only=True
    )
    site_url = serializers.SerializerMethodField()

    class Meta:
        model = FoiLaw
        depth = 0
        fields = (
            'resource_uri', 'id', 'name', 'slug', 'description',
            'long_description', 'law_type',
            'created', 'updated', 'request_note', 'request_note_html', 'meta',
            'site_url', 'jurisdiction', 'email_only', 'mediator',
            'priority', 'url', 'max_response_time', 'email_only',
            'requires_signature', 'max_response_time_unit',
            'letter_start', 'letter_end'
        )

    def get_site_url(self, obj):
        language = get_language_from_query(self.context.get('request'))
        with translation.override(language):
            return obj.get_absolute_domain_url()

    def to_representation(self, instance):
        """Activate language based on request query param."""
        request = self.context.get('request')
        if request:
            lang = request.GET.get('language', settings.LANGUAGE_CODE)
            instance.set_current_language(lang)
        ret = super().to_representation(instance)
        return ret


class FoiLawSerializer(SimpleFoiLawSerializer):
    combined = serializers.HyperlinkedRelatedField(
        view_name='api:law-detail',
        lookup_field='pk',
        read_only=True,
        many=True
    )

    class Meta(SimpleFoiLawSerializer.Meta):
        fields = SimpleFoiLawSerializer.Meta.fields + (
{'fraction_non_alphanumeric': 0.08999017268005054, 'fraction_numerical': 0.0005615611399691141, 'mean_word_length': 3.3894023413431915, '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': '4867395', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1938, 'n_words': 395}