From a3aba332eefe6ac8e517241b8fa83b9f7bc55ab1 Mon Sep 17 00:00:00 2001 From: Daniel Yohan Date: Thu, 12 Dec 2019 16:56:24 -0300 Subject: [PATCH 1/4] Adjusts --- bothub/api/v2/repository/filters.py | 2 - bothub/api/v2/tests/test_repository.py | 56 ++++++++++++++ .../migrations/0042_auto_20191212_1922.py | 18 +++++ .../migrations/0043_auto_20191212_1923.py | 18 +++++ bothub/common/models.py | 40 +++++----- bothub/common/tests.py | 76 ++++++++----------- 6 files changed, 146 insertions(+), 64 deletions(-) create mode 100644 bothub/common/migrations/0042_auto_20191212_1922.py create mode 100644 bothub/common/migrations/0043_auto_20191212_1923.py diff --git a/bothub/api/v2/repository/filters.py b/bothub/api/v2/repository/filters.py index ef6b53c2..6a25d157 100644 --- a/bothub/api/v2/repository/filters.py +++ b/bothub/api/v2/repository/filters.py @@ -8,8 +8,6 @@ from bothub.common.models import RepositoryAuthorization from bothub.common.models import RequestRepositoryAuthorization -# from bothub.common.models import RepositoryUpdate - class RepositoriesFilter(filters.FilterSet): class Meta: diff --git a/bothub/api/v2/tests/test_repository.py b/bothub/api/v2/tests/test_repository.py index 4adaa2ff..c03b3f1f 100644 --- a/bothub/api/v2/tests/test_repository.py +++ b/bothub/api/v2/tests/test_repository.py @@ -18,6 +18,7 @@ from bothub.api.v2.repository.views import RepositoryVotesViewSet from bothub.api.v2.repository.views import SearchRepositoriesViewSet from bothub.api.v2.tests.utils import create_user_and_token +from bothub.api.v2.versionning.views import RepositoryVersionViewSet from bothub.common import languages from bothub.common.models import Repository from bothub.common.models import RepositoryAuthorization @@ -1781,3 +1782,58 @@ def test_text_required(self): ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn("text", content_data.keys()) + + +class VersionsTestCase(TestCase): + def setUp(self): + self.factory = RequestFactory() + + self.owner, self.owner_token = create_user_and_token("owner") + + self.repository = Repository.objects.create( + owner=self.owner, + name="Testing", + slug="test", + language=languages.LANGUAGE_EN, + ) + current_version = self.repository.current_version() + RepositoryExample.objects.create( + repository_version_language=current_version, + text="my name is Douglas", + intent="greet", + ) + RepositoryExample.objects.create( + repository_version_language=current_version, + text="my name is John", + intent="greet", + ) + current_version.start_training(self.owner) + + def request(self, data, token=None): + authorization_header = ( + {"HTTP_AUTHORIZATION": "Token {}".format(token.key)} if token else {} + ) + request = self.factory.get( + "/v2/repository/version/", data, **authorization_header + ) + response = RepositoryVersionViewSet.as_view({"get": "list"})(request) + response.render() + content_data = json.loads(response.content) + return (response, content_data) + + def test_okay(self): + response, content_data = self.request( + {"repository": str(self.repository.uuid)}, self.owner_token + ) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(content_data.get("count"), 1) + + def test_not_authenticated(self): + response, content_data = self.request( + {"repository": str(self.repository.uuid)} + ) + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + + def test_without_repository(self): + response, content_data = self.request({}, self.owner_token) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) diff --git a/bothub/common/migrations/0042_auto_20191212_1922.py b/bothub/common/migrations/0042_auto_20191212_1922.py new file mode 100644 index 00000000..c8ccf158 --- /dev/null +++ b/bothub/common/migrations/0042_auto_20191212_1922.py @@ -0,0 +1,18 @@ +# Generated by Django 2.1.11 on 2019-12-12 19:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('common', '0041_auto_20191212_1714'), + ] + + operations = [ + migrations.AlterField( + model_name='repositoryversionlanguage', + name='last_update', + field=models.DateTimeField(verbose_name='last update'), + ), + ] diff --git a/bothub/common/migrations/0043_auto_20191212_1923.py b/bothub/common/migrations/0043_auto_20191212_1923.py new file mode 100644 index 00000000..6b3a9547 --- /dev/null +++ b/bothub/common/migrations/0043_auto_20191212_1923.py @@ -0,0 +1,18 @@ +# Generated by Django 2.1.11 on 2019-12-12 19:23 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('common', '0042_auto_20191212_1922'), + ] + + operations = [ + migrations.AlterField( + model_name='repositoryversionlanguage', + name='last_update', + field=models.DateTimeField(null=True, verbose_name='last update'), + ), + ] diff --git a/bothub/common/models.py b/bothub/common/models.py index 8bf8c966..76d8984f 100644 --- a/bothub/common/models.py +++ b/bothub/common/models.py @@ -478,10 +478,6 @@ def current_version(self, language=None, is_default=True): repository_version, created = self.versions.get_or_create(is_default=is_default) - if repository_version.created_by is None: - repository_version.created_by = repository_version.repository.owner - repository_version.save(update_fields=["created_by"]) - repository_version_language, created = RepositoryVersionLanguage.objects.get_or_create( repository_version=repository_version, language=language ) @@ -568,10 +564,13 @@ class Meta: ) repository_version = models.ForeignKey( RepositoryVersion, models.CASCADE - ) # updates related_name + ) training_log = models.TextField(_("training log"), blank=True, editable=False) created_at = models.DateTimeField(_("created at"), auto_now_add=True) - last_update = models.DateTimeField(_("last update"), auto_now_add=True) + last_update = models.DateTimeField( + _("last update"), + null=True + ) total_training_end = models.IntegerField( _("total training end"), default=0, blank=False, null=False ) @@ -731,10 +730,6 @@ def __str__(self): return "Repository Version Language #{}".format(self.id) # pragma: no cover def validate_init_train(self, by=None): - # if self.trained_at: - # raise RepositoryUpdateAlreadyTrained() - # if self.training_started_at: - # raise RepositoryUpdateAlreadyStartedTraining() if by: authorization = self.repository_version.repository.get_user_authorization( by @@ -764,13 +759,13 @@ def start_training(self, created_by): self.repository_version.save(update_fields=["created_by"]) def save_training(self, bot_data): - # if self.trained_at: - # raise RepositoryUpdateAlreadyTrained() + last_time = timezone.now() - self.training_end_at = timezone.now() + self.training_end_at = last_time + self.last_update = last_time self.bot_data = bot_data self.total_training_end += 1 - self.save(update_fields=["total_training_end", "training_end_at", "bot_data"]) + self.save(update_fields=["total_training_end", "training_end_at", "bot_data", "last_update"]) def get_bot_data(self): return self.bot_data @@ -811,6 +806,8 @@ def save(self, *args, **kwargs): self.last_update = timezone.now() self.repository_version_language.last_update = timezone.now() self.repository_version_language.save(update_fields=["last_update"]) + print(args) + print(kwargs) super(RepositoryExample, self).save(*args, **kwargs) @property @@ -838,11 +835,16 @@ def get_entities(self, language): # pragma: no cover return self.entities.all() return self.get_translation(language).entities.all() - def delete(self): - self.deleted_in = self.repository_version_language.repository_version.repository.current_version( - self.repository_version_language.language - ) - self.save(update_fields=["deleted_in"]) + # def delete(self): + # print('entrou aqui') + # print(self.repository_version_language.repository_version.repository.current_version( + # self.repository_version_language.language + # )) + # self.deleted_in = self.repository_version_language.repository_version.repository.current_version( + # self.repository_version_language.language + # ) + # print(self.deleted_in) + # self.save(update_fields=["deleted_in"]) class RepositoryTranslatedExampleManager(models.Manager): diff --git a/bothub/common/tests.py b/bothub/common/tests.py index b44f9fa6..74574576 100644 --- a/bothub/common/tests.py +++ b/bothub/common/tests.py @@ -1,6 +1,7 @@ from django.conf import settings from django.core.exceptions import ValidationError from django.test import TestCase +from django.utils import timezone from bothub.authentication.models import User from . import languages @@ -17,7 +18,7 @@ from .models import RequestRepositoryAuthorization -class RepositoryUpdateTestCase(TestCase): +class RepositoryVersionTestCase(TestCase): def setUp(self): owner = User.objects.create_user("fake@user.com", "user", "123456") self.repository = Repository.objects.create(owner=owner, slug="test") @@ -32,12 +33,12 @@ def setUp(self): def test_repository_example_entity(self): self.assertEqual(self.entity.value, "User") - # def test_repository_current_update(self): - # update1 = self.repository.current_version("en") - # self.assertEqual(update1, self.repository.current_version("en")) - # update1.training_started_at = timezone.now() - # update1.save() - # self.assertNotEqual(update1, self.repository.current_version("en")) + def test_repository_current_version(self): + update1 = self.repository.current_version("en") + self.assertEqual(update1, self.repository.current_version("en")) + update1.training_started_at = timezone.now() + update1.save() + self.assertEqual(update1, self.repository.current_version("en")) class TranslateTestCase(TestCase): @@ -439,7 +440,7 @@ def test_role_contributor_can_contribute(self): self.assertTrue(authorization_user.can_contribute) -class RepositoryUpdateTrainingTestCase(TestCase): +class RepositoryVersionTrainingTestCase(TestCase): def setUp(self): self.owner = User.objects.create_user("owner@user.com", "user") @@ -456,23 +457,6 @@ def test_train(self): update.save_training(bot_data) self.assertEqual(update.get_bot_data(), bot_data) - # def test_already_started_trained(self): - # update = self.repository.current_version() - # update.start_training(self.owner) - # with self.assertRaises(RepositoryUpdateAlreadyStartedTraining): - # update.start_training(self.owner) - - # def test_already_trained(self): - # update = self.repository.current_version() - # update.start_training(self.owner) - # update.save_training(b"") - # - # with self.assertRaises(RepositoryUpdateAlreadyTrained): - # update.start_training(self.owner) - # - # with self.assertRaises(RepositoryUpdateAlreadyTrained): - # update.save_training(self.owner) - def test_training_not_allowed(self): user = User.objects.create_user("fake@user.com", "fake") @@ -481,7 +465,7 @@ def test_training_not_allowed(self): update.start_training(user) -class RepositoryUpdateExamplesTestCase(TestCase): +class RepositoryVersionExamplesTestCase(TestCase): def setUp(self): self.owner = User.objects.create_user("owner@user.com", "user") @@ -489,36 +473,42 @@ def setUp(self): owner=self.owner, name="Test", slug="test", language=languages.LANGUAGE_EN ) RepositoryExample.objects.create( - repository_update=self.repository.current_version(), + repository_version_language=self.repository.current_version(), text="hi", intent="greet", ) example = RepositoryExample.objects.create( - repository_update=self.repository.current_version(), + repository_version_language=self.repository.current_version(), text="hello1", intent="greet", ) example.delete() + print(example.deleted_in) self.update = self.repository.current_version() self.update.start_training(self.owner) self.update.save_training(b"") - # def test_okay(self): - # new_update_1 = self.repository.current_version() - # RepositoryExample.objects.create( - # repository_update=new_update_1, text="hello", intent="greet" - # ) - # new_update_1.start_training(self.owner) - # - # new_update_2 = self.repository.current_version() - # RepositoryExample.objects.create( - # repository_update=new_update_2, text="good morning", intent="greet" - # ) - # - # self.assertEqual(self.update.examples.count(), 1) - # self.assertEqual(new_update_1.examples.count(), 2) - # self.assertEqual(new_update_2.examples.count(), 3) + def test_okay(self): + new_update_1 = self.repository.current_version() + RepositoryExample.objects.create( + repository_version_language=new_update_1, text="hello", intent="greet" + ) + new_update_1.start_training(self.owner) + + new_update_2 = self.repository.current_version() + RepositoryExample.objects.create( + repository_version_language=new_update_2, text="good morning", intent="greet" + ) + + print(self.update.examples) + + for t in self.update.examples: + print(t.deleted_in) + + self.assertEqual(self.update.examples.count(), 1) + self.assertEqual(new_update_1.examples.count(), 2) + self.assertEqual(new_update_2.examples.count(), 3) # def test_examples_deleted_consistency(self): # new_update_1 = self.repository.current_version() From e41b42f67300494b9e4dc4d2f4d1e5d2f87c67b2 Mon Sep 17 00:00:00 2001 From: Daniel Yohan Date: Thu, 12 Dec 2019 17:23:56 -0300 Subject: [PATCH 2/4] Delete examples --- bothub/api/v2/evaluate/serializers.py | 2 +- bothub/api/v2/example/serializers.py | 3 +- bothub/api/v2/repository/serializers.py | 5 +- bothub/api/v2/repository/views.py | 5 - bothub/api/v2/tests/test_repository.py | 2 +- bothub/api/v2/versionning/serializers.py | 24 +- .../migrations/0041_auto_20191212_1714.py | 248 ------------------ .../0041_delete_examples_isdeleted.py | 19 ++ .../migrations/0042_auto_20191212_1922.py | 18 -- .../migrations/0042_auto_20191212_2013.py | 133 ++++++++++ .../migrations/0043_auto_20191212_1923.py | 18 -- bothub/common/models.py | 30 +-- bothub/common/tests.py | 64 +---- 13 files changed, 166 insertions(+), 405 deletions(-) delete mode 100644 bothub/common/migrations/0041_auto_20191212_1714.py create mode 100644 bothub/common/migrations/0041_delete_examples_isdeleted.py delete mode 100644 bothub/common/migrations/0042_auto_20191212_1922.py create mode 100644 bothub/common/migrations/0042_auto_20191212_2013.py delete mode 100644 bothub/common/migrations/0043_auto_20191212_1923.py diff --git a/bothub/api/v2/evaluate/serializers.py b/bothub/api/v2/evaluate/serializers.py index 6533422f..58ee0257 100644 --- a/bothub/api/v2/evaluate/serializers.py +++ b/bothub/api/v2/evaluate/serializers.py @@ -44,7 +44,7 @@ class Meta: "entities", "created_at", ] - read_only_fields = ["deleted_in", "created_at"] + read_only_fields = ["created_at"] ref_name = None entities = RepositoryEvaluateEntitySerializer(many=True, required=False) diff --git a/bothub/api/v2/example/serializers.py b/bothub/api/v2/example/serializers.py index ce621a26..9d7b4d2a 100644 --- a/bothub/api/v2/example/serializers.py +++ b/bothub/api/v2/example/serializers.py @@ -64,7 +64,6 @@ class Meta: fields = [ "id", "repository_version", - "deleted_in", "text", "intent", "language", @@ -72,7 +71,7 @@ class Meta: "entities", "translations", ] - read_only_fields = ["deleted_in", "translations"] + read_only_fields = ["translations"] ref_name = None entities = RepositoryExampleEntitySerializer(many=True, read_only=True) diff --git a/bothub/api/v2/repository/serializers.py b/bothub/api/v2/repository/serializers.py index a5a47bd4..d0cb8f61 100644 --- a/bothub/api/v2/repository/serializers.py +++ b/bothub/api/v2/repository/serializers.py @@ -482,7 +482,6 @@ class Meta: fields = [ "id", "repository", - "deleted_in", "text", "intent", "language", @@ -491,7 +490,7 @@ class Meta: "translations", "repository_version", ] - read_only_fields = ["deleted_in"] + read_only_fields = [] ref_name = None id = serializers.PrimaryKeyRelatedField(read_only=True, style={"show": False}) @@ -547,7 +546,6 @@ def create(self, validated_data): intent=validated_data.get("intent"), repository_version_language__repository_version__repository=repository, repository_version_language__language=language, - deleted_in__isnull=True, ): raise APIExceptionCustom( detail=_("Intention and Sentence already exists") @@ -560,7 +558,6 @@ def create(self, validated_data): intent=validated_data.get("intent"), repository_version_language=repository_version_language, repository_version_language__repository_version__is_default=True, - deleted_in__isnull=True, repository_version_language__language=language, ): raise APIExceptionCustom( diff --git a/bothub/api/v2/repository/views.py b/bothub/api/v2/repository/views.py index 55aac5f3..96612410 100644 --- a/bothub/api/v2/repository/views.py +++ b/bothub/api/v2/repository/views.py @@ -492,8 +492,3 @@ def upload_examples(self, request, **kwargs): not_added.append(data) return Response({"added": count_added, "not_added": not_added}) - - def perform_destroy(self, obj): - if obj.deleted_in: - raise APIException(_("Example already deleted")) - obj.delete() diff --git a/bothub/api/v2/tests/test_repository.py b/bothub/api/v2/tests/test_repository.py index c03b3f1f..b7d0441f 100644 --- a/bothub/api/v2/tests/test_repository.py +++ b/bothub/api/v2/tests/test_repository.py @@ -1277,7 +1277,7 @@ def test_already_deleted(self): self.example.delete() response = self.request(self.example, self.owner_token) - self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) class RepositoryExampleUpdateTestCase(TestCase): diff --git a/bothub/api/v2/versionning/serializers.py b/bothub/api/v2/versionning/serializers.py index 1a16de8b..d671f763 100644 --- a/bothub/api/v2/versionning/serializers.py +++ b/bothub/api/v2/versionning/serializers.py @@ -84,23 +84,13 @@ def create(self, validated_data): ) for example in examples: - if example.deleted_in: - example_id = RepositoryExample.objects.create( - repository_version_language=version_language, - deleted_in=instance, - text=example.text, - intent=example.intent, - created_at=example.created_at, - last_update=example.last_update, - ) - else: - example_id = RepositoryExample.objects.create( - repository_version_language=version_language, - text=example.text, - intent=example.intent, - created_at=example.created_at, - last_update=example.last_update, - ) + example_id = RepositoryExample.objects.create( + repository_version_language=version_language, + text=example.text, + intent=example.intent, + created_at=example.created_at, + last_update=example.last_update, + ) example_entites = RepositoryExampleEntity.objects.filter( repository_example=example diff --git a/bothub/common/migrations/0041_auto_20191212_1714.py b/bothub/common/migrations/0041_auto_20191212_1714.py deleted file mode 100644 index 464827e4..00000000 --- a/bothub/common/migrations/0041_auto_20191212_1714.py +++ /dev/null @@ -1,248 +0,0 @@ -# Generated by Django 2.1.11 on 2019-12-12 17:14 - -import bothub.common.languages -from django.conf import settings -from django.db import migrations, models -import django.db.models.deletion -import django.utils.timezone - - -class Migration(migrations.Migration): - - dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ("common", "0040_initial"), - ] - - operations = [ - migrations.CreateModel( - name="RepositoryVersion", - fields=[ - ( - "id", - models.AutoField( - auto_created=True, - primary_key=True, - serialize=False, - verbose_name="ID", - ), - ), - ("name", models.CharField(default="master", max_length=40)), - ( - "last_update", - models.DateTimeField(auto_now_add=True, verbose_name="last update"), - ), - ("is_default", models.BooleanField(default=True)), - ( - "created_at", - models.DateTimeField(auto_now_add=True, verbose_name="created at"), - ), - ( - "created_by", - models.ForeignKey( - blank=True, - null=True, - on_delete=django.db.models.deletion.CASCADE, - to=settings.AUTH_USER_MODEL, - ), - ), - ], - options={"verbose_name": "repository version", "ordering": ["-is_default"]}, - ), - migrations.CreateModel( - name="RepositoryVersionLanguage", - fields=[ - ( - "id", - models.AutoField( - auto_created=True, - primary_key=True, - serialize=False, - verbose_name="ID", - ), - ), - ( - "language", - models.CharField( - max_length=5, - validators=[bothub.common.languages.validate_language], - verbose_name="language", - ), - ), - ("bot_data", models.TextField(blank=True, verbose_name="bot data")), - ( - "training_started_at", - models.DateTimeField( - blank=True, null=True, verbose_name="training started at" - ), - ), - ( - "training_end_at", - models.DateTimeField( - blank=True, null=True, verbose_name="trained at" - ), - ), - ( - "failed_at", - models.DateTimeField( - blank=True, null=True, verbose_name="failed at" - ), - ), - ("use_analyze_char", models.BooleanField(default=False)), - ("use_name_entities", models.BooleanField(default=False)), - ("use_competing_intents", models.BooleanField(default=False)), - ( - "algorithm", - models.CharField( - choices=[ - ("statistical_model", "Statistical Model"), - ( - "neural_network_internal", - "Neural Network with internal vocabulary", - ), - ( - "neural_network_external", - "Neural Network with external vocabulary (BETA)", - ), - ], - default="statistical_model", - max_length=24, - verbose_name="algorithm", - ), - ), - ( - "training_log", - models.TextField( - blank=True, editable=False, verbose_name="training log" - ), - ), - ( - "created_at", - models.DateTimeField(auto_now_add=True, verbose_name="created at"), - ), - ( - "last_update", - models.DateTimeField(auto_now_add=True, verbose_name="last update"), - ), - ( - "total_training_end", - models.IntegerField(default=0, verbose_name="total training end"), - ), - ( - "repository_version", - models.ForeignKey( - on_delete=django.db.models.deletion.CASCADE, - to="common.RepositoryVersion", - ), - ), - ], - options={ - "verbose_name": "repository version language", - "verbose_name_plural": "repository version languages", - "ordering": ["-created_at"], - }, - ), - migrations.RemoveField(model_name="repositoryupdate", name="by"), - migrations.RemoveField(model_name="repositoryupdate", name="repository"), - migrations.RemoveField(model_name="repository", name="total_updates"), - migrations.RemoveField( - model_name="repositoryevaluate", name="repository_update" - ), - migrations.RemoveField( - model_name="repositoryevaluateresult", name="repository_update" - ), - migrations.RemoveField( - model_name="repositoryexample", name="repository_update" - ), - migrations.RemoveField( - model_name="repositorytranslatedexample", name="repository_update" - ), - migrations.AddField( - model_name="repositoryexample", - name="last_update", - field=models.DateTimeField( - default=django.utils.timezone.now, verbose_name="last update" - ), - preserve_default=False, - ), - migrations.AlterField( - model_name="repositoryevaluate", - name="deleted_in", - field=models.ForeignKey( - blank=True, - null=True, - on_delete=django.db.models.deletion.CASCADE, - related_name="deleted_evaluate", - to="common.RepositoryVersionLanguage", - ), - ), - migrations.AlterField( - model_name="repositoryexample", - name="deleted_in", - field=models.ForeignKey( - blank=True, - null=True, - on_delete=django.db.models.deletion.CASCADE, - related_name="deleted", - to="common.RepositoryVersionLanguage", - ), - ), - migrations.DeleteModel(name="RepositoryUpdate"), - migrations.AddField( - model_name="repositoryversion", - name="repository", - field=models.ForeignKey( - on_delete=django.db.models.deletion.CASCADE, - related_name="versions", - to="common.Repository", - ), - ), - migrations.AddField( - model_name="repositoryevaluate", - name="repository_version_language", - field=models.ForeignKey( - default=None, - editable=False, - on_delete=django.db.models.deletion.CASCADE, - related_name="added_evaluate", - to="common.RepositoryVersionLanguage", - ), - preserve_default=False, - ), - migrations.AddField( - model_name="repositoryevaluateresult", - name="repository_version_language", - field=models.ForeignKey( - default=None, - editable=False, - on_delete=django.db.models.deletion.CASCADE, - related_name="results", - to="common.RepositoryVersionLanguage", - ), - preserve_default=False, - ), - migrations.AddField( - model_name="repositoryexample", - name="repository_version_language", - field=models.ForeignKey( - default=None, - editable=False, - on_delete=django.db.models.deletion.CASCADE, - related_name="added", - to="common.RepositoryVersionLanguage", - ), - preserve_default=False, - ), - migrations.AddField( - model_name="repositorytranslatedexample", - name="repository_version_language", - field=models.ForeignKey( - default=None, - editable=False, - on_delete=django.db.models.deletion.CASCADE, - related_name="translated_added", - to="common.RepositoryVersionLanguage", - ), - preserve_default=False, - ), - ] diff --git a/bothub/common/migrations/0041_delete_examples_isdeleted.py b/bothub/common/migrations/0041_delete_examples_isdeleted.py new file mode 100644 index 00000000..657bd131 --- /dev/null +++ b/bothub/common/migrations/0041_delete_examples_isdeleted.py @@ -0,0 +1,19 @@ +from django.db import migrations + + +def noop(apps, schema_editor): # pragma: no cover + pass + + +def delete_examples_already_deleted(apps, schema_editor): # pragma: no cover + RepositoryExample = apps.get_model("common", "RepositoryExample") + RepositoryExample.objects.filter(deleted_in__isnull=False).delete() + + +class Migration(migrations.Migration): + + dependencies = [ + ('common', '0040_initial'), + ] + + operations = [migrations.RunPython(delete_examples_already_deleted, noop)] diff --git a/bothub/common/migrations/0042_auto_20191212_1922.py b/bothub/common/migrations/0042_auto_20191212_1922.py deleted file mode 100644 index c8ccf158..00000000 --- a/bothub/common/migrations/0042_auto_20191212_1922.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.1.11 on 2019-12-12 19:22 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('common', '0041_auto_20191212_1714'), - ] - - operations = [ - migrations.AlterField( - model_name='repositoryversionlanguage', - name='last_update', - field=models.DateTimeField(verbose_name='last update'), - ), - ] diff --git a/bothub/common/migrations/0042_auto_20191212_2013.py b/bothub/common/migrations/0042_auto_20191212_2013.py new file mode 100644 index 00000000..7486cfdc --- /dev/null +++ b/bothub/common/migrations/0042_auto_20191212_2013.py @@ -0,0 +1,133 @@ +# Generated by Django 2.1.11 on 2019-12-12 20:13 + +import bothub.common.languages +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('common', '0041_delete_examples_isdeleted'), + ] + + operations = [ + migrations.CreateModel( + name='RepositoryVersion', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(default='master', max_length=40)), + ('last_update', models.DateTimeField(auto_now_add=True, verbose_name='last update')), + ('is_default', models.BooleanField(default=True)), + ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')), + ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + options={ + 'verbose_name': 'repository version', + 'ordering': ['-is_default'], + }, + ), + migrations.CreateModel( + name='RepositoryVersionLanguage', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('language', models.CharField(max_length=5, validators=[bothub.common.languages.validate_language], verbose_name='language')), + ('bot_data', models.TextField(blank=True, verbose_name='bot data')), + ('training_started_at', models.DateTimeField(blank=True, null=True, verbose_name='training started at')), + ('training_end_at', models.DateTimeField(blank=True, null=True, verbose_name='trained at')), + ('failed_at', models.DateTimeField(blank=True, null=True, verbose_name='failed at')), + ('use_analyze_char', models.BooleanField(default=False)), + ('use_name_entities', models.BooleanField(default=False)), + ('use_competing_intents', models.BooleanField(default=False)), + ('algorithm', models.CharField(choices=[('statistical_model', 'Statistical Model'), ('neural_network_internal', 'Neural Network with internal vocabulary'), ('neural_network_external', 'Neural Network with external vocabulary (BETA)')], default='statistical_model', max_length=24, verbose_name='algorithm')), + ('training_log', models.TextField(blank=True, editable=False, verbose_name='training log')), + ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')), + ('last_update', models.DateTimeField(null=True, verbose_name='last update')), + ('total_training_end', models.IntegerField(default=0, verbose_name='total training end')), + ('repository_version', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='common.RepositoryVersion')), + ], + options={ + 'verbose_name': 'repository version language', + 'verbose_name_plural': 'repository version languages', + 'ordering': ['-created_at'], + }, + ), + migrations.RemoveField( + model_name='repositoryupdate', + name='by', + ), + migrations.RemoveField( + model_name='repositoryupdate', + name='repository', + ), + migrations.RemoveField( + model_name='repository', + name='total_updates', + ), + migrations.RemoveField( + model_name='repositoryevaluate', + name='repository_update', + ), + migrations.RemoveField( + model_name='repositoryevaluateresult', + name='repository_update', + ), + migrations.RemoveField( + model_name='repositoryexample', + name='deleted_in', + ), + migrations.RemoveField( + model_name='repositoryexample', + name='repository_update', + ), + migrations.RemoveField( + model_name='repositorytranslatedexample', + name='repository_update', + ), + migrations.AddField( + model_name='repositoryexample', + name='last_update', + field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='last update'), + preserve_default=False, + ), + migrations.AlterField( + model_name='repositoryevaluate', + name='deleted_in', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='deleted_evaluate', to='common.RepositoryVersionLanguage'), + ), + migrations.DeleteModel( + name='RepositoryUpdate', + ), + migrations.AddField( + model_name='repositoryversion', + name='repository', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='versions', to='common.Repository'), + ), + migrations.AddField( + model_name='repositoryevaluate', + name='repository_version_language', + field=models.ForeignKey(default=None, editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='added_evaluate', to='common.RepositoryVersionLanguage'), + preserve_default=False, + ), + migrations.AddField( + model_name='repositoryevaluateresult', + name='repository_version_language', + field=models.ForeignKey(default=None, editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='results', to='common.RepositoryVersionLanguage'), + preserve_default=False, + ), + migrations.AddField( + model_name='repositoryexample', + name='repository_version_language', + field=models.ForeignKey(default=None, editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='added', to='common.RepositoryVersionLanguage'), + preserve_default=False, + ), + migrations.AddField( + model_name='repositorytranslatedexample', + name='repository_version_language', + field=models.ForeignKey(default=None, editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='translated_added', to='common.RepositoryVersionLanguage'), + preserve_default=False, + ), + ] diff --git a/bothub/common/migrations/0043_auto_20191212_1923.py b/bothub/common/migrations/0043_auto_20191212_1923.py deleted file mode 100644 index 6b3a9547..00000000 --- a/bothub/common/migrations/0043_auto_20191212_1923.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.1.11 on 2019-12-12 19:23 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('common', '0042_auto_20191212_1922'), - ] - - operations = [ - migrations.AlterField( - model_name='repositoryversionlanguage', - name='last_update', - field=models.DateTimeField(null=True, verbose_name='last update'), - ), - ] diff --git a/bothub/common/models.py b/bothub/common/models.py index 76d8984f..5b3fa78d 100644 --- a/bothub/common/models.py +++ b/bothub/common/models.py @@ -63,7 +63,7 @@ def order_by_relevance(self): ) def supported_language(self, language): - valid_examples = RepositoryExample.objects.filter(deleted_in__isnull=True) + valid_examples = RepositoryExample.objects.all() valid_updates = RepositoryVersionLanguage.objects.filter( added__in=valid_examples ) @@ -402,8 +402,6 @@ def examples( ) if language: query = query.filter(repository_version_language__language=language) - if exclude_deleted: - return query.exclude(deleted_in__isnull=False) return query def evaluations( @@ -420,8 +418,6 @@ def evaluations( ) if language: query = query.filter(repository_version_language__language=language) - if exclude_deleted: - return query.exclude(deleted_in__isnull=False) return query # pragma: no cover def evaluations_results(self, queryset=None, version_default=True): @@ -587,12 +583,7 @@ def examples(self): t_started_at = self.training_started_at examples = examples.exclude( models.Q(last_update__lte=self.training_end_at) - | models.Q(deleted_in=self) - | models.Q(deleted_in__training_started_at__lt=t_started_at) ) - - else: - examples = examples.exclude(deleted_in__isnull=False) return examples.distinct() @property @@ -697,7 +688,6 @@ def ready_for_train(self): if ( not self.added.exists() and not self.translated_added.exists() - and not self.deleted.exists() ): return False @@ -784,13 +774,6 @@ class Meta: repository_version_language = models.ForeignKey( RepositoryVersionLanguage, models.CASCADE, related_name="added", editable=False ) - deleted_in = models.ForeignKey( - RepositoryVersionLanguage, - models.CASCADE, - related_name="deleted", - blank=True, - null=True, - ) text = models.TextField(_("text"), help_text=_("Example text")) intent = models.CharField( _("intent"), @@ -835,17 +818,6 @@ def get_entities(self, language): # pragma: no cover return self.entities.all() return self.get_translation(language).entities.all() - # def delete(self): - # print('entrou aqui') - # print(self.repository_version_language.repository_version.repository.current_version( - # self.repository_version_language.language - # )) - # self.deleted_in = self.repository_version_language.repository_version.repository.current_version( - # self.repository_version_language.language - # ) - # print(self.deleted_in) - # self.save(update_fields=["deleted_in"]) - class RepositoryTranslatedExampleManager(models.Manager): def create( diff --git a/bothub/common/tests.py b/bothub/common/tests.py index 74574576..f2b522e6 100644 --- a/bothub/common/tests.py +++ b/bothub/common/tests.py @@ -295,10 +295,6 @@ def test_language(self): ) self.assertEqual(self.example.language, self.language) - def test_delete(self): - self.example.delete() - self.assertEqual(self.example.deleted_in, self.repository.current_version()) - class RepositoryAuthorizationTestCase(TestCase): def setUp(self): @@ -483,7 +479,6 @@ def setUp(self): intent="greet", ) example.delete() - print(example.deleted_in) self.update = self.repository.current_version() self.update.start_training(self.owner) @@ -500,65 +495,10 @@ def test_okay(self): RepositoryExample.objects.create( repository_version_language=new_update_2, text="good morning", intent="greet" ) - - print(self.update.examples) - - for t in self.update.examples: - print(t.deleted_in) - - self.assertEqual(self.update.examples.count(), 1) - self.assertEqual(new_update_1.examples.count(), 2) + self.assertEqual(self.update.examples.count(), 2) + self.assertEqual(new_update_1.examples.count(), 3) self.assertEqual(new_update_2.examples.count(), 3) - # def test_examples_deleted_consistency(self): - # new_update_1 = self.repository.current_version() - # RepositoryExample.objects.create( - # repository_update=new_update_1, text="hello", intent="greet" - # ) - # RepositoryExample.objects.create( - # repository_update=new_update_1, text="hello d1", intent="greet" - # ).delete() - # examples_1_count = new_update_1.examples.count() - # new_update_1.start_training(self.owner) - # - # new_update_2 = self.repository.current_version() - # RepositoryExample.objects.create( - # repository_update=new_update_2, text="hellow", intent="greet" - # ) - # examples_2_count = new_update_2.examples.count() - # new_update_2.start_training(self.owner) - # - # new_update_3 = self.repository.current_version() - # RepositoryExample.objects.create( - # repository_update=new_update_3, text="hellow", intent="greet" - # ) - # RepositoryExample.objects.create( - # repository_update=new_update_3, text="hello d2", intent="greet" - # ).delete() - # RepositoryExample.objects.create( - # repository_update=new_update_3, text="hello d3", intent="greet" - # ).delete() - # RepositoryExample.objects.create( - # repository_update=new_update_3, text="hello d4", intent="greet" - # ).delete() - # examples_3_count = new_update_3.examples.count() - # new_update_3.start_training(self.owner) - # - # new_update_4 = self.repository.current_version() - # RepositoryExample.objects.create( - # repository_update=new_update_4, text="hellow", intent="greet" - # ) - # examples_4_count = new_update_4.examples.count() - # new_update_4.start_training(self.owner) - # - # self.assertEqual(examples_1_count, new_update_1.examples.count()) - # - # self.assertEqual(examples_2_count, new_update_2.examples.count()) - # - # self.assertEqual(examples_3_count, new_update_3.examples.count()) - # - # self.assertEqual(examples_4_count, new_update_4.examples.count()) - class RepositoryReadyForTrain(TestCase): def setUp(self): From 567b0f07c29fec566f41453ea07fedff833bacc3 Mon Sep 17 00:00:00 2001 From: Daniel Yohan Date: Thu, 12 Dec 2019 17:34:48 -0300 Subject: [PATCH 3/4] Update last update after delete example --- bothub/common/models.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bothub/common/models.py b/bothub/common/models.py index 5b3fa78d..8189f6fd 100644 --- a/bothub/common/models.py +++ b/bothub/common/models.py @@ -789,8 +789,6 @@ def save(self, *args, **kwargs): self.last_update = timezone.now() self.repository_version_language.last_update = timezone.now() self.repository_version_language.save(update_fields=["last_update"]) - print(args) - print(kwargs) super(RepositoryExample, self).save(*args, **kwargs) @property @@ -818,6 +816,11 @@ def get_entities(self, language): # pragma: no cover return self.entities.all() return self.get_translation(language).entities.all() + def delete(self, using=None, keep_parents=False): + self.repository_version_language.last_update = timezone.now() + self.repository_version_language.save(update_fields=['last_update']) + return super().delete(using, keep_parents) + class RepositoryTranslatedExampleManager(models.Manager): def create( From 275fc3d7f0943eefc8b6487a9d747f378ae8ebf5 Mon Sep 17 00:00:00 2001 From: Daniel Yohan Date: Thu, 12 Dec 2019 17:37:53 -0300 Subject: [PATCH 4/4] [fix] PEP8 --- bothub/api/v2/tests/test_repository.py | 4 +- .../0041_delete_examples_isdeleted.py | 4 +- .../migrations/0042_auto_20191212_2013.py | 265 ++++++++++++------ bothub/common/models.py | 30 +- bothub/common/tests.py | 4 +- 5 files changed, 203 insertions(+), 104 deletions(-) diff --git a/bothub/api/v2/tests/test_repository.py b/bothub/api/v2/tests/test_repository.py index b7d0441f..b70d95da 100644 --- a/bothub/api/v2/tests/test_repository.py +++ b/bothub/api/v2/tests/test_repository.py @@ -1829,9 +1829,7 @@ def test_okay(self): self.assertEqual(content_data.get("count"), 1) def test_not_authenticated(self): - response, content_data = self.request( - {"repository": str(self.repository.uuid)} - ) + response, content_data = self.request({"repository": str(self.repository.uuid)}) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) def test_without_repository(self): diff --git a/bothub/common/migrations/0041_delete_examples_isdeleted.py b/bothub/common/migrations/0041_delete_examples_isdeleted.py index 657bd131..8637fcf3 100644 --- a/bothub/common/migrations/0041_delete_examples_isdeleted.py +++ b/bothub/common/migrations/0041_delete_examples_isdeleted.py @@ -12,8 +12,6 @@ def delete_examples_already_deleted(apps, schema_editor): # pragma: no cover class Migration(migrations.Migration): - dependencies = [ - ('common', '0040_initial'), - ] + dependencies = [("common", "0040_initial")] operations = [migrations.RunPython(delete_examples_already_deleted, noop)] diff --git a/bothub/common/migrations/0042_auto_20191212_2013.py b/bothub/common/migrations/0042_auto_20191212_2013.py index 7486cfdc..1339f10d 100644 --- a/bothub/common/migrations/0042_auto_20191212_2013.py +++ b/bothub/common/migrations/0042_auto_20191212_2013.py @@ -11,123 +11,228 @@ class Migration(migrations.Migration): dependencies = [ migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ('common', '0041_delete_examples_isdeleted'), + ("common", "0041_delete_examples_isdeleted"), ] operations = [ migrations.CreateModel( - name='RepositoryVersion', + name="RepositoryVersion", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(default='master', max_length=40)), - ('last_update', models.DateTimeField(auto_now_add=True, verbose_name='last update')), - ('is_default', models.BooleanField(default=True)), - ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')), - ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(default="master", max_length=40)), + ( + "last_update", + models.DateTimeField(auto_now_add=True, verbose_name="last update"), + ), + ("is_default", models.BooleanField(default=True)), + ( + "created_at", + models.DateTimeField(auto_now_add=True, verbose_name="created at"), + ), + ( + "created_by", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), ], - options={ - 'verbose_name': 'repository version', - 'ordering': ['-is_default'], - }, + options={"verbose_name": "repository version", "ordering": ["-is_default"]}, ), migrations.CreateModel( - name='RepositoryVersionLanguage', + name="RepositoryVersionLanguage", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('language', models.CharField(max_length=5, validators=[bothub.common.languages.validate_language], verbose_name='language')), - ('bot_data', models.TextField(blank=True, verbose_name='bot data')), - ('training_started_at', models.DateTimeField(blank=True, null=True, verbose_name='training started at')), - ('training_end_at', models.DateTimeField(blank=True, null=True, verbose_name='trained at')), - ('failed_at', models.DateTimeField(blank=True, null=True, verbose_name='failed at')), - ('use_analyze_char', models.BooleanField(default=False)), - ('use_name_entities', models.BooleanField(default=False)), - ('use_competing_intents', models.BooleanField(default=False)), - ('algorithm', models.CharField(choices=[('statistical_model', 'Statistical Model'), ('neural_network_internal', 'Neural Network with internal vocabulary'), ('neural_network_external', 'Neural Network with external vocabulary (BETA)')], default='statistical_model', max_length=24, verbose_name='algorithm')), - ('training_log', models.TextField(blank=True, editable=False, verbose_name='training log')), - ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')), - ('last_update', models.DateTimeField(null=True, verbose_name='last update')), - ('total_training_end', models.IntegerField(default=0, verbose_name='total training end')), - ('repository_version', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='common.RepositoryVersion')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "language", + models.CharField( + max_length=5, + validators=[bothub.common.languages.validate_language], + verbose_name="language", + ), + ), + ("bot_data", models.TextField(blank=True, verbose_name="bot data")), + ( + "training_started_at", + models.DateTimeField( + blank=True, null=True, verbose_name="training started at" + ), + ), + ( + "training_end_at", + models.DateTimeField( + blank=True, null=True, verbose_name="trained at" + ), + ), + ( + "failed_at", + models.DateTimeField( + blank=True, null=True, verbose_name="failed at" + ), + ), + ("use_analyze_char", models.BooleanField(default=False)), + ("use_name_entities", models.BooleanField(default=False)), + ("use_competing_intents", models.BooleanField(default=False)), + ( + "algorithm", + models.CharField( + choices=[ + ("statistical_model", "Statistical Model"), + ( + "neural_network_internal", + "Neural Network with internal vocabulary", + ), + ( + "neural_network_external", + "Neural Network with external vocabulary (BETA)", + ), + ], + default="statistical_model", + max_length=24, + verbose_name="algorithm", + ), + ), + ( + "training_log", + models.TextField( + blank=True, editable=False, verbose_name="training log" + ), + ), + ( + "created_at", + models.DateTimeField(auto_now_add=True, verbose_name="created at"), + ), + ( + "last_update", + models.DateTimeField(null=True, verbose_name="last update"), + ), + ( + "total_training_end", + models.IntegerField(default=0, verbose_name="total training end"), + ), + ( + "repository_version", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="common.RepositoryVersion", + ), + ), ], options={ - 'verbose_name': 'repository version language', - 'verbose_name_plural': 'repository version languages', - 'ordering': ['-created_at'], + "verbose_name": "repository version language", + "verbose_name_plural": "repository version languages", + "ordering": ["-created_at"], }, ), + migrations.RemoveField(model_name="repositoryupdate", name="by"), + migrations.RemoveField(model_name="repositoryupdate", name="repository"), + migrations.RemoveField(model_name="repository", name="total_updates"), migrations.RemoveField( - model_name='repositoryupdate', - name='by', - ), - migrations.RemoveField( - model_name='repositoryupdate', - name='repository', - ), - migrations.RemoveField( - model_name='repository', - name='total_updates', + model_name="repositoryevaluate", name="repository_update" ), migrations.RemoveField( - model_name='repositoryevaluate', - name='repository_update', + model_name="repositoryevaluateresult", name="repository_update" ), + migrations.RemoveField(model_name="repositoryexample", name="deleted_in"), migrations.RemoveField( - model_name='repositoryevaluateresult', - name='repository_update', + model_name="repositoryexample", name="repository_update" ), migrations.RemoveField( - model_name='repositoryexample', - name='deleted_in', - ), - migrations.RemoveField( - model_name='repositoryexample', - name='repository_update', - ), - migrations.RemoveField( - model_name='repositorytranslatedexample', - name='repository_update', + model_name="repositorytranslatedexample", name="repository_update" ), migrations.AddField( - model_name='repositoryexample', - name='last_update', - field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='last update'), + model_name="repositoryexample", + name="last_update", + field=models.DateTimeField( + default=django.utils.timezone.now, verbose_name="last update" + ), preserve_default=False, ), migrations.AlterField( - model_name='repositoryevaluate', - name='deleted_in', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='deleted_evaluate', to='common.RepositoryVersionLanguage'), - ), - migrations.DeleteModel( - name='RepositoryUpdate', - ), + model_name="repositoryevaluate", + name="deleted_in", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="deleted_evaluate", + to="common.RepositoryVersionLanguage", + ), + ), + migrations.DeleteModel(name="RepositoryUpdate"), migrations.AddField( - model_name='repositoryversion', - name='repository', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='versions', to='common.Repository'), + model_name="repositoryversion", + name="repository", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="versions", + to="common.Repository", + ), ), migrations.AddField( - model_name='repositoryevaluate', - name='repository_version_language', - field=models.ForeignKey(default=None, editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='added_evaluate', to='common.RepositoryVersionLanguage'), + model_name="repositoryevaluate", + name="repository_version_language", + field=models.ForeignKey( + default=None, + editable=False, + on_delete=django.db.models.deletion.CASCADE, + related_name="added_evaluate", + to="common.RepositoryVersionLanguage", + ), preserve_default=False, ), migrations.AddField( - model_name='repositoryevaluateresult', - name='repository_version_language', - field=models.ForeignKey(default=None, editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='results', to='common.RepositoryVersionLanguage'), + model_name="repositoryevaluateresult", + name="repository_version_language", + field=models.ForeignKey( + default=None, + editable=False, + on_delete=django.db.models.deletion.CASCADE, + related_name="results", + to="common.RepositoryVersionLanguage", + ), preserve_default=False, ), migrations.AddField( - model_name='repositoryexample', - name='repository_version_language', - field=models.ForeignKey(default=None, editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='added', to='common.RepositoryVersionLanguage'), + model_name="repositoryexample", + name="repository_version_language", + field=models.ForeignKey( + default=None, + editable=False, + on_delete=django.db.models.deletion.CASCADE, + related_name="added", + to="common.RepositoryVersionLanguage", + ), preserve_default=False, ), migrations.AddField( - model_name='repositorytranslatedexample', - name='repository_version_language', - field=models.ForeignKey(default=None, editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='translated_added', to='common.RepositoryVersionLanguage'), + model_name="repositorytranslatedexample", + name="repository_version_language", + field=models.ForeignKey( + default=None, + editable=False, + on_delete=django.db.models.deletion.CASCADE, + related_name="translated_added", + to="common.RepositoryVersionLanguage", + ), preserve_default=False, ), ] diff --git a/bothub/common/models.py b/bothub/common/models.py index 8189f6fd..c18669d7 100644 --- a/bothub/common/models.py +++ b/bothub/common/models.py @@ -558,15 +558,10 @@ class Meta: choices=Repository.ALGORITHM_CHOICES, default=Repository.ALGORITHM_STATISTICAL_MODEL, ) - repository_version = models.ForeignKey( - RepositoryVersion, models.CASCADE - ) + repository_version = models.ForeignKey(RepositoryVersion, models.CASCADE) training_log = models.TextField(_("training log"), blank=True, editable=False) created_at = models.DateTimeField(_("created at"), auto_now_add=True) - last_update = models.DateTimeField( - _("last update"), - null=True - ) + last_update = models.DateTimeField(_("last update"), null=True) total_training_end = models.IntegerField( _("total training end"), default=0, blank=False, null=False ) @@ -580,10 +575,7 @@ def examples(self): | models.Q(translations__language=self.language) ) if self.training_end_at and (self.training_end_at >= self.last_update): - t_started_at = self.training_started_at - examples = examples.exclude( - models.Q(last_update__lte=self.training_end_at) - ) + examples = examples.exclude(models.Q(last_update__lte=self.training_end_at)) return examples.distinct() @property @@ -685,10 +677,7 @@ def ready_for_train(self): if previous_update.failed_at: return True - if ( - not self.added.exists() - and not self.translated_added.exists() - ): + if not self.added.exists() and not self.translated_added.exists(): return False if self.examples.count() == 0: @@ -755,7 +744,14 @@ def save_training(self, bot_data): self.last_update = last_time self.bot_data = bot_data self.total_training_end += 1 - self.save(update_fields=["total_training_end", "training_end_at", "bot_data", "last_update"]) + self.save( + update_fields=[ + "total_training_end", + "training_end_at", + "bot_data", + "last_update", + ] + ) def get_bot_data(self): return self.bot_data @@ -818,7 +814,7 @@ def get_entities(self, language): # pragma: no cover def delete(self, using=None, keep_parents=False): self.repository_version_language.last_update = timezone.now() - self.repository_version_language.save(update_fields=['last_update']) + self.repository_version_language.save(update_fields=["last_update"]) return super().delete(using, keep_parents) diff --git a/bothub/common/tests.py b/bothub/common/tests.py index f2b522e6..19899234 100644 --- a/bothub/common/tests.py +++ b/bothub/common/tests.py @@ -493,7 +493,9 @@ def test_okay(self): new_update_2 = self.repository.current_version() RepositoryExample.objects.create( - repository_version_language=new_update_2, text="good morning", intent="greet" + repository_version_language=new_update_2, + text="good morning", + intent="greet", ) self.assertEqual(self.update.examples.count(), 2) self.assertEqual(new_update_1.examples.count(), 3)