-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Management commands to clean tags and nan values
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |