data cleaning commands

pull/94/head
eric 2020-08-06 17:18:34 -04:00
parent 0cb15ca999
commit 6507c392f7
2 changed files with 39 additions and 0 deletions

View File

@ -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()

View File

@ -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()