<commit_before>import json
import re

from django.contrib.gis.geos import Point

from rest_framework import serializers
from rest_framework import viewsets
from rest_framework.settings import api_settings

from rest_framework_jsonp.renderers import JSONPRenderer

from django_filters import rest_framework as filters

from froide.helper.api_utils import OpenRefineReconciliationMixin

from .models import GeoRegion


GERMAN_PLZ_RE = re.compile(r'\d{5}')


class GeoRegionSerializer(serializers.HyperlinkedModelSerializer):
    resource_uri = serializers.HyperlinkedIdentityField(
        view_name='api:georegion-detail',
        lookup_field='pk'
    )
    part_of = serializers.HyperlinkedRelatedField(
        view_name='api:georegion-detail',
        lookup_field='pk',
        read_only=True,
        many=False
    )
    centroid = serializers.SerializerMethodField()

    class Meta:
        model = GeoRegion
        depth = 0
        fields = (
            'resource_uri', 'id', 'name', 'slug', 'kind',
            'kind_detail', 'level',
            'region_identifier', 'global_identifier',
            'area', 'population', 'valid_on',
            'part_of', 'centroid',
        )

    def get_centroid(self, obj):
        if obj.geom is not None:
            return json.loads(obj.geom.centroid.json)
        return None


class GeoRegionDetailSerializer(GeoRegionSerializer):
    geom = serializers.SerializerMethodField()
    gov_seat = serializers.SerializerMethodField()

    class Meta(GeoRegionSerializer.Meta):
        fields = GeoRegionSerializer.Meta.fields + (
            'geom', 'gov_seat',
            'centroid',
        )

    def get_geom(self, obj):
        if obj.geom is not None:
            return json.loads(obj.geom.json)
        return None

    def get_gov_seat(self, obj):
        if obj.gov_seat is not None:
            return json.loads(obj.gov_seat.json)
        return None


class GeoRegionFilter(filters.FilterSet):
    id = filters.CharFilter(method='id_filter')
    q = filters.CharFilter(method='search_filter')
    kind = filters.CharFilter(method='kind_filter')
    level = filters.NumberFilter(method='level_filter')
    ancestor = filters.ModelChoiceFilter(
        method='ancestor_filter',
        queryset=GeoRegion.objects.all()
    )
    latlng = filters.CharFilter(method='latlng_filter')

    class Meta:
        model = GeoRegion
        fields = (
            'name', 'level', 'kind', 'slug'
        )

    def search_filter(self, queryset, name, value):
        return queryset.filter(name__icontains=value)

    def kind_filter(self, queryset, name, value):
        return queryset.filter(kind=value)

    def level_filter(self, queryset, name, value):
        return queryset.filter(level=value)

    def id_filter(self, queryset, name, value):
        ids = value.split(',')
        return queryset.filter(pk__in=ids)<commit_msg>Allow to filter regions by name with lowercase<commit_after>import json
import re

from django.contrib.gis.geos import Point

from rest_framework import serializers
from rest_framework import viewsets
from rest_framework.settings import api_settings

from rest_framework_jsonp.renderers import JSONPRenderer

from django_filters import rest_framework as filters

from froide.helper.api_utils import OpenRefineReconciliationMixin

from .models import GeoRegion


GERMAN_PLZ_RE = re.compile(r'\d{5}')


class GeoRegionSerializer(serializers.HyperlinkedModelSerializer):
    resource_uri = serializers.HyperlinkedIdentityField(
        view_name='api:georegion-detail',
        lookup_field='pk'
    )
    part_of = serializers.HyperlinkedRelatedField(
        view_name='api:georegion-detail',
        lookup_field='pk',
        read_only=True,
        many=False
    )
    centroid = serializers.SerializerMethodField()

    class Meta:
        model = GeoRegion
        depth = 0
        fields = (
            'resource_uri', 'id', 'name', 'slug', 'kind',
            'kind_detail', 'level',
            'region_identifier', 'global_identifier',
            'area', 'population', 'valid_on',
            'part_of', 'centroid',
        )

    def get_centroid(self, obj):
        if obj.geom is not None:
            return json.loads(obj.geom.centroid.json)
        return None


class GeoRegionDetailSerializer(GeoRegionSerializer):
    geom = serializers.SerializerMethodField()
    gov_seat = serializers.SerializerMethodField()

    class Meta(GeoRegionSerializer.Meta):
        fields = GeoRegionSerializer.Meta.fields + (
            'geom', 'gov_seat',
            'centroid',
        )

    def get_geom(self, obj):
        if obj.geom is not None:
            return json.loads(obj.geom.json)
        return None

    def get_gov_seat(self, obj):
        if obj.gov_seat is not None:
            return json.loads(obj.gov_seat.json)
        return None


class GeoRegionFilter(filters.FilterSet):
    id = filters.CharFilter(method='id_filter')
    q = filters.CharFilter(method='search_filter')
    kind = filters.CharFilter(method='kind_filter')
    level = filters.NumberFilter(method='level_filter')
    ancestor = filters.ModelChoiceFilter(
        method='ancestor_filter',
        queryset=GeoRegion.objects.all()
    )
    latlng = filters.CharFilter(method='latlng_filter')
    name = filters.CharFilter(method='name_filter')

    class Meta:
        model = GeoRegion
        fields = (
            'name', 'level', 'kind', 'slug'
        )

    def name_filter(self, queryset, name, value):
        qs = queryset.filter(name__icontains=value)
        if not qs:
            return queryset.filter(name__icontains=value.capitalize())
        return qs

    def search_filter(self, queryset, name, value):
        return queryset.filter(name__icontains=value)

    def kind_filter(self, queryset, name, value):
        return queryset.filter(kind=value)

    def level_filter(self, queryset, name, value):
        return queryset.filter(level=value)

    def id_filter(self, queryset, name, value):
        ids = value.split(',')
        return queryset.filter(pk__in=ids)
{'fraction_non_alphanumeric': 0.09792967466316135, 'fraction_numerical': 0.0006572461386789353, 'mean_word_length': 2.9603122966818476, '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': '6248375', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1784, 'n_words': 380}