from functools import reduce

from django_filters import DateFromToRangeFilter as _DateFromToRangeFilter
from rest_framework.fields import DecimalField


class PriceField(DecimalField):
    def __init__(self, *args, max_digits=10, decimal_places=2, min_value=0, **kwargs):
        super().__init__(*args, max_digits=max_digits, decimal_places=decimal_places, min_value=min_value, **kwargs)


def deep_get(dictionary, *keys, default=None):
    """Get value from a nested dictionary (JSON)"""
    return reduce(lambda d, key: d.get(key, None) if isinstance(d, dict) else default, keys, dictionary)


class DateFromToRangeFilter(_DateFromToRangeFilter):
    """ DateFromToRangeFilter with replaced after/before suffixes to from/to """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.field.widget.suffixes = ['from', 'to']
