Skip to content

Commit

Permalink
Merge branch 'api-v2' of https://github.com/Ilhasoft/bothub-engine in…
Browse files Browse the repository at this point in the history
…to api-v2
  • Loading branch information
dougppaz committed Sep 27, 2018
2 parents 6305ef1 + 372be83 commit acd5677
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
2 changes: 2 additions & 0 deletions bothub/api/v2/examples/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def filter_order_by_translation(self, queryset, name, value):
return result_queryset

def filter_label(self, queryset, name, value):
if value == 'other':
return queryset.filter(entities__entity__label__isnull=True)
return queryset.filter(entities__entity__label__value=value)

def filter_entity(self, queryset, name, value):
Expand Down
23 changes: 19 additions & 4 deletions bothub/api/v2/repository/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ class Meta:
'examples__count',
]

entities = serializers.SlugRelatedField(
many=True,
slug_field='value',
read_only=True)
entities = serializers.SerializerMethodField()
examples__count = serializers.SerializerMethodField()

def get_entities(self, obj):
entities = obj.repository.other_entities \
if obj.value == 'other' else obj.entities.all()
return map(lambda e: e.value, entities)

def get_examples__count(self, obj):
if obj.value == 'other':
return obj.repository.examples(
exclude_deleted=True).filter(
entities__entity__in=obj.repository.other_entities) \
.count()
return obj.examples().count()


Expand Down Expand Up @@ -93,6 +100,7 @@ class Meta:
'intents',
'intents_list',
'labels',
'other_label',
'examples__count',
'absolute_url',
'authorization',
Expand Down Expand Up @@ -134,6 +142,7 @@ class Meta:
source='current_labels',
many=True,
read_only=True)
other_label = serializers.SerializerMethodField()
examples__count = serializers.SerializerMethodField()
absolute_url = serializers.SerializerMethodField()
authorization = serializers.SerializerMethodField()
Expand All @@ -153,6 +162,12 @@ def get_intents(self, obj):
def get_intents_list(self, obj):
return obj.intents

def get_other_label(self, obj):
return RepositoryEntityLabelSerializer(
RepositoryEntityLabel(
repository=obj,
value='other')).data

def get_examples__count(self, obj):
return obj.examples().count()

Expand Down
11 changes: 6 additions & 5 deletions bothub/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,19 @@ def entities_list(self):

@property
def current_labels(self):
return self.labels.filter(entities__value__in=self.examples(
exclude_deleted=True).exclude(
entities__entity__value__isnull=True).values_list(
'entities__entity__value',
flat=True).distinct()).distinct()
return self.labels.filter(
entities__value__in=self.entities_list).distinct()

@property
def labels_list(self):
return self.current_labels.values_list(
'value',
flat=True).distinct()

@property
def other_entities(self):
return self.current_entities.filter(label__isnull=True)

@property
def admins(self):
admins = [self.owner] + [
Expand Down
37 changes: 37 additions & 0 deletions bothub/common/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,3 +1045,40 @@ def test_set_label_to_none(self):
name_entity.set_label(None)

self.assertIsNone(name_entity.label)


class RepositoryOtherEntitiesTest(TestCase):
def setUp(self):
self.owner = User.objects.create_user('owner@user.com', 'user')

self.repository = Repository.objects.create(
owner=self.owner,
name='Test',
slug='test',
language=languages.LANGUAGE_EN)

self.example = RepositoryExample.objects.create(
repository_update=self.repository.current_update(),
text='my name is Douglas')

self.example_entity_1 = RepositoryExampleEntity.objects.create(
repository_example=self.example,
start=11,
end=18,
entity='douglas')
entity = self.example_entity_1.entity
entity.set_label('name')
entity.save()

self.example_entity_2 = RepositoryExampleEntity.objects.create(
repository_example=self.example,
start=0,
end=2,
entity='object')

def test_ok(self):
other_entities = self.repository.other_entities
self.assertEqual(
other_entities.count(),
1)
self.assertIn(self.example_entity_2.entity, other_entities)

0 comments on commit acd5677

Please sign in to comment.