Last active
December 18, 2015 11:09
-
-
Save copitux/5773821 to your computer and use it in GitHub Desktop.
Iso 8601 DateTimeFilter to Django Rest Framework and django-filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 | |
import django_filters | |
from rest_framework.compat import parse_datetime | |
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
It's missing an ' in line 10 of sample.py.
There were a few typos and a broken import that prevented this from working (at least with DRF3.0). I fixed them, diff: https://gist.github.com/Nitron/b9e2fc4eff0b13ffa573/revisions
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same question. GPL? LGPL? MIT?