Skip to content

Commit

Permalink
feat: Management commands to clean tags and nan values
Browse files Browse the repository at this point in the history
  • Loading branch information
hepplerj committed Jun 18, 2024
1 parent 83756db commit 2b51beb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
19 changes: 19 additions & 0 deletions denig/management/commands/cleannan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.core.management.base import BaseCommand
from django.db.models import Q

from denig.models import Document, Fragment


class Command(BaseCommand):
help = "Clean up nan values from the database."

def handle(self, *args, **kwargs):
fields_to_clean = ["transcription", "notes"]

for field in fields_to_clean:
nan_filter = Q(**{f"{field}__iexact": "nan"})
records = Fragment.objects.filter(nan_filter)

for record in records:
setattr(record, field, "")
record.save()
29 changes: 29 additions & 0 deletions denig/management/commands/fixtags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.core.management.base import BaseCommand

from denig.models import Document


class Command(BaseCommand):
help = "Split concatenated tags into separate tags"

def handle(self, *args, **kwargs):
objects = Document.objects.all()

for obj in objects:
tags = obj.tags.names()
updated = False

for tag in tags:
if " ; " in tag:
split_tags = tag.split(" ; ")
obj.tags.remove(tag)

for split_tag in split_tags:
obj.tags.add(split_tag.strip())

updated = True

if updated:
obj.save()

self.stdout.write(self.style.SUCCESS("Successfully split concatenated tags"))

0 comments on commit 2b51beb

Please sign in to comment.