Skip to content

Commit

Permalink
Refact Repository Example
Browse files Browse the repository at this point in the history
  • Loading branch information
dyohan9 committed Aug 12, 2019
1 parent 5ef620e commit 828a0b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
13 changes: 5 additions & 8 deletions bothub/api/v2/example/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class Meta:
'start',
'end',
'entity',
'entity_method',
'label',
'label_method',
'created_at',
'value',
]
Expand All @@ -30,8 +28,6 @@ class Meta:
queryset=RepositoryExample.objects,
help_text=_('Example\'s ID'),
required=False)
entity_method = serializers.SerializerMethodField(required=False)
label_method = serializers.SerializerMethodField(required=False)

entity = EntityValueField()
label = LabelValueField(
Expand All @@ -40,12 +36,13 @@ class Meta:

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if kwargs.get('data') == 'GET':
self.fields['label'] = serializers.SerializerMethodField(
required=False
)
self.validators.append(EntityNotEqualLabelValidator())

def get_entity_method(self, obj):
return obj.entity.value

def get_label_method(self, obj):
def get_label(self, obj):
if not obj.entity.label:
return None
return obj.entity.label.value
Expand Down
7 changes: 7 additions & 0 deletions bothub/api/v2/repository/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,13 @@ class Meta:

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if kwargs['context'].get('request').stream is None:
self.fields['entities'] = \
RepositoryExampleEntitySerializer(
many=True,
style={'text_field': 'text'},
data='GET'
)
self.validators.append(ExampleWithIntentOrEntityValidator())
self.validators.append(IntentAndSentenceNotExistsValidator())

Expand Down
6 changes: 3 additions & 3 deletions bothub/api/v2/tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ def test_entity_has_label(self):
status.HTTP_200_OK)
entity = content_data.get('entities')[0]
self.assertIn(
'label_method',
'label',
entity.keys())

def test_entity_has_valid_label(self):
Expand All @@ -1445,10 +1445,10 @@ def test_entity_has_valid_label(self):
status.HTTP_200_OK)
entity = content_data.get('entities')[0]
self.assertIn(
'label_method',
'label',
entity.keys())
self.assertEqual(
entity.get('label_method'),
entity.get('label'),
label)


Expand Down

0 comments on commit 828a0b3

Please sign in to comment.