Skip to content

Instantly share code, notes, and snippets.

@Nitron
Forked from copitux/filters.py
Last active August 29, 2015 14:10
Show Gist options
  • Save Nitron/b9e2fc4eff0b13ffa573 to your computer and use it in GitHub Desktop.
Save Nitron/b9e2fc4eff0b13ffa573 to your computer and use it in GitHub Desktop.
"""
The MIT License (MIT)
Copyright (c) <2013> <David Medina>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from django import forms
from django.utils.encoding import force_str
from django.utils.dateparse import parse_datetime
import django_filters
from rest_framework import ISO_8601
class IsoDateTimeField(forms.DateTimeField):
"""
It support 'iso-8601' date format too which is out the scope of
the ``datetime.strptime`` standard library
# ISO 8601: ``http://www.w3.org/TR/NOTE-datetime``
"""
def strptime(self, value, format):
value = force_str(value)
if format == ISO_8601:
parsed = parse_datetime(value)
if parsed is None: # Continue with other formats if doesn't match
raise ValueError
return parsed
return super(IsoDateTimeField, self).strptime(value, format)
class PreciseDateTimeField(IsoDateTimeField):
""" Only support ISO 8601 """
def __init__(self, *args, **kwargs):
kwargs['input_formats'] = (ISO_8601, )
super(PreciseDateTimeField, self).__init__(*args, **kwargs)
class IsoDateTimeFilter(django_filters.DateTimeFilter):
""" Extend ``DateTimeFilter`` to filter by ISO 8601 formated dates too"""
field_class = IsoDateTimeField
class PreciseDateTimeFilter(django_filters.DateTimeFilter):
""" Extend ``DateTimeFilter`` to filter only by ISO 8601 formated dates """
field_class = PreciseDateTimeField
import django_filters
from rest_framework import ISO_8601
from thisgist.filters import IsoDateTimeField
class User(django_filters.FilterSet):
min_date = IsoDateTimeField(source='date_joined', lookup_type='gte',
input_formats=(ISO_8601, '%m/%d/%Y %H:%M:%S'))
# With the apropiated RestView we could do hard or light filtering
# GET /resource/?min_date=2006-10-25T14:30:59.000000Z HTTP/1.1
# GET /resource/?min_date=10/25/2006%2014:30 HTTP/1.1
@ShaneDrury
Copy link

Should https://gist.github.com/Nitron/b9e2fc4eff0b13ffa573#file-sample-py-L9 be:

min_date = IsoDateTimeFilter(source='date_joined', lookup_type='gte',
                                input_formats=(ISO_8601, '%m/%d/%Y %H:%M:%S'))

i.e. IsoDateTimeField -> IsoDateTimeFilter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment