7 |
7 |
8 from django.contrib.auth.models import User, Group |
8 from django.contrib.auth.models import User, Group |
9 from django.core.management import call_command |
9 from django.core.management import call_command |
10 from django.core.management.base import BaseCommand, CommandError |
10 from django.core.management.base import BaseCommand, CommandError |
11 from ldt.ldt_utils.models import Content, Project |
11 from ldt.ldt_utils.models import Content, Project |
|
12 from ldt.security.cache import cached_assign |
12 from optparse import make_option |
13 from optparse import make_option |
|
14 import json |
13 import os.path |
15 import os.path |
14 import json |
|
15 from ldt.security.cache import cached_assign |
|
16 |
16 |
17 |
17 |
18 class Command(BaseCommand): |
18 class Command(BaseCommand): |
19 ''' |
19 ''' |
20 Load users, medias, contents, project and guardian permissions from json file generated by dumpdata |
20 Load users, medias, contents, project and guardian permissions from json file generated by dumpdata |
68 if len(args) != 1: |
68 if len(args) != 1: |
69 raise CommandError("The command has no argument or too much arguments. Only one is needed : the json file path.") |
69 raise CommandError("The command has no argument or too much arguments. Only one is needed : the json file path.") |
70 |
70 |
71 # Check if temporary files already exist |
71 # Check if temporary files already exist |
72 path = os.path.abspath(args[0]) |
72 path = os.path.abspath(args[0]) |
73 dir = os.path.dirname(path) |
73 dirpath = os.path.dirname(path) |
74 path_file1 = os.path.join(dir, 'temp_data1.json') |
74 path_file1 = os.path.join(dirpath, 'temp_data1.json') |
75 path_file2 = os.path.join(dir, 'temp_data2.json') |
75 path_file2 = os.path.join(dirpath, 'temp_data2.json') |
76 do_import = True |
76 do_import = True |
77 if os.path.exists(path_file1) or os.path.exists(path_file2): |
77 if os.path.exists(path_file1) or os.path.exists(path_file2): |
78 confirm = raw_input((""" |
78 confirm = raw_input((""" |
79 The folder %s contains the files temp_data1.json or temp_data2.json. These files will be overwritten. |
79 The folder %s contains the files temp_data1.json or temp_data2.json. These files will be overwritten. |
80 |
80 |
81 Do you want to continue ? |
81 Do you want to continue ? |
82 |
82 |
83 Type 'y' to continue, or 'n' to quit: """) % dir) |
83 Type 'y' to continue, or 'n' to quit: """) % dirpath) |
84 do_import = (confirm == "y") |
84 do_import = (confirm == "y") |
85 |
85 |
86 # Continue |
86 # Continue |
87 if do_import: |
87 if do_import: |
88 # Init ignore list |
88 # Init ignore list |
134 if "pk" in obj: |
134 if "pk" in obj: |
135 # For both contents and projects, we save 2 dicts [id]=pk and [pk]=id |
135 # For both contents and projects, we save 2 dicts [id]=pk and [pk]=id |
136 # It will enable to parse and replace easily the old pk by the new ones in the permission datas |
136 # It will enable to parse and replace easily the old pk by the new ones in the permission datas |
137 if m=="ldt_utils.project": |
137 if m=="ldt_utils.project": |
138 pk = str(obj["pk"]) |
138 pk = str(obj["pk"]) |
139 id = obj["fields"]["ldt_id"] |
139 ldt_id = obj["fields"]["ldt_id"] |
140 project_pk_id[pk] = id |
140 project_pk_id[pk] = ldt_id |
141 elif m=="ldt_utils.content": |
141 elif m=="ldt_utils.content": |
142 pk = str(obj["pk"]) |
142 pk = str(obj["pk"]) |
143 id = obj["fields"]["iri_id"] |
143 ldt_id = obj["fields"]["iri_id"] |
144 content_pk_id[pk] = id |
144 content_pk_id[pk] = ldt_id |
145 obj["pk"] = None |
145 obj["pk"] = None |
146 else: |
146 else: |
147 obj["pk"] = None |
147 obj["pk"] = None |
148 data_file2.append(obj) |
148 data_file2.append(obj) |
149 # Save usernames except AnonymousUser |
149 # Save usernames except AnonymousUser |
182 # We replace the old pk by the natural keys in the permission datas |
182 # We replace the old pk by the natural keys in the permission datas |
183 ignored_project_pks = [] |
183 ignored_project_pks = [] |
184 ignored_content_pks = [] |
184 ignored_content_pks = [] |
185 perm_data = [] |
185 perm_data = [] |
186 for obj in data_file2: |
186 for obj in data_file2: |
187 type = obj["fields"]["content_type"][1] |
187 content_type = obj["fields"]["content_type"][1] |
188 old_pk = obj["fields"]["object_pk"] |
188 old_pk = obj["fields"]["object_pk"] |
189 if type=="project": |
189 if content_type =="project": |
190 try: |
190 try: |
191 obj["fields"]["object_pk"] = project_pk_id[old_pk] |
191 obj["fields"]["object_pk"] = project_pk_id[old_pk] |
192 except: |
192 except: |
193 # The dumpdata can contain permissions for removed projects |
193 # The dumpdata can contain permissions for removed projects |
194 ignored_project_pks.append(old_pk) |
194 ignored_project_pks.append(old_pk) |
195 continue |
195 continue |
196 # Keeping only valuables objs avoids errors when we we get the new pks |
196 # Keeping only valuables objs avoids errors when we we get the new pks |
197 perm_data.append(obj) |
197 perm_data.append(obj) |
198 elif type == "content": |
198 elif content_type == "content": |
199 try: |
199 try: |
200 obj["fields"]["object_pk"] = content_pk_id[old_pk] |
200 obj["fields"]["object_pk"] = content_pk_id[old_pk] |
201 except: |
201 except: |
202 # The dumpdata can contain permissions for removed contents |
202 # The dumpdata can contain permissions for removed contents |
203 ignored_content_pks.append(old_pk) |
203 ignored_content_pks.append(old_pk) |
235 for p in projects: |
235 for p in projects: |
236 project_id_pk[p["ldt_id"]] = str(p["pk"]) |
236 project_id_pk[p["ldt_id"]] = str(p["pk"]) |
237 |
237 |
238 # Now we reparse the perm_data and update with the new pks |
238 # Now we reparse the perm_data and update with the new pks |
239 for obj in perm_data: |
239 for obj in perm_data: |
240 type = obj["fields"]["content_type"][1] |
240 content_type = obj["fields"]["content_type"][1] |
241 obj_id = obj["fields"]["object_pk"] |
241 obj_id = obj["fields"]["object_pk"] |
242 if type=="project": |
242 if content_type=="project": |
243 obj["fields"]["object_pk"] = project_id_pk[obj_id] |
243 obj["fields"]["object_pk"] = project_id_pk[obj_id] |
244 elif type == "content": |
244 elif content_type == "content": |
245 obj["fields"]["object_pk"] = content_id_pk[obj_id] |
245 obj["fields"]["object_pk"] = content_id_pk[obj_id] |
246 |
246 |
247 |
247 |
248 # We save the datas in a file in order to simply call loaddata |
248 # We save the datas in a file in order to simply call loaddata |
249 print("Writing %s..." % path_file2) |
249 print("Writing %s..." % path_file2) |