Skip to content

Commit

Permalink
Repack
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Dec 13, 2018
1 parent bb4f8d4 commit cd3657d
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 3 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.*" }
Expand All @@ -32,4 +31,4 @@ branches:
- master

script:
- echo "TODO"
- python runtests.py
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions daterangefilter/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

# required for tests.py
File renamed without changes.
File renamed without changes.
105 changes: 105 additions & 0 deletions daterangefilter/tests.py
Original file line number Diff line number Diff line change
@@ -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')
31 changes: 31 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
@@ -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')
9 changes: 8 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down

0 comments on commit cd3657d

Please sign in to comment.