Skip to content

Commit

Permalink
Merge pull request #99 from maingoh/fixed_nullable_fields
Browse files Browse the repository at this point in the history
Fixed sort on None fields
  • Loading branch information
asfaltboy authored Apr 26, 2020
2 parents 85c282b + 4029e12 commit 142ecd0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
35 changes: 34 additions & 1 deletion advanced_filters/tests/test_get_field_choices_view.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import json
import sys
from datetime import timedelta
from operator import attrgetter

import django
import factory
import pytest
from django.utils import timezone
from django.utils.encoding import force_str
from tests.factories import ClientFactory

Expand Down Expand Up @@ -47,7 +51,7 @@ def assert_view_error(client, error, exception=None, **view_kwargs):
else:
ARGUMENT_LENGTH_ERROR = "need more than 1 value to unpack"

if sys.version_info < (3, ) and django.VERSION < (1, 11):
if sys.version_info < (3,) and django.VERSION < (1, 11):
MISSING_FIELD_ERROR = "SalesRep has no field named u'baz'"
else:
MISSING_FIELD_ERROR = "SalesRep has no field named 'baz'"
Expand Down Expand Up @@ -135,3 +139,32 @@ def test_distinct_database_choices(user, client, settings):
assert_json(
response.content, {"results": [{"id": "[email protected]", "text": "[email protected]"}]}
)


def test_choices_no_date_fields_support(user, client, settings):
settings.ADVANCED_FILTERS_MAX_CHOICES = 4
logins = [timezone.now(), timezone.now() - timedelta(days=1), None]
ClientFactory.create_batch(
3, assigned_to=user, email="[email protected]", last_login=factory.Iterator(logins)
)
view_url = reverse(
URL_NAME, kwargs=dict(model="customers.Client", field_name="last_login")
)
response = client.get(view_url)
assert_json(response.content, {"results": []})


def test_choices_has_null(user, client, settings):
settings.ADVANCED_FILTERS_MAX_CHOICES = 4
named_users = ClientFactory.create_batch(2, assigned_to=user)
names = [None] + sorted(set([nu.first_name for nu in named_users]))
assert len(named_users) == 2
ClientFactory.create_batch(2, assigned_to=user, first_name=None)
view_url = reverse(
URL_NAME, kwargs=dict(model="customers.Client", field_name="first_name")
)
response = client.get(view_url)
assert_json(
response.content,
{"results": [{"id": name, "text": str(name)} for name in names]},
)
3 changes: 1 addition & 2 deletions advanced_filters/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from operator import itemgetter
import logging

from django.apps import apps
Expand Down Expand Up @@ -72,6 +71,6 @@ def get(self, request, model=None, field_name=None):
choices = []

results = [{'id': c[0], 'text': force_str(c[1])} for c in sorted(
choices, key=itemgetter(0))]
choices, key=lambda x: (x[0] is not None, x[0]))]

return self.render_json_response({'results': results})
2 changes: 1 addition & 1 deletion tests/customers/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Migration(migrations.Migration):
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('language', models.CharField(choices=[(b'en', b'English'), (b'sp', b'Spanish'), (b'it', b'Italian')], default=b'en', max_length=8)),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')),
('first_name', models.CharField(null=True, max_length=30, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=30, verbose_name='last name')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
Expand Down
2 changes: 1 addition & 1 deletion tests/customers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Client(AbstractBaseUser):
language = models.CharField(max_length=8, choices=VALID_LANGUAGES,
default='en')
email = models.EmailField(_('email address'), blank=True)
first_name = models.CharField(_('first name'), max_length=30, blank=True)
first_name = models.CharField(_('first name'), max_length=30, null=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
is_active = models.BooleanField(
_('active'), default=True,
Expand Down
1 change: 1 addition & 0 deletions tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ class ClientFactory(factory.django.DjangoModelFactory):
class Meta:
model = 'customers.Client'

first_name = factory.faker.Faker('first_name')
email = factory.Sequence(lambda n: 'c%[email protected]' % n)

0 comments on commit 142ecd0

Please sign in to comment.