diff --git a/core/management/commands/clean_dangling_works.py b/core/management/commands/clean_dangling_works.py new file mode 100644 index 00000000..a7d9a3b4 --- /dev/null +++ b/core/management/commands/clean_dangling_works.py @@ -0,0 +1,22 @@ +from django.core.management.base import BaseCommand +from django.db.models import Count + +from regluit.core.models import Work, WasWork +from regluit.core.bookloader import merge_works + + + +class Command(BaseCommand): + '''remove works and editions without titles''' + help = "remove works and editions without titles" + + def handle(self, **options): + orphans = Work.objects.annotate(num_editions=Count('editions')).filter(num_editions=0) + for work in orphans: + self.stdout.write('cleaning %s' % work.title) + parent = None + for parent in WasWork.objects.filter(was=work.id): + # remerge into parent + merge_works(parent.work, work) + if not parent: + work.delete() diff --git a/core/management/commands/refresh_free.py b/core/management/commands/refresh_free.py new file mode 100644 index 00000000..35b8894b --- /dev/null +++ b/core/management/commands/refresh_free.py @@ -0,0 +1,17 @@ +from django.core.management.base import BaseCommand +from django.db.models import Sum + +from regluit.core.models import Work + + + +class Command(BaseCommand): + '''remove works and editions without titles''' + help = "remove works and editions without titles" + + def handle(self, **options): + qs = Work.objects.annotate(num_free=Sum('editions__ebooks__active')).filter(num_free__gt=0) + for free in qs.filter(is_free=False): + self.stdout.write('freeing %s' % free.title) + free.is_free = True + free.save()