loadandadddata correction
authorcavaliet
Tue, 09 Apr 2013 17:11:38 +0200
changeset 70 6c39cfabefc8
parent 69 329b4aa82e22
child 71 51dadd455326
loadandadddata correction
src/ldtplatform/management/commands/loadandadddata.py
--- a/src/ldtplatform/management/commands/loadandadddata.py	Mon Apr 08 17:03:38 2013 +0200
+++ b/src/ldtplatform/management/commands/loadandadddata.py	Tue Apr 09 17:11:38 2013 +0200
@@ -127,11 +127,11 @@
                         # (a bit fuzzy for media and src but good for others)
                         if not ((m=="auth.user" and "username" in obj["fields"] and obj["fields"]["username"] in user_ignore_list) or \
                                 (m=="auth.group" and "name" in obj["fields"] and obj["fields"]["name"] in group_ignore_list) or \
-                                (m=="ldt_utils.media" and "src" in obj["fields"] and any(s in obj["fields"]["src"] for s in content_ignore_list)) or \
+                                (m=="ldt_utils.media" and "src" in obj["fields"] and any((s+".") in obj["fields"]["src"] for s in content_ignore_list)) or \
                                 (m=="ldt_utils.content" and "iri_id" in obj["fields"] and obj["fields"]["iri_id"] in content_ignore_list)):
                             data_file1.append(obj)
                         #else:
-                        #   print("I don't keep from datas %s, pk = %s" % (m, obj["pk"]))
+                        #    print("I don't keep from datas %s, pk = %s" % (m, obj["pk"]))
                         if "pk" in obj:
                             # For both contents and projects, we save 2 dicts [id]=pk and [pk]=id
                             # It will enable to parse and replace easily the old pk by the new ones in the permission datas
@@ -156,6 +156,24 @@
             #data_file1.append(content_pk_id)
             #data_file1.append(content_id_pk)
             
+            # Check if import will fail with the usernames
+            existing_usernames = User.objects.all().values_list("username", flat=True)
+            for un in usernames:
+                if un in existing_usernames and un not in user_ignore_list:
+                    print("import will fail with username : %s" % str(un))
+                    do_import = False
+            
+            # Check if import will fail with the contents's iri_id
+            existing_iri_ids = Content.objects.all().values_list("iri_id", flat=True)
+            new_iri_ids = list(content_pk_id.values())
+            for iri_id in new_iri_ids:
+                if iri_id in existing_iri_ids and iri_id not in content_ignore_list:
+                    print("import will fail with iri_id : %s" % str(iri_id))
+                    do_import = False
+            if not do_import:
+                print("Add the usernames and iri_id to the ignore parameters -u and -c")
+                return ""
+            
             # We save the datas in a file in order to simply call loaddata
             print("Writing %s..." % path_file1)
             file1 =  open(path_file1, 'w')
@@ -186,6 +204,17 @@
                         ignored_content_pks.append(old_pk)
                         continue
                     # Keeping only valuables objs avoids errors when we we get the new pks
+                    obj_id = obj["fields"]["object_pk"]
+                    model = obj["model"] # "guardian.groupobjectpermission" or "guardian.userobjectpermission"
+                    if obj_id in content_ignore_list:
+                        if model=="guardian.groupobjectpermission":
+                            if obj["fields"]["group"][0] in group_ignore_list:
+                                #print("permissions : j'ignore %s pour le groupe %s ..." % (obj_id, obj["fields"]["group"][0]))
+                                continue
+                        elif model=="guardian.userobjectpermission":
+                            if obj["fields"]["user"][0] in user_ignore_list:
+                                #print("permissions : j'ignore %s pour le user %s ..." % (obj_id, obj["fields"]["user"][0]))
+                                continue
                     perm_data.append(obj)
             # We inform the user
             print("%d project permissions were ignored because projects do not exist in the current datas." % len(ignored_project_pks))