2
2
import json
3
3
4
4
from django .core .management .base import BaseCommand
5
+ from django .db import transaction
5
6
6
7
from qfdmo .models .acteur import Acteur , ActeurStatus , RevisionActeur
7
8
8
- CHUNK = 1000
9
-
10
9
11
10
class Command (BaseCommand ):
12
11
help = "Export Ressources using CSV format"
@@ -39,48 +38,54 @@ def handle(self, *args, **options):
39
38
with open (mapping_file , "r" ) as f :
40
39
mapping = json .load (f )
41
40
42
- for old_id , new_id in mapping .items ():
43
- # Check if the ids are not empty
44
- if old_id and new_id :
45
- self .stdout .write (
46
- self .style .SUCCESS (f"Updating `{ old_id } ` to `{ new_id } `" )
47
- )
48
- else :
49
- self .stdout .write (
50
- self .style .WARNING (
51
- f"Skipping `{ old_id } ` to `{ new_id } ` : one of the ids is empty"
41
+ with transaction .atomic ():
42
+ for old_id , new_id in mapping .items ():
43
+ # Check if the ids are not empty
44
+ if old_id and new_id :
45
+ self .stdout .write (
46
+ self .style .SUCCESS (f"Mapping `{ old_id } ` to `{ new_id } `" )
52
47
)
53
- )
54
- continue
55
-
56
- # test acteur with this new external id already exists
57
- acteur_from_db = Acteur .objects .filter (
58
- identifiant_externe = new_id , source__code = source_code
59
- ).first ()
60
- id_index = 0
61
- while acteur_from_db :
62
- id_index += 1
63
- new_id_indexed = f"{ new_id } _{ id_index } "
64
- acteur_from_db = Acteur .objects .filter (
65
- identifiant_externe = new_id_indexed , source__code = source_code
48
+ else :
49
+ self .stdout .write (
50
+ self .style .WARNING (
51
+ f"Skipping `{ old_id } ` to `{ new_id } ` : one of the ids is"
52
+ " empty"
53
+ )
54
+ )
55
+ continue
56
+
57
+ # Update acteur if exists
58
+ acteur = Acteur .objects .filter (
59
+ identifiant_externe = old_id , source__code = source_code
66
60
).first ()
67
61
68
- try :
69
- new_id = new_id_indexed
70
- statut = ActeurStatus .INACTIF
71
- except NameError :
72
- statut = ActeurStatus .ACTIF
62
+ if not acteur :
63
+ self .stdout .write (self .style .WARNING (f"Acteur { old_id } not found" ))
64
+ continue
73
65
74
- # Update acteur if exists
75
- acteur = Acteur .objects .filter (
76
- identifiant_externe = old_id , source__code = source_code
77
- ).first ()
66
+ # test acteur with this new external id already exists
67
+ acteur_from_db = Acteur .objects .filter (
68
+ identifiant_externe = new_id , source__code = source_code
69
+ ).first ()
70
+ id_index = 0
71
+ while acteur_from_db :
72
+ self .stdout .write (
73
+ self .style .WARNING (
74
+ f"Acteur { acteur_from_db .identifiant_externe } already"
75
+ " exists, trying to find an unused id"
76
+ )
77
+ )
78
+ id_index += 1
79
+ new_id_indexed = f"{ new_id } _{ id_index } "
80
+ acteur_from_db = Acteur .objects .filter (
81
+ identifiant_externe = new_id_indexed , source__code = source_code
82
+ ).first ()
78
83
79
- if not acteur :
80
- self .stdout .write (self .style .WARNING (f"Acteur { old_id } not found" ))
81
- continue
84
+ statut = ActeurStatus .ACTIF
85
+ if id_index :
86
+ new_id = f"{ new_id } _{ id_index } "
87
+ statut = ActeurStatus .INACTIF
82
88
83
- if not dry_run :
84
89
self .stdout .write (
85
90
self .style .SUCCESS (
86
91
f"Updating acteur { acteur .identifiant_unique } to { new_id } "
@@ -89,31 +94,23 @@ def handle(self, *args, **options):
89
94
acteur .identifiant_externe = new_id
90
95
acteur .statut = statut
91
96
acteur .save ()
92
- else :
93
- self .stdout .write (
94
- self .style .WARNING (
95
- f"Dry run: would update Acteur { old_id } to { new_id } "
96
- )
97
- )
98
97
99
- # Update revision acteur if exists
100
- revision_acteur = RevisionActeur .objects .filter (
101
- identifiant_externe = old_id , source__code = source_code
102
- ).first ()
98
+ # Update revision acteur if exists
99
+ revision_acteur = RevisionActeur .objects .filter (
100
+ identifiant_externe = old_id , source__code = source_code
101
+ ).first ()
103
102
104
- if revision_acteur and not dry_run :
105
- self .stdout .write (
106
- self .style .SUCCESS (
107
- f"Updating revision acteur { revision_acteur .identifiant_unique } "
108
- f" to { new_id } "
109
- )
110
- )
111
- revision_acteur .identifiant_externe = new_id
112
- revision_acteur .statut = statut
113
- revision_acteur .save ()
114
- if revision_acteur and dry_run :
115
- self .stdout .write (
116
- self .style .WARNING (
117
- f"Dry run: would update RevisionActeur { old_id } to { new_id } "
103
+ if revision_acteur :
104
+ self .stdout .write (
105
+ self .style .SUCCESS (
106
+ "Updating revision acteur "
107
+ f"{ revision_acteur .identifiant_unique } to { new_id } "
108
+ )
118
109
)
119
- )
110
+ revision_acteur .identifiant_externe = new_id
111
+ revision_acteur .statut = statut
112
+ revision_acteur .save ()
113
+
114
+ if dry_run :
115
+ # Rollback the transaction
116
+ raise Exception ("Rolling back transaction because of dry run" )
0 commit comments