Skip to content

Commit

Permalink
employee_record: Fix the archive serialized as string once and for all
Browse files Browse the repository at this point in the history
See 37c890f for rational, and
adccc5f for things we had to do since.
  • Loading branch information
rsebille committed Jan 6, 2025
1 parent a7d824b commit 646bacf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
12 changes: 0 additions & 12 deletions itou/employee_record/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ def company_data_sent(self, obj):
if not obj.archived_json:
return self.get_empty_value_display()

# Handle older data serialized from the JSON as string
if not isinstance(obj.archived_json, dict):
obj._set_archived_json(obj.archived_json)

siret = obj.archived_json["siret"]
measure = obj.archived_json["mesure"]
return f"{siret} ({measure})"
Expand All @@ -80,10 +76,6 @@ def user_data_sent(self, obj):
if not obj.archived_json:
return self.get_empty_value_display()

# Handle older data serialized from the JSON as string
if not isinstance(obj.archived_json, dict):
obj._set_archived_json(obj.archived_json)

firstname = obj.archived_json["personnePhysique"]["prenom"]
lastname = obj.archived_json["personnePhysique"]["nomUsage"]
id_itou = obj.archived_json["personnePhysique"]["idItou"]
Expand All @@ -94,10 +86,6 @@ def approval_data_sent(self, obj):
if not obj.archived_json:
return self.get_empty_value_display()

# Handle older data serialized from the JSON as string
if not isinstance(obj.archived_json, dict):
obj._set_archived_json(obj.archived_json)

number = obj.archived_json["personnePhysique"]["passIae"]
start = obj.archived_json["personnePhysique"]["passDateDeb"]
end = obj.archived_json["personnePhysique"]["passDateFin"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import json
import time

from django.db import migrations


def forward(apps, schema_editor):
print()
EmployeeRecord = apps.get_model("employee_record", "EmployeeRecord")
total_updated = 0
to_update = []
for obj in EmployeeRecord.objects.filter(archived_json__isnull=False, archived_json__startswith='"{').only(
"pk", "archived_json"
):
obj.archived_json = json.loads(obj.archived_json)
to_update.append(obj)
if len(to_update) % 1000 == 0:
total_updated += EmployeeRecord.objects.bulk_update(to_update, {"archived_json"})
print(f"{total_updated} objects updated")
to_update = []
time.sleep(0.5)
if to_update:
total_updated += EmployeeRecord.objects.bulk_update(to_update, {"archived_json"})
print(f"{total_updated} objects updated")


class Migration(migrations.Migration):
atomic = False

dependencies = [
("employee_record", "0001_initial"),
]

operations = [
migrations.RunPython(code=forward, reverse_code=migrations.RunPython.noop, elidable=True),
]

0 comments on commit 646bacf

Please sign in to comment.