Skip to content

Commit

Permalink
Merge 3ea9250 into 192f128
Browse files Browse the repository at this point in the history
  • Loading branch information
helllllllder committed Dec 1, 2021
2 parents 192f128 + 3ea9250 commit 1ad9791
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ You can set environment variables in your OS, write on ```.env``` file or pass v
| REPOSITORY_NLP_LOG_LIMIT | ```int``` | ```10000``` | Limit of query size to repository log.
| REPOSITORY_RESTRICT_ACCESS_NLP_LOGS | ```list``` | ```[]``` | Restricts log access to a particular or multiple intelligences
| REPOSITORY_KNOWLEDGE_BASE_DESCRIPTION_LIMIT | ```int``` | ```450``` | Limit of characters in the knowledge base description
| REPOSITORY_EXAMPLE_TEXT_WORDS_LIMIT | ```int``` | ```200``` | Limit of words for the example sentence text
| ELASTICSEARCH_DSL | ```string``` | ```es:9200``` | URL Elasticsearch.
| ELASTICSEARCH_NUMBER_OF_SHARDS | ```int``` | ```1``` | Specify the number of shards for the indexes.
| ELASTICSEARCH_NUMBER_OF_REPLICAS | ```int``` | ```1``` | Specify the number of replicas for the indexes.
Expand Down
2 changes: 2 additions & 0 deletions bothub/api/v2/knowledge_base/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from bothub.api.v2.repository.validators import ExampleTextHasLettersValidator
from rest_framework import serializers

from bothub.common import languages
Expand Down Expand Up @@ -50,6 +51,7 @@ class Meta:
]
read_only_fields = ["created_at", "last_update"]

text = serializers.CharField(required=False, validators=[ExampleTextHasLettersValidator()])
knowledge_base = serializers.PrimaryKeyRelatedField(
queryset=QAKnowledgeBase.objects
)
Expand Down
5 changes: 4 additions & 1 deletion bothub/api/v2/repository/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
CanContributeInRepositoryVersionValidator,
CanCreateRepositoryInOrganizationValidator,
ExampleWithIntentOrEntityValidator,
ExampleTextHasLettersValidator,
ExampleTextHasLimitedWordsValidator,
IntentValidator,
)
from ..translation.validators import (
Expand Down Expand Up @@ -1249,7 +1251,7 @@ class Meta:
ref_name = None

id = serializers.PrimaryKeyRelatedField(read_only=True, style={"show": False})
text = EntityText(style={"entities_field": "entities"}, required=False)
text = EntityText(style={"entities_field": "entities"}, required=False, validators=[ExampleTextHasLettersValidator(), ExampleTextHasLimitedWordsValidator()])
repository = serializers.PrimaryKeyRelatedField(
queryset=Repository.objects,
validators=[CanContributeInRepositoryValidator()],
Expand Down Expand Up @@ -1322,6 +1324,7 @@ def create(self, validated_data):
intent, created = RepositoryIntent.objects.get_or_create(
repository_version=version_id, text=intent_text
)

validated_data.update({"intent": intent})
example = self.Meta.model.objects.create(**validated_data)
for entity_data in entities_data:
Expand Down
21 changes: 21 additions & 0 deletions bothub/api/v2/repository/validators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from django.conf import settings

from django.utils.translation import ugettext_lazy as _
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -77,6 +78,26 @@ def set_context(self, serializer):
self.request = serializer.context.get("request")


class ExampleTextHasLettersValidator(object):
def __call__(self, value):
reg = re.compile(r".[a-zA-Z_]")
if not reg.match(value):
raise ValidationError(_("Enter a valid value that has letters in it"))


class ExampleTextHasLimitedWordsValidator(object):
def __call__(self, value):
count = len(value.split())
if count > settings.REPOSITORY_EXAMPLE_TEXT_WORDS_LIMIT:
raise ValidationError(
_(
"Enter a valid value that is in the range of "
+ str(settings.REPOSITORY_EXAMPLE_TEXT_WORDS_LIMIT)
+ " words"
)
)


class APIExceptionCustom(APIException):
"""Readers error class"""

Expand Down
97 changes: 95 additions & 2 deletions bothub/api/v2/tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from django.conf import settings

from django.test import TestCase
from django.test import RequestFactory
Expand All @@ -12,9 +13,10 @@

from bothub.api.v2.tests.utils import create_user_and_token
from bothub.api.v2.examples.views import ExamplesViewSet
from bothub.api.v2.repository.views import RepositoryExampleViewSet


class ListExamplesAPITestCase(TestCase):
class DefaultExamplesAPITestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.owner, self.owner_token = create_user_and_token("owner")
Expand Down Expand Up @@ -79,13 +81,15 @@ def setUp(self):
repository_version_language=self.repository_2.current_version(
languages.LANGUAGE_PT
),
text="oi",
text="oi ",
intent=self.example2_intent_1,
)
self.translation_6 = RepositoryTranslatedExample.objects.create(
original_example=self.example_6, language=languages.LANGUAGE_EN, text="hi"
)


class ListExamplesAPITestCase(DefaultExamplesAPITestCase):
def request(self, data={}, token=None):
authorization_header = (
{"HTTP_AUTHORIZATION": "Token {}".format(token.key)} if token else {}
Expand Down Expand Up @@ -247,3 +251,92 @@ def test_filter_entity(self):
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(content_data.get("count"), 1)


class CreateExamplesAPITestCase(DefaultExamplesAPITestCase):
def request(self, data, token):
authorization_header = (
{"HTTP_AUTHORIZATION": "Token {}".format(token.key)}
)
request = self.factory.post(
"/v2/repository/example/",
json.dumps(data),
content_type="application/json",
**authorization_header,
)

response = RepositoryExampleViewSet.as_view({"post": "create"})(request)
response.render()
content_data = json.loads(response.content)
return (response, content_data)

def test_ok(self):
data = {
"repository": str(self.repository.uuid),
"repository_version": self.repository.current_version().repository_version.pk,
"text": "testing 123 yés ///????³³²²¹¹£ ++++-----",
"language": "en",
"entities": [
{
"start": 9,
"end": 11,
"entity": "numero",
}
],
"intent": str(self.example_intent_1.pk),
"is_corrected": False
}

response, content_data = self.request(
data,
self.owner_token,
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_text_without_letters(self):
data = {
"repository": str(self.repository.uuid),
"repository_version": self.repository.current_version().repository_version.pk,
"text": " ---- //// -----",
"language": "en",
"entities": [
{
"start": 9,
"end": 11,
"entity": "numero",
}
],
"intent": str(self.example_intent_1.pk),
"is_corrected": False
}
response, content_data = self.request(
data,
self.owner_token,
)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_text_words_limit(self):
limit = settings.REPOSITORY_EXAMPLE_TEXT_WORDS_LIMIT + 1
text = " ".join(['teste' for x in range(limit)])
data = {
"repository": str(self.repository.uuid),
"repository_version": self.repository.current_version().repository_version.pk,
"text": text,
"language": "en",
"entities": [
{
"start": 9,
"end": 11,
"entity": "numero",
}
],
"intent": str(self.example_intent_1.pk),
"is_corrected": False
}
response, content_data = self.request(
data,
self.owner_token,
)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
30 changes: 30 additions & 0 deletions bothub/common/migrations/0112_auto_20211201_1330.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 3.2.8 on 2021-12-01 13:30

import django.core.validators
from django.db import migrations, models
import re


class Migration(migrations.Migration):

dependencies = [
('common', '0111_auto_20210908_1135'),
]

operations = [
migrations.AlterField(
model_name='qatext',
name='text',
field=models.TextField(help_text='QA context text', max_length=25000, validators=[django.core.validators.RegexValidator(re.compile('.[-a-zA-Z_]'), 'Enter a valid value that have letters in it', 'invalid')], verbose_name='text'),
),
migrations.AlterField(
model_name='repositoryexample',
name='text',
field=models.TextField(help_text='Example text', validators=[django.core.validators.RegexValidator(re.compile('.[-a-zA-Z_]'), 'Enter a valid value that have letters in it', 'invalid')], verbose_name='text'),
),
migrations.AlterField(
model_name='repositorytranslatedexample',
name='text',
field=models.TextField(help_text='Translation text', validators=[django.core.validators.RegexValidator(re.compile('.[-a-zA-Z_]'), 'Enter a valid value that have letters in it', 'invalid')], verbose_name='text'),
),
]
15 changes: 12 additions & 3 deletions bothub/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .exceptions import TrainingNotAllowed
from .. import utils


item_key_regex = _lazy_re_compile(r"^[-a-z0-9_]+\Z")
validate_item_key = RegexValidator(
item_key_regex,
Expand All @@ -35,6 +36,12 @@
"invalid",
)

validate_text = RegexValidator(
_lazy_re_compile(r".[-a-zA-Z_]"),
_("Enter a valid value that have letters in it"),
"invalid",
)


def can_t_be_other(value): # pragma: no cover
if value == "other":
Expand Down Expand Up @@ -1428,7 +1435,7 @@ class Meta:
RepositoryVersionLanguage, models.CASCADE, related_name="added", editable=False
)
text = models.TextField(
_("text"), help_text=_("Example text"), blank=False, null=False
_("text"), help_text=_("Example text"), blank=False, null=False, validators=[validate_text]
)
intent = models.ForeignKey(RepositoryIntent, models.CASCADE)
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
Expand Down Expand Up @@ -1541,7 +1548,7 @@ class Meta:
help_text=_("Translation language"),
validators=[languages.validate_language],
)
text = models.TextField(_("text"), help_text=_("Translation text"))
text = models.TextField(_("text"), help_text=_("Translation text"), validators=[validate_text])
created_at = models.DateTimeField(_("created at"), auto_now_add=True)

objects = RepositoryTranslatedExampleManager()
Expand Down Expand Up @@ -2333,7 +2340,9 @@ class QAtext(models.Model):
knowledge_base = models.ForeignKey(
QAKnowledgeBase, on_delete=models.CASCADE, related_name="texts"
)
text = models.TextField(_("text"), help_text=_("QA context text"), max_length=25000)
text = models.TextField(
_("text"), help_text=_("QA context text"), max_length=25000, validators=[validate_text]
)
language = models.CharField(
_("language"),
max_length=5,
Expand Down
4 changes: 4 additions & 0 deletions bothub/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
CONNECT_CERTIFICATE_GRPC_CRT=(str, None),
REPOSITORY_RESTRICT_ACCESS_NLP_LOGS=(list, []),
REPOSITORY_KNOWLEDGE_BASE_DESCRIPTION_LIMIT=(int, 450),
REPOSITORY_EXAMPLE_TEXT_WORDS_LIMIT=(int, 200),
ELASTICSEARCH_DSL=(str, "localhost:9200"),
ELASTICSEARCH_REPOSITORYNLPLOG_INDEX=(str, "ai_repositorynlplog"),
ELASTICSEARCH_REPOSITORYQANLPLOG_INDEX=(str, "ai_repositoryqanlplog"),
Expand Down Expand Up @@ -443,6 +444,9 @@
# Limit of characters for the knowledge base description
REPOSITORY_KNOWLEDGE_BASE_DESCRIPTION_LIMIT = env.list("REPOSITORY_KNOWLEDGE_BASE_DESCRIPTION_LIMIT", default=450)

# Limit of words for the example sentence
REPOSITORY_EXAMPLE_TEXT_WORDS_LIMIT = env.list("REPOSITORY_EXAMPLE_TEXT_WORDS_LIMIT", default=200)


# django_redis
CACHES = {
Expand Down

0 comments on commit 1ad9791

Please sign in to comment.