Add support for adding or editing document types to smart links the API.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2017-03-13 22:55:29 -04:00
parent 7341971c86
commit 5ddb3f1cff
2 changed files with 61 additions and 3 deletions

View File

@@ -1,9 +1,13 @@
from __future__ import absolute_import, unicode_literals
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.reverse import reverse
from documents.serializers import DocumentSerializer
from documents.models import DocumentType
from documents.serializers import DocumentSerializer, DocumentTypeSerializer
from .models import SmartLink, SmartLinkCondition
@@ -41,13 +45,15 @@ class SmartLinkSerializer(serializers.HyperlinkedModelSerializer):
conditions_url = serializers.HyperlinkedIdentityField(
view_name='rest_api:smartlinkcondition-list'
)
document_types = DocumentTypeSerializer(read_only=True, many=True)
class Meta:
extra_kwargs = {
'url': {'view_name': 'rest_api:smartlink-detail'},
}
fields = (
'conditions_url', 'dynamic_label', 'enabled', 'label', 'id', 'url'
'conditions_url', 'document_types', 'dynamic_label', 'enabled',
'label', 'id', 'url'
)
model = SmartLink
@@ -104,12 +110,38 @@ class WritableSmartLinkSerializer(serializers.ModelSerializer):
conditions_url = serializers.HyperlinkedIdentityField(
view_name='rest_api:smartlinkcondition-list'
)
document_types_pk_list = serializers.CharField(
help_text=_(
'Comma separated list of document type primary keys to which this '
'smart link will be attached.'
), required=False
)
class Meta:
extra_kwargs = {
'url': {'view_name': 'rest_api:smartlink-detail'},
}
fields = (
'conditions_url', 'dynamic_label', 'enabled', 'label', 'id', 'url'
'conditions_url', 'document_types_pk_list', 'dynamic_label',
'enabled', 'label', 'id', 'url'
)
model = SmartLink
def validate(self, attrs):
document_types_pk_list = attrs.pop('document_types_pk_list', None)
document_types_result = []
if document_types_pk_list:
for pk in document_types_pk_list.split(','):
try:
document_type = DocumentType.objects.get(pk=pk)
except DocumentType.DoesNotExist:
raise ValidationError(_('No such document type: %s') % pk)
else:
# Accumulate valid stored document_type pks
document_types_result.append(document_type.pk)
attrs['document_types'] = DocumentType.objects.filter(
pk__in=document_types_result
)
return attrs

View File

@@ -4,6 +4,7 @@ from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.test import override_settings
from rest_framework.exceptions import ValidationError
from rest_framework.test import APITestCase
from documents.models import DocumentType
@@ -72,6 +73,26 @@ class SmartLinkAPITestCase(APITestCase):
self.assertEqual(SmartLink.objects.count(), 1)
self.assertEqual(smart_link.label, TEST_SMART_LINK_LABEL)
def test_smart_link_create_with_document_types_view(self):
self._create_document_type()
response = self.client.post(
reverse('rest_api:smartlink-list'), data={
'label': TEST_SMART_LINK_LABEL,
'document_types_pk_list': self.document_type.pk
},
)
smart_link = SmartLink.objects.first()
self.assertEqual(response.data['id'], smart_link.pk)
self.assertEqual(response.data['label'], TEST_SMART_LINK_LABEL)
self.assertEqual(SmartLink.objects.count(), 1)
self.assertEqual(smart_link.label, TEST_SMART_LINK_LABEL)
self.assertQuerysetEqual(
smart_link.document_types.all(), (repr(self.document_type),)
)
def test_smart_link_delete_view(self):
smart_link = self._create_smart_link()
@@ -93,18 +114,23 @@ class SmartLinkAPITestCase(APITestCase):
)
def test_smart_link_patch_view(self):
self._create_document_type()
smart_link = self._create_smart_link()
self.client.patch(
reverse('rest_api:smartlink-detail', args=(smart_link.pk,)),
data={
'label': TEST_SMART_LINK_LABEL_EDITED,
'document_types_pk_list': self.document_type.pk
}
)
smart_link.refresh_from_db()
self.assertEqual(smart_link.label, TEST_SMART_LINK_LABEL_EDITED)
self.assertQuerysetEqual(
smart_link.document_types.all(), (repr(self.document_type),)
)
def test_smart_link_put_view(self):
smart_link = self._create_smart_link()