Skip to content

Commit

Permalink
[fix] Migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
dyohan9 committed Aug 16, 2019
1 parent 157a767 commit aff0f1c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
31 changes: 31 additions & 0 deletions bothub/common/migrations/0032_repository_total_updates.py
Original file line number Diff line number Diff line change
@@ -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),
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Migration(migrations.Migration):

dependencies = [
('common', '0031_auto_20190502_1732'),
('common', '0032_repository_total_updates'),
]

operations = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class Migration(migrations.Migration):

dependencies = [
('common', '0032_auto_20190702_1419'),
('common', '0033_auto_20190702_1419'),
]

operations = [
Expand Down
11 changes: 10 additions & 1 deletion bothub/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions bothub/common/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aff0f1c

Please sign in to comment.