diff --git a/.travis.yml b/.travis.yml index 608f8bb..0b40257 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,6 @@ cache: pip matrix: fast_finish: true include: - - { python: "2.7", env: DJANGO_VERSION="1.8.*" } - { python: "2.7", env: DJANGO_VERSION="1.9.*" } - { python: "2.7", env: DJANGO_VERSION="1.10.*" } - { python: "2.7", env: DJANGO_VERSION="1.11.*" } @@ -32,4 +31,4 @@ branches: - master script: - - echo "TODO" + - python runtests.py diff --git a/__init__.py b/daterangefilter/__init__.py similarity index 100% rename from __init__.py rename to daterangefilter/__init__.py diff --git a/apps.py b/daterangefilter/apps.py similarity index 100% rename from apps.py rename to daterangefilter/apps.py diff --git a/filters.py b/daterangefilter/filters.py similarity index 100% rename from filters.py rename to daterangefilter/filters.py diff --git a/locale/ru/LC_MESSAGES/django.mo b/daterangefilter/locale/ru/LC_MESSAGES/django.mo similarity index 100% rename from locale/ru/LC_MESSAGES/django.mo rename to daterangefilter/locale/ru/LC_MESSAGES/django.mo diff --git a/locale/ru/LC_MESSAGES/django.po b/daterangefilter/locale/ru/LC_MESSAGES/django.po similarity index 100% rename from locale/ru/LC_MESSAGES/django.po rename to daterangefilter/locale/ru/LC_MESSAGES/django.po diff --git a/daterangefilter/models.py b/daterangefilter/models.py new file mode 100644 index 0000000..4ae8a41 --- /dev/null +++ b/daterangefilter/models.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- + +from __future__ import unicode_literals + +# required for tests.py diff --git a/templates/daterangefilter/_future_ranges.html b/daterangefilter/templates/daterangefilter/_future_ranges.html similarity index 100% rename from templates/daterangefilter/_future_ranges.html rename to daterangefilter/templates/daterangefilter/_future_ranges.html diff --git a/templates/daterangefilter/_past_ranges.html b/daterangefilter/templates/daterangefilter/_past_ranges.html similarity index 100% rename from templates/daterangefilter/_past_ranges.html rename to daterangefilter/templates/daterangefilter/_past_ranges.html diff --git a/templates/daterangefilter/daterangefilter.html b/daterangefilter/templates/daterangefilter/daterangefilter.html similarity index 100% rename from templates/daterangefilter/daterangefilter.html rename to daterangefilter/templates/daterangefilter/daterangefilter.html diff --git a/templates/daterangefilter/ranges.html b/daterangefilter/templates/daterangefilter/ranges.html similarity index 100% rename from templates/daterangefilter/ranges.html rename to daterangefilter/templates/daterangefilter/ranges.html diff --git a/templates/daterangefilter/suit_daterangefilter.html b/daterangefilter/templates/daterangefilter/suit_daterangefilter.html similarity index 100% rename from templates/daterangefilter/suit_daterangefilter.html rename to daterangefilter/templates/daterangefilter/suit_daterangefilter.html diff --git a/daterangefilter/tests.py b/daterangefilter/tests.py new file mode 100644 index 0000000..6638559 --- /dev/null +++ b/daterangefilter/tests.py @@ -0,0 +1,105 @@ +# -*- coding: utf-8 -*- + +from __future__ import unicode_literals + +import datetime + +from django.utils import timezone +from django.test import RequestFactory, TestCase +from django.test.utils import override_settings +from django.db import models +from django.contrib.admin import ModelAdmin, site +from django.contrib.admin.views.main import ChangeList +from django.contrib.auth.models import User +from django.utils.encoding import force_text + +from .filters import DateRangeFilter, PastDateRangeFilter, FutureDateRangeFilter + + +class MyModel(models.Model): + created_at = models.DateTimeField() + publish_at = models.DateTimeField() + review_at = models.DateTimeField() + + class Meta: + ordering = ['created_at'] + + +class MyModelAdmin(ModelAdmin): + list_filter = [ + ('created_at', PastDateRangeFilter), + ('publish_at', FutureDateRangeFilter), + ('review_at', DateRangeFilter) + ] + ordering = ['-id'] + + +def select_by(dictlist): + return [x for x in dictlist][0] + + +class DateRangeFilterTestCase(TestCase): + def setUp(self): + self.today = datetime.date.today() + self.tomorrow = self.today + datetime.timedelta(days=1) + self.one_week_ago = self.today - datetime.timedelta(days=7) + + self.object_one = MyModel.objects.create( + created_at=timezone.now(), + publish_at=timezone.now(), + review_at=timezone.now() + ) + self.object_two = MyModel.objects.create( + created_at=timezone.now() - datetime.timedelta(days=7), + publish_at=timezone.now(), + review_at=timezone.now() + ) + + self.user = User.objects.create_user(username='test', password='top_secret') + + def get_changelist(self, request, model, modeladmin): + if getattr(modeladmin, 'get_changelist_instance', None): + return modeladmin.get_changelist_instance(request) + + return ChangeList( + request, model, modeladmin.list_display, + modeladmin.list_display_links, modeladmin.list_filter, + modeladmin.date_hierarchy, modeladmin.search_fields, + modeladmin.list_select_related, modeladmin.list_per_page, + modeladmin.list_max_show_all, modeladmin.list_editable, modeladmin, + ) + + def test_datefilter(self): + self.request_factory = RequestFactory() + modeladmin = MyModelAdmin(MyModel, site) + + request = self.request_factory.get('/') + request.user = self.user + + changelist = self.get_changelist(request, MyModel, modeladmin) + + queryset = changelist.get_queryset(request) + + self.assertEqual(list(queryset), [self.object_two, self.object_one]) + filterspec = changelist.get_filters(request)[0][0] + self.assertEqual(force_text(filterspec.title), 'created at') + + def test_datefilter_filtered(self): + self.request_factory = RequestFactory() + modeladmin = MyModelAdmin(MyModel, site) + + request = self.request_factory.get('/', {'created_at__gte': self.today, + 'created_at__lte': self.tomorrow}) + request.user = self.user + + changelist = self.get_changelist(request, MyModel, modeladmin) + + queryset = changelist.get_queryset(request) + + self.assertEqual(list(queryset), [self.object_one]) + filterspec = changelist.get_filters(request)[0][0] + self.assertEqual(force_text(filterspec.title), 'created at') + + choice = select_by(filterspec.choices(changelist)) + self.assertEqual(choice['query_string'], '?') + self.assertEqual(choice['field_name'], 'created_at') diff --git a/runtests.py b/runtests.py new file mode 100644 index 0000000..ba3c16c --- /dev/null +++ b/runtests.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +from __future__ import unicode_literals + +import django + +from django.conf import settings +from django.core.management import call_command + + +settings.configure( + INSTALLED_APPS=( + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sites', + 'django.contrib.admin', + 'django.contrib.sessions', + 'daterangefilter', + ), + DATABASES={ + 'default': {'ENGINE': 'django.db.backends.sqlite3'} + }, + TEST_RUNNER='django.test.runner.DiscoverRunner', + USE_TZ=True, + TIME_ZONE='UTC', +) + +django.setup() + +if __name__ == '__main__': + call_command('test', 'daterangefilter') diff --git a/setup.py b/setup.py index c94e265..ad34405 100644 --- a/setup.py +++ b/setup.py @@ -12,10 +12,17 @@ description="Date range filter for Django admin", long_description=long_description, long_description_content_type="text/markdown", - url="https://github.com/andreynovikov/django-daterangefilter", + url="https://github.com/andreynovikov/django-daterangefilter/tree/master", packages=setuptools.find_packages(), classifiers=[ + "Framework :: Django", + "Programming Language :: Python", + "Programming Language :: Python :: 2", + "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.4", + "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ],