Skip to content

Commit

Permalink
Simplify validation for component and release grouping.
Browse files Browse the repository at this point in the history
  • Loading branch information
erichuanggit committed Aug 19, 2015
1 parent 8171059 commit 2c0067b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
25 changes: 4 additions & 21 deletions pdc/apps/component/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,31 +480,14 @@ class GroupSerializer(StrictSerializerMixin, serializers.ModelSerializer):
)

def validate(self, value):
# POST
# # POST
if not self.instance:
components = value.get('components', [])
release = value.get('release')
# PUT: unique_together fields have to be provided
elif not self.partial:
if 'components' in value:
components = value.get('components', [])
else:
components = self.instance.components.all()
release = value.get('release')
# PATCH:
# PUT or PATCH
else:
if 'components' in value:
if 'release' in value:
release = value.get('release')
else:
release = self.instance.release
components = value.get('components', [])
else:
if 'release' in value:
release = value.get('release')
else:
release = self.instance.release
components = self.instance.components.all()
components = value.get('components', self.instance.components.all())
release = value.get('release', self.instance.release)

for component in components:
if component.release != release:
Expand Down
8 changes: 8 additions & 0 deletions pdc/apps/component/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2174,6 +2174,14 @@ def test_create_group_with_components_id(self):
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertNumChanges([1])

def test_create_group_with_components_and_different_release(self):
url = reverse('componentgroup-list')
data = {'group_type': 'type2', 'release': 'release-2.0', 'description': 'dd',
'components': [{'id': 1}]}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertNumChanges([])

def test_create_group_with_components_unique_together_fields(self):
url = reverse('componentgroup-list')
data = {'group_type': 'type2', 'release': 'release-1.0', 'description': 'dd',
Expand Down

0 comments on commit 2c0067b

Please sign in to comment.