From c142533898b3f8e2763d2ec5d4d7a3ceee91b4fa Mon Sep 17 00:00:00 2001 From: eric Date: Wed, 27 Mar 2019 21:22:56 -0400 Subject: [PATCH] db cleaning --- core/management/commands/clean_languages.py | 20 +++++++++++++++++++ .../management/commands/delete_empty_works.py | 18 +++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 core/management/commands/clean_languages.py create mode 100644 core/management/commands/delete_empty_works.py diff --git a/core/management/commands/clean_languages.py b/core/management/commands/clean_languages.py new file mode 100644 index 00000000..c9838383 --- /dev/null +++ b/core/management/commands/clean_languages.py @@ -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() diff --git a/core/management/commands/delete_empty_works.py b/core/management/commands/delete_empty_works.py new file mode 100644 index 00000000..4a7cf387 --- /dev/null +++ b/core/management/commands/delete_empty_works.py @@ -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()