db cleaning

pull/94/head
eric 2019-03-27 21:22:56 -04:00
parent e563da9655
commit c142533898
2 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,20 @@
from django.core.management.base import BaseCommand
from regluit.core.models import Work
from regluit.utils.lang import lang_to_language_code, lang_and_locale, iso639
iso639 = r'^[a-z][a-z][a-z]?$'
lang_and_locale = r'^[a-z][a-z]\-[A-Z][A-Z]$'
class Command(BaseCommand):
'''remove works and editions without titles'''
help = "remove works and editions without titles"
def handle(self, **options):
badworks = Work.objects.exclude(language__regex=iso639)
badworks = badworks.exclude(language__regex=lang_and_locale)
self.stdout.write('{} works to fix'.format(badworks.count()))
for work in badworks:
language = lang_to_language_code(work.language)
work.language = language if language else 'xx'
work.save()

View File

@ -0,0 +1,18 @@
from django.core.management.base import BaseCommand
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):
badworks = Work.objects.filter(title='')
for work in badworks:
work.selected_edition = None
for edition in work.editions.all():
edition.delete()
work.delete()