Skip to content
This repository has been archived by the owner on Jan 18, 2020. It is now read-only.

Commit

Permalink
Add setting to control when stats link is included in field links
Browse files Browse the repository at this point in the history
Signed-off-by: Don Naegely <naegelyd@gmail.com>
  • Loading branch information
naegelyd committed Sep 17, 2014
1 parent 306fdb4 commit b888263
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion serrano/resources/field/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import logging
from django.conf import settings
from django.core.urlresolvers import reverse
from preserialize.serialize import serialize
from restlib2.http import codes
Expand All @@ -11,7 +12,6 @@
from .. import templates

can_change_field = lambda u: u.has_perm('avocado.change_datafield')
stats_capable = lambda x: not x.searchable
log = logging.getLogger(__name__)


Expand Down Expand Up @@ -54,6 +54,8 @@ def field_posthook(instance, data, request):
args=[instance.pk])),
}

stats_capable = getattr(settings, 'STATS_CAPABLE',
lambda x: not x.searchable)
if stats_capable(instance):
data['_links']['stats'] = {
'href': uri(reverse('serrano:field-stats',
Expand Down
19 changes: 19 additions & 0 deletions tests/cases/resources/tests/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ def test_get_all(self):
self.assertEqual(response.status_code, codes.ok)
self.assertEqual(len(json.loads(response.content)), 5)

def test_stats_capable_setting(self):
# Initially, the default stats_capable check will be used that allows
# for stats on all non-searchable fields so we will expect that
# endpoint to be included in the _links.
response = self.client.get('/api/fields/2/',
HTTP_ACCEPT='applicaton/json')
self.assertEqual(response.status_code, codes.ok)
content = json.loads(response.content)
self.assertTrue('stats' in content['_links'])

# Now, overriding that setting so that this field is not
# "stats_capable" should remove the stats endpoint from _links.
with self.settings(STATS_CAPABLE=lambda x: x.id != 2):
response = self.client.get('/api/fields/2/',
HTTP_ACCEPT='applicaton/json')
self.assertEqual(response.status_code, codes.ok)
content = json.loads(response.content)
self.assertFalse('stats' in content['_links'])

@override_settings(SERRANO_CHECK_ORPHANED_FIELDS=True)
def test_get_all_orphan(self):
# Orphan one of the fields we are about to retrieve
Expand Down

0 comments on commit b888263

Please sign in to comment.