Skip to content

Commit 374cf31

Browse files
committed
fix bug with custom_objectID
1 parent d6dd556 commit 374cf31

File tree

8 files changed

+65
-18
lines changed

8 files changed

+65
-18
lines changed

.coveragerc

Whitespace-only changes.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
Algolia Search for Django
22
==================
33

4-
This package let you easily integrate the Algolia Search API to your favorite ORM. It's based on the [algoliasearch-client-python](https://github.com/algolia/algoliasearch-client-python) package. Both Python 2.x and 3.x are supported.
4+
This package let you easily integrate the Algolia Search API to your favorite ORM. It's based on the [algoliasearch-client-python](https://github.com/algolia/algoliasearch-client-python) package.
55

66
You might be interested in the sample Django application providing a typeahead.js based auto-completion and Google-like instant search: [algoliasearch-django-example](https://github.com/algolia/algoliasearch-django-example)
77

8+
Compatible with **Python 2.7**, **Python 3.2+** and **Django 1.7+**
9+
810
[![Build Status](https://travis-ci.org/algolia/algoliasearch-django.svg?branch=master)](https://travis-ci.org/algolia/algoliasearch-django)
911
[![Coverage Status](https://coveralls.io/repos/algolia/algoliasearch-django/badge.svg?branch=master)](https://coveralls.io/r/algolia/algoliasearch-django)
1012
[![PyPI version](https://badge.fury.io/py/algoliasearch-django.svg?branch=master)](http://badge.fury.io/py/algoliasearch-django)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
django
1+
django>=1.7
22
algoliasearch
33
tox

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
'django.contrib.algoliasearch.management': 'src/management',
4242
'django.contrib.algoliasearch.management.commands': 'src/management/commands'
4343
},
44-
install_requires = ['django', 'algoliasearch'],
44+
install_requires = ['django>=1.7', 'algoliasearch'],
4545
description = 'Algolia Search integration for Django',
4646
long_description = README,
4747
author = 'Algolia Team',

src/models.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,14 @@ def __init__(self, model, client):
6363

6464
# Check geo_field
6565
if self.geo_field:
66-
if hasattr(model, self.geo_field):
67-
attr = getattr(model, self.geo_field)
68-
if not (isinstance(attr, tuple) or callable(attr)):
69-
raise AlgoliaIndexError(
70-
'`geo_field` should be a tuple or a callable that returns a tuple.')
71-
else:
66+
if not hasattr(model, self.geo_field):
7267
raise AlgoliaIndexError('{} is not an attribute of {}.'.format(
7368
self.geo_field, model))
7469

7570
# Check custom_objectID
7671
if self.custom_objectID:
77-
if hasattr(model, self.custom_objectID):
78-
attr = getattr(model, elf.custom_objectID)
79-
if not (isinstance(attr, str) or isinstance(attr, int)):
80-
raise AlgoliaIndexError(
81-
'`custom_objectID` should be a string or an integer.')
82-
else:
83-
raise AlgoliaIndex('`{}` is not an attribute of {}.'.format(
72+
if not (hasattr(model, self.custom_objectID) or (self.custom_objectID in all_fields)):
73+
raise AlgoliaIndex('{} is not an attribute of {}.'.format(
8474
self.custom_objectID, model))
8575

8676
def __set_index(self, client):
@@ -108,7 +98,7 @@ def __set_index(self, client):
10898
def __get_objectID(self, instance):
10999
'''Return the objectID of an instance.'''
110100
if self.custom_objectID:
111-
return getattr(instance, custom_objectID)
101+
return getattr(instance, self.custom_objectID)
112102
else:
113103
return instance.pk
114104

tests/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
class Example(models.Model):
5+
uid = models.IntegerField()
56
name = models.CharField(max_length=20)
67
address = models.CharField(max_length=200)
78
lat = models.FloatField()

tests/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@
6161
ALGOLIA = {
6262
'APPLICATION_ID': os.getenv('ALGOLIA_APPLICATION_ID'),
6363
'API_KEY': os.getenv('ALGOLIA_API_KEY'),
64-
'INDEX_PREFIX': 'django',
64+
'INDEX_PREFIX': 'django' + os.getenv('TRAVIS_JOB_NUMBER', ''),
6565
'INDEX_SUFFIX': 'test'
6666
}

tests/test_index.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33

44
from django.contrib.algoliasearch import AlgoliaIndex
55
from django.contrib.algoliasearch import algolia_engine
6+
from django.contrib.algoliasearch.models import AlgoliaIndexError
67

78
from .models import Example
89

910

1011
class IndexTestCase(TestCase):
1112
def setUp(self):
1213
self.client = algolia_engine.client
14+
self.instance = Example(uid=4, name='SuperK', address='Finland',
15+
lat=63.3, lng=-32.0)
1316

1417
def test_default_index_name(self):
1518
index = AlgoliaIndex(Example, self.client)
@@ -21,3 +24,54 @@ class ExampleIndex(AlgoliaIndex):
2124

2225
index = ExampleIndex(Example, self.client)
2326
self.assertEqual(index.index_name, 'django_customName_test')
27+
28+
def test_geo_fields(self):
29+
class ExampleIndex(AlgoliaIndex):
30+
geo_field = 'location'
31+
32+
index = ExampleIndex(Example, self.client)
33+
obj = index._AlgoliaIndex__build_object(self.instance)
34+
self.assertEqual(obj['_geoloc'], {'lat':63.3, 'lng':-32.0})
35+
36+
def test_custom_objectID(self):
37+
class ExampleIndex(AlgoliaIndex):
38+
custom_objectID = 'uid'
39+
40+
index = ExampleIndex(Example, self.client)
41+
obj = index._AlgoliaIndex__build_object(self.instance)
42+
self.assertEqual(obj['objectID'], 4)
43+
44+
def test_one_field(self):
45+
class ExampleIndex(AlgoliaIndex):
46+
fields = 'name'
47+
48+
index = ExampleIndex(Example, self.client)
49+
obj = index._AlgoliaIndex__build_object(self.instance)
50+
self.assertNotIn('uid', obj)
51+
self.assertIn('name', obj)
52+
self.assertNotIn('address', obj)
53+
self.assertNotIn('lat', obj)
54+
self.assertNotIn('lng', obj)
55+
self.assertNotIn('location', obj)
56+
self.assertEqual(len(obj), 2)
57+
58+
def test_multiple_fields(self):
59+
class ExampleIndex(AlgoliaIndex):
60+
fields = ('name', 'address')
61+
62+
index = ExampleIndex(Example, self.client)
63+
obj = index._AlgoliaIndex__build_object(self.instance)
64+
self.assertNotIn('uid', obj)
65+
self.assertIn('name', obj)
66+
self.assertIn('address', obj)
67+
self.assertNotIn('lat', obj)
68+
self.assertNotIn('lng', obj)
69+
self.assertNotIn('location', obj)
70+
self.assertEqual(len(obj), 3)
71+
72+
def test_invalid_fields(self):
73+
class ExampleIndex(AlgoliaIndex):
74+
fields = ('name', 'color')
75+
76+
with self.assertRaises(AlgoliaIndexError):
77+
index = ExampleIndex(Example, self.client)

0 commit comments

Comments
 (0)