Skip to content

Commit

Permalink
db: Move GetterRun not/in_use() methods to the model manager
Browse files Browse the repository at this point in the history
  • Loading branch information
R2ZER0 committed Oct 26, 2023
1 parent 81e3fc8 commit afedffa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion datastore/db/management/commands/delete_datagetter_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def add_arguments(self, parser):
def handle(self, *args, **options):
if options.get("all-not-in-use"):
options["getter_run_ids"] = set(options["getter_run_ids"]).union(
[gr.pk for gr in GetterRun.all_not_in_use()]
[gr.pk for gr in GetterRun.objects.not_in_use()]
)

if options.get("older_than_days"):
Expand Down
24 changes: 13 additions & 11 deletions datastore/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,19 @@ def __str__(self):
return self.series


class GetterRunManager(models.Manager):
def in_use(self):
"""Return the QuerySet of all GetterRuns in-use by any Latest best."""
return self.filter(sourcefile__latest__isnull=False).distinct()

def not_in_use(self):
"""Return the QuerySet of all GetterRuns NOT in-use by any Latest best. Inverse of in_use()."""
return self.exclude(sourcefile__latest__isnull=False).distinct()


class GetterRun(models.Model):
objects = GetterRunManager()

datetime = models.DateTimeField(default=timezone.now)
archived = models.BooleanField(default=False)

Expand All @@ -133,17 +145,7 @@ def __str__(self):

def is_in_use(self):
"""Check if this GetterRun is included in any Latest best."""
return GetterRun.all_in_use().filter(pk=self.pk).exists()

@classmethod
def all_in_use(cls):
"""Return the QuerySet of all GetterRuns in-use by any Latest best."""
return cls.objects.filter(sourcefile__latest__isnull=False).distinct()

@classmethod
def all_not_in_use(cls):
"""Return the QuerySet of all GetterRuns NOT in-use by any Latest best. Inverse of all_in_use()."""
return cls.objects.exclude(sourcefile__latest__isnull=False).distinct()
return GetterRun.objects.in_use().filter(pk=self.pk).exists()


class SourceFile(models.Model):
Expand Down
4 changes: 2 additions & 2 deletions datastore/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_delete_datagetter_data(self):
self.assertEqual(db.GetterRun.objects.count(), 0)

def test_doesnt_delete_in_use_datagetter_data_oldest(self):
in_use_pks_before = set(gr.pk for gr in db.GetterRun.all_in_use())
in_use_pks_before = set(gr.pk for gr in db.GetterRun.objects.in_use())
err_out = StringIO()
call_command(
"delete_datagetter_data",
Expand All @@ -67,7 +67,7 @@ def test_doesnt_delete_in_use_datagetter_data_oldest(self):
)

def test_doesnt_delete_in_use_datagetter_data_all(self):
in_use_pks_before = set(gr.pk for gr in db.GetterRun.all_in_use())
in_use_pks_before = set(gr.pk for gr in db.GetterRun.objects.in_use())
err_out = StringIO()
call_command(
"delete_datagetter_data",
Expand Down
4 changes: 2 additions & 2 deletions datastore/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class GetterRunTest(TransactionTestCase):

def test_in_use(self):
total_count = db.GetterRun.objects.all().count()
in_use_count = db.GetterRun.all_in_use().count()
not_in_use_count = db.GetterRun.all_not_in_use().count()
in_use_count = db.GetterRun.objects.in_use().count()
not_in_use_count = db.GetterRun.objects.not_in_use().count()

self.assertLessEqual(in_use_count, total_count)
self.assertLess(
Expand Down

0 comments on commit afedffa

Please sign in to comment.