<commit_before>from __future__ import absolute_import

from django import forms
from django.core import validators
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

from .widgets import LatLngWidget, MapWidget


class LatLngField(forms.MultiValueField):
    """Subclass of ``MultiValueField`` that creates two
    ``FloatField``s for latitude and longitude respectively.
    """
    widget = LatLngWidget
    default_error_messages = {
        'invalid_latitude':  _(u'Enter a valid latitude.'),
        'invalid_longitude': _(u'Enter a valid longitude.')
    }

    def __init__(self, *args, **kwargs):
        errors = self.default_error_messages.copy()
        if 'error_messages' in kwargs:
            errors.update(kwargs['error_messages'])
        localize = kwargs.get('localize', False)
        fields = (
            forms.FloatField(
                label=_(u'Latitude'),
                error_messages={'invalid': errors['invalid_latitude']},
                localize=localize
            ),
            forms.FloatField(
                label=_(u'Longitude'),
                error_messages={'invalid': errors['invalid_longitude']},
                localize=localize
            ),
        )
        super(LatLngField, self).__init__(fields, *args, **kwargs)

    def compress(self, data_list):
        if not data_list:
            return {}
        if data_list[0] in validators.EMPTY_VALUES:
            raise ValidationError(self.error_messages['invalid_latitude'])
        if data_list[1] in validators.EMPTY_VALUES:
            raise ValidationError(self.error_messages['invalid_longitude'])
        return {
            'latitude':  data_list[0],
            'longitude': data_list[1],
        }


class MapField(LatLngField):
    """Subclass of ``LatLngField`` that uses a ``MapWidget`` to render
    and adds an extra field for clearing the location.
    """
    widget = MapWidget

    def __init__(self, *args, **kwargs):
        """Adds an extra `clear_location` field."""
        super(MapField, self).__init__(*args, **kwargs)
        localize = kwargs.get('localize', False)
        self.fields += (
            forms.BooleanField(initial=False, required=False,
                               localize=localize),
        )

    def compress(self, data_list):
        if data_list and data_list[2]:
            return {}
        return super(MapField, self).compress(data_list)<commit_msg>adding a way to set widget_attrs from the `MapField`. similar to tinymce's field.<commit_after>from __future__ import absolute_import

from django import forms
from django.core import validators
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

from .widgets import LatLngWidget, MapWidget


class LatLngField(forms.MultiValueField):
    """Subclass of ``MultiValueField`` that creates two
    ``FloatField``s for latitude and longitude respectively.
    """
    widget = LatLngWidget
    default_error_messages = {
        'invalid_latitude':  _(u'Enter a valid latitude.'),
        'invalid_longitude': _(u'Enter a valid longitude.')
    }

    def __init__(self, *args, **kwargs):
        errors = self.default_error_messages.copy()
        if 'error_messages' in kwargs:
            errors.update(kwargs['error_messages'])
        localize = kwargs.get('localize', False)
        fields = (
            forms.FloatField(
                label=_(u'Latitude'),
                error_messages={'invalid': errors['invalid_latitude']},
                localize=localize
            ),
            forms.FloatField(
                label=_(u'Longitude'),
                error_messages={'invalid': errors['invalid_longitude']},
                localize=localize
            ),
        )
        super(LatLngField, self).__init__(fields, *args, **kwargs)

    def compress(self, data_list):
        if not data_list:
            return {}
        if data_list[0] in validators.EMPTY_VALUES:
            raise ValidationError(self.error_messages['invalid_latitude'])
        if data_list[1] in validators.EMPTY_VALUES:
            raise ValidationError(self.error_messages['invalid_longitude'])
        return {
            'latitude':  data_list[0],
            'longitude': data_list[1],
        }


class MapField(LatLngField):
    """Subclass of ``LatLngField`` that uses a ``MapWidget`` to render
    and adds an extra field for clearing the location.
    """
    widget = MapWidget

    def __init__(self, *args, **kwargs):
        """Adds an extra `clear_location` field."""
        widget_attrs = kwargs.pop('widget_attrs', None)
        super(MapField, self).__init__(*args, **kwargs)

        if widget_attrs is not None:
            self.widget = MapWidget(**widget_attrs)

        localize = kwargs.get('localize', False)
        self.fields += (
            forms.BooleanField(initial=False, required=False,
                               localize=localize),
        )

    def compress(self, data_list):
        if data_list and data_list[2]:
            return {}
        return super(MapField, self).compress(data_list)
{'fraction_non_alphanumeric': 0.10555878365291498, 'fraction_numerical': 0.001936858415649816, 'mean_word_length': 2.5057705363204343, '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': '2054309', 'n_tokens_mistral': 1436, 'n_tokens_neox': 1405, 'n_words': 337}