71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
from django.db import migrations
|
|
|
|
|
|
def migrate_guided_giep_data(apps, schema_editor):
|
|
try:
|
|
GuidedOperationData = apps.get_model('interventions', 'GuidedOperationData')
|
|
GuidedOperationMedia = apps.get_model('interventions', 'GuidedOperationMedia')
|
|
GiepInspection = apps.get_model('inspections', 'GiepInspection')
|
|
InspectionDocument = apps.get_model('inspections', 'InspectionDocument')
|
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
|
Intervention = apps.get_model('interventions', 'Intervention')
|
|
except LookupError:
|
|
return
|
|
|
|
try:
|
|
interv_ct = ContentType.objects.get_for_model(Intervention)
|
|
except Exception:
|
|
return
|
|
|
|
giep_datas = GuidedOperationData.objects.filter(
|
|
guided_template__slug='inspection_rain_water'
|
|
).select_related('intervention')
|
|
|
|
for gd in giep_datas:
|
|
interv = gd.intervention
|
|
if not interv:
|
|
continue
|
|
|
|
raw_data = gd.data or {}
|
|
date_pluie = raw_data.get('date_derniere_pluie') or 'null'
|
|
orage = raw_data.get('orage_moins_une_heure')
|
|
|
|
giep_insp, _created = GiepInspection.objects.get_or_create(
|
|
asset_content_type=interv_ct,
|
|
asset_object_id=interv.id,
|
|
defaults={
|
|
'inspector': interv.created_by if hasattr(interv, 'created_by') else None,
|
|
'inspection_date': gd.created_at,
|
|
'result_status': 'compliant',
|
|
'nature': 'visual',
|
|
'inspector_role': 'observer',
|
|
'mission_intervention': interv,
|
|
'date_derniere_pluie': str(date_pluie),
|
|
'orage_moins_une_heure': orage,
|
|
'data': raw_data,
|
|
'notes': f"Migré depuis l'intervention {interv.code}",
|
|
}
|
|
)
|
|
|
|
for media in GuidedOperationMedia.objects.filter(guided_data=gd):
|
|
InspectionDocument.objects.get_or_create(
|
|
inspection=giep_insp,
|
|
file=media.file,
|
|
field_key=media.field_key
|
|
)
|
|
|
|
|
|
def reverse_migrate_guided_giep_data(apps, schema_editor):
|
|
pass
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('inspections', '0004_giepinspection_inspectiondocument_field_key_and_more'),
|
|
('interventions', '0001_initial'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(migrate_guided_giep_data, reverse_code=reverse_migrate_guided_giep_data),
|
|
]
|