Skip to content

Commit

Permalink
Accept "unlimited" as valid input for role count limit
Browse files Browse the repository at this point in the history
This patch also fixes a bug where changes to count_limit would not be
logged.

JIRA: PDC-1118
  • Loading branch information
lubomir committed Oct 26, 2015
1 parent 9fcfe91 commit 7ad08a7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pdc/apps/contact/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __unicode__(self):
return u"%s" % self.name

def export(self, fields=None):
_fields = ['name'] if fields is None else fields
_fields = ['name', 'count_limit'] if fields is None else fields
return model_to_dict(self, fields=_fields)


Expand Down
24 changes: 20 additions & 4 deletions pdc/apps/contact/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,29 @@
from .models import ContactRole, Person, Maillist, Contact, RoleContact


class LimitField(serializers.IntegerField):
UNLIMITED_STR = 'unlimited'
doc_format = '"{}"|int'.format(UNLIMITED_STR)

def __init__(self, unlimited_value, **kwargs):
super(LimitField, self).__init__(**kwargs)
self.unlimited_value = unlimited_value

def to_representation(self, obj):
if obj == self.unlimited_value:
return self.__class__.UNLIMITED_STR
return super(LimitField, self).to_representation(obj)

def to_internal_value(self, value):
if value == self.__class__.UNLIMITED_STR:
return self.unlimited_value
return super(LimitField, self).to_internal_value(value)


class ContactRoleSerializer(StrictSerializerMixin,
serializers.HyperlinkedModelSerializer):
name = serializers.SlugField()

def to_representation(self, instance):
count_limit = instance.count_limit if instance.count_limit != ContactRole.UNLIMITED else 'unlimited'
return {'name': instance.name, 'count_limit': count_limit}
count_limit = LimitField(required=False, unlimited_value=ContactRole.UNLIMITED)

class Meta:
model = ContactRole
Expand Down
18 changes: 17 additions & 1 deletion pdc/apps/contact/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rest_framework.test import APITestCase

from pdc.apps.common.test_utils import TestCaseWithChangeSetMixin
from .models import RoleContact, Person
from .models import ContactRole, RoleContact, Person


class ContactRoleRESTTestCase(TestCaseWithChangeSetMixin, APITestCase):
Expand Down Expand Up @@ -95,6 +95,22 @@ def test_delete_protect(self):
self.assertIn("protected", response.content)
self.assertNumChanges([])

def test_update_limit_unlimited(self):
response = self.client.patch(reverse('contactrole-detail', args=['allow_3_role']),
{'count_limit': 'unlimited'},
format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertNumChanges([1])
role = ContactRole.objects.get(name='allow_3_role')
self.assertEqual(role.count_limit, ContactRole.UNLIMITED)

def test_update_limit_with_random_string(self):
response = self.client.patch(reverse('contactrole-detail', args=['allow_3_role']),
{'count_limit': 'many'},
format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertNumChanges([])


class PersonRESTTestCase(TestCaseWithChangeSetMixin, APITestCase):
fixtures = ['pdc/apps/contact/fixtures/tests/person.json', ]
Expand Down

0 comments on commit 7ad08a7

Please sign in to comment.