Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

Fix admin date filter #106

Merged
merged 1 commit into from
Oct 16, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions versions/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.utils.text import capfirst
from django.template.response import TemplateResponse
from django import VERSION

from datetime import datetime

class DateTimeFilterForm(forms.Form):
def __init__(self, request, *args, **kwargs):
Expand Down Expand Up @@ -52,6 +52,7 @@ class DateTimeFilter(admin.FieldListFilter):
title = 'DateTime filter'

def __init__(self, field, request, params, model, model_admin, field_path):
self.field_path = field_path
self.lookup_kwarg_as_ofdate = '%s_as_of_0' % field_path
self.lookup_kwarg_as_oftime = '%s_as_of_1' % field_path
super(DateTimeFilter, self).__init__(field, request, params, model, model_admin, field_path)
Expand All @@ -67,8 +68,9 @@ def get_form(self, request):
return DateTimeFilterForm(request, data=self.used_parameters, field_name=self.field_path)

def queryset(self, request, queryset):
if self.form.is_valid() and self.form.cleaned_data.values()[0] is not None:
filter_params = self.form.cleaned_data.values()[0]
fieldname = '%s_as_of' % self.field_path
if self.form.is_valid() and fieldname in self.form.cleaned_data:
filter_params = self.form.cleaned_data.get(fieldname, datetime.utcnow())
return queryset.as_of(filter_params)
else:
return queryset
Expand Down