-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3544 management script for previous commit
- Loading branch information
1 parent
e15fe7f
commit 3ea0d19
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
classification/management/commands/remove_invisible_characters.py
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,39 @@ | ||
import json | ||
|
||
from django.core.management.base import BaseCommand | ||
import re | ||
from classification.models import Classification | ||
from library.guardian_utils import admin_bot | ||
|
||
|
||
def has_invisible_characters(text): | ||
pattern = re.compile(r'[^\u0000-\u007F]') | ||
return re.search(pattern, text) | ||
|
||
|
||
def ensure_string(data): | ||
if isinstance(data, (dict, list)): | ||
return json.dumps(data) | ||
elif isinstance(data, str): | ||
return data | ||
else: | ||
return str(data) | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def handle(self, *args, **options): | ||
classifications = Classification.objects.all() | ||
user = admin_bot() | ||
|
||
for classification in classifications: | ||
evidence = classification.evidence | ||
|
||
for key, value in evidence.items(): | ||
for k, v in value.items(): | ||
if match := has_invisible_characters(ensure_string(v)): | ||
print(f"match found in {classification.id} record, in: {key}") | ||
classification.revalidate(user=user) | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS('Invisible characters removed from evidence field for all classifications.')) |
14 changes: 14 additions & 0 deletions
14
classification/migrations/0115_oneoff_remove_non_ascii_characters.py
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,14 @@ | ||
from django.db import migrations | ||
|
||
from manual.operations.manual_operations import ManualOperation | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('classification', '0114_classification_withdraw_reason'), | ||
] | ||
|
||
operations = [ | ||
ManualOperation(task_id=ManualOperation.task_id_manage(["remove_invisible_characters"])) | ||
] |