<commit_before>import json
from django.core.validators import EMPTY_VALUES
from rest_framework import serializers
from django.core import validators
from django.utils.translation import ugettext_lazy as _
import datetime

class ThreePartDateField(serializers.WritableField):
    """
    A serializer field for handling three part date time JSON formatted
    datetime.date.

    Expected input format:
        {
            "day": 25,
            "month": 12,
            "year": 2012
        }
    """

    type_name = 'ThreePartDateField'
    type_label = 'date'

    default_error_messages = {
        'invalid':
            _('Date field has wrong format. Use   { "day": 25, "month": 12, "year": 2012 }'),
    }

    def from_native(self, value):
        """c
        Parse json data and return a date object
        """
        if value in EMPTY_VALUES:
            return None

        value_type = type(value)
        if value_type is str or value_type is unicode:
            try:
                value = value.replace("'", '"')
                value = json.loads(value)
            except ValueError:
                msg = self.error_messages['invalid']
                raise serializers.ValidationError(msg)

        if value:
            day = value.get("day")
            month = value.get("month")
            year = value.get("year")

            if all([day, month, year]):
                try:
                    dt_object = datetime.date(int(year), int(month), int(day))
                except ValueError as ve:
                    raise serializers.ValidationError(ve)
                return dt_object
            elif not all([day, month, year]):
                return None
            msg = self.error_messages['invalid']
            raise serializers.ValidationError(msg)

    def to_native(self, value):
        """
        Transform datetime.date object to json.
        """
        if value is None:
            return value


        if isinstance(value, datetime.date):
            value = {
                "year": str(value.year),
                "month": str(value.month),
                "day": str(value.day)
            }
        return value<commit_msg>Add validation for dob >= 1900<commit_after>import json
from django.core.validators import EMPTY_VALUES
from rest_framework import serializers
from django.core import validators
from django.utils.translation import ugettext_lazy as _
import datetime

class ThreePartDateField(serializers.WritableField):
    """
    A serializer field for handling three part date time JSON formatted
    datetime.date.

    Expected input format:
        {
            "day": 25,
            "month": 12,
            "year": 2012
        }
    """

    type_name = 'ThreePartDateField'
    type_label = 'date'

    default_error_messages = {
        'invalid':
            _('Date field has wrong format. Use   { "day": 25, "month": 12, "year": 2012 }'),
    }

    def from_native(self, value):
        """c
        Parse json data and return a date object
        """
        if value in EMPTY_VALUES:
            return None

        value_type = type(value)
        if value_type is str or value_type is unicode:
            try:
                value = value.replace("'", '"')
                value = json.loads(value)
            except ValueError:
                msg = self.error_messages['invalid']
                raise serializers.ValidationError(msg)

        if value:
            day = value.get("day")
            month = value.get("month")
            year = value.get("year")

            if all([day, month, year]):
                try:
                    dt_object = datetime.date(int(year), int(month), int(day))
                except ValueError as ve:
                    raise serializers.ValidationError(ve)

                if int(year) < 1900:
                    raise serializers.ValidationError('year must be >= 1900')
                return dt_object
            elif not all([day, month, year]):
                return None
            msg = self.error_messages['invalid']
            raise serializers.ValidationError(msg)

    def to_native(self, value):
        """
        Transform datetime.date object to json.
        """
        if value is None:
            return value


        if isinstance(value, datetime.date):
            value = {
                "year": str(value.year),
                "month": str(value.month),
                "day": str(value.day)
            }
        return value
{'fraction_non_alphanumeric': 0.09425593257928587, 'fraction_numerical': 0.009758261255267244, 'mean_word_length': 1.659198113207547, 'pattern_counts': {'":': 18, '<': 4, '<?xml version=': 0, '>': 5, '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': '2996841', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1141, 'n_words': 337}