<commit_before>"""Serializer fields"""
from __future__ import absolute_import
import collections

from django.contrib.gis import geos, forms
from django.contrib.gis.db.models.query import GeoQuerySet
from rest_framework import renderers
from rest_framework.fields import Field, FileField

from spillway.compat import json
from spillway.forms import fields


class GeometryField(Field):
    def bind(self, field_name, parent):
        try:
            renderer = parent.context['request'].accepted_renderer
        except (AttributeError, KeyError):
            pass
        else:
            obj = parent.root.instance
            if not isinstance(obj, GeoQuerySet):
                try:
                    obj = obj[0]
                except (IndexError, TypeError):
                    pass
            if hasattr(obj, renderer.format):
                self.source = renderer.format
        super(GeometryField, self).bind(field_name, parent)

    def get_attribute(self, instance):
        # SpatiaLite returns empty/invalid geometries in WKT or GeoJSON with
        # exceedingly high simplification tolerances.
        try:
            return super(GeometryField, self).get_attribute(instance)
        except geos.GEOSException:
            return None

    def to_internal_value(self, data):
        # forms.GeometryField cannot handle geojson dicts.
        if isinstance(data, collections.Mapping):
            data = json.dumps(data)
        field = fields.GeometryField(widget=forms.BaseGeometryWidget())
        return field.to_python(data)

    def to_representation(self, value):
        # Create a dict from the GEOSGeometry when the value is not previously
        # serialized from the spatial db.
        try:
            return {'type': value.geom_type, 'coordinates': value.coords}
        # Value is already serialized as geojson, kml, etc.
        except AttributeError:<commit_msg>Check QuerySet annotations for renderer format<commit_after>"""Serializer fields"""
from __future__ import absolute_import
import collections

from django.contrib.gis import geos, forms
from django.db.models.query import QuerySet
from rest_framework import renderers
from rest_framework.fields import Field, FileField

from spillway.compat import json
from spillway.forms import fields


class GeometryField(Field):
    def bind(self, field_name, parent):
        try:
            renderer = parent.context['request'].accepted_renderer
        except (AttributeError, KeyError):
            pass
        else:
            obj = parent.root.instance
            try:
                has_format = renderer.format in obj.query.annotations
            except AttributeError:
                if not isinstance(obj, QuerySet):
                    try:
                        obj = obj[0]
                    except (IndexError, TypeError):
                        pass
                has_format = hasattr(obj, renderer.format)
            if has_format:
                self.source = renderer.format
        super(GeometryField, self).bind(field_name, parent)

    def get_attribute(self, instance):
        # SpatiaLite returns empty/invalid geometries in WKT or GeoJSON with
        # exceedingly high simplification tolerances.
        try:
            return super(GeometryField, self).get_attribute(instance)
        except geos.GEOSException:
            return None

    def to_internal_value(self, data):
        # forms.GeometryField cannot handle geojson dicts.
        if isinstance(data, collections.Mapping):
            data = json.dumps(data)
        field = fields.GeometryField(widget=forms.BaseGeometryWidget())
        return field.to_python(data)

    def to_representation(self, value):
        # Create a dict from the GEOSGeometry when the value is not previously
        # serialized from the spatial db.
        try:
            return {'type': value.geom_type, 'coordinates': value.coords}
        # Value is already serialized as geojson, kml, etc.
        except AttributeError:
{'fraction_non_alphanumeric': 0.07112553032193661, 'fraction_numerical': 0.0004991265285749938, 'mean_word_length': 2.385135135135135, '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': '2596819', 'n_tokens_mistral': 1029, 'n_tokens_neox': 986, 'n_words': 307}