diff --git a/bothub/common/migrations/0032_repository_total_updates.py b/bothub/common/migrations/0032_repository_total_updates.py new file mode 100644 index 00000000..77b7f9ab --- /dev/null +++ b/bothub/common/migrations/0032_repository_total_updates.py @@ -0,0 +1,31 @@ +# Generated by Django 2.1.5 on 2019-08-14 19:45 + +from django.db import migrations, models + +from bothub.common.models import RepositoryUpdate +from bothub.common.models import Repository + + +def updateRepository(apps, schema_editor): + for update in RepositoryUpdate.objects.all().filter( + trained_at__isnull=False + ): + repository = Repository.objects.get(uuid=update.repository.uuid) + repository.total_updates += 1 + repository.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('common', '0031_auto_20190502_1732'), + ] + + operations = [ + migrations.AddField( + model_name='repository', + name='total_updates', + field=models.IntegerField(default=0, verbose_name='total updates'), + ), + migrations.RunPython(updateRepository), + ] diff --git a/bothub/common/migrations/0032_auto_20190702_1419.py b/bothub/common/migrations/0033_auto_20190702_1419.py similarity index 91% rename from bothub/common/migrations/0032_auto_20190702_1419.py rename to bothub/common/migrations/0033_auto_20190702_1419.py index a1d0a178..8d0b1a20 100644 --- a/bothub/common/migrations/0032_auto_20190702_1419.py +++ b/bothub/common/migrations/0033_auto_20190702_1419.py @@ -7,7 +7,7 @@ class Migration(migrations.Migration): dependencies = [ - ('common', '0031_auto_20190502_1732'), + ('common', '0032_repository_total_updates'), ] operations = [ diff --git a/bothub/common/migrations/0033_auto_20190702_1443.py b/bothub/common/migrations/0034_auto_20190702_1443.py similarity index 88% rename from bothub/common/migrations/0033_auto_20190702_1443.py rename to bothub/common/migrations/0034_auto_20190702_1443.py index 1d969c29..94f28b53 100644 --- a/bothub/common/migrations/0033_auto_20190702_1443.py +++ b/bothub/common/migrations/0034_auto_20190702_1443.py @@ -6,7 +6,7 @@ class Migration(migrations.Migration): dependencies = [ - ('common', '0032_auto_20190702_1419'), + ('common', '0033_auto_20190702_1419'), ] operations = [ diff --git a/bothub/common/models.py b/bothub/common/models.py index 70f60ffa..176a5675 100644 --- a/bothub/common/models.py +++ b/bothub/common/models.py @@ -62,7 +62,7 @@ def publics(self): def order_by_relevance(self): return self \ .annotate(examples_sum=models.Sum('updates__added')) \ - .order_by('-examples_sum', '-created_at') + .order_by('-total_updates', '-examples_sum', '-created_at') def supported_language(self, language): valid_examples = RepositoryExample.objects.filter( @@ -176,6 +176,13 @@ class Meta: _('created at'), auto_now_add=True) + total_updates = models.IntegerField( + _('total updates'), + default=0, + blank=False, + null=False + ) + objects = RepositoryManager() nlp_train_url = '{}train/'.format(settings.BOTHUB_NLP_BASE_URL) @@ -642,6 +649,8 @@ def save_training(self, bot_data): self.trained_at = timezone.now() self.bot_data = base64.b64encode(bot_data).decode('utf8') + self.repository.total_updates += 1 + self.repository.save() self.save( update_fields=[ 'trained_at', diff --git a/bothub/common/tests.py b/bothub/common/tests.py index b88521df..02d9e2a4 100644 --- a/bothub/common/tests.py +++ b/bothub/common/tests.py @@ -290,6 +290,7 @@ def test_last_trained_update(self): self.assertEqual( update_1, self.repository.last_trained_update()) + self.assertEqual(self.repository.total_updates, 1) def test_available_languages(self): available_languages = self.repository.available_languages