src/ldtplatform/management/commands/loadandadddata.py
changeset 70 6c39cfabefc8
parent 69 329b4aa82e22
child 71 51dadd455326
equal deleted inserted replaced
69:329b4aa82e22 70:6c39cfabefc8
   125                     if m!="guardian.userobjectpermission" and m!="guardian.groupobjectpermission":
   125                     if m!="guardian.userobjectpermission" and m!="guardian.groupobjectpermission":
   126                         # We remove user admin, user AnonymousUser, group everyone and users and contents in ignore list
   126                         # We remove user admin, user AnonymousUser, group everyone and users and contents in ignore list
   127                         # (a bit fuzzy for media and src but good for others)
   127                         # (a bit fuzzy for media and src but good for others)
   128                         if not ((m=="auth.user" and "username" in obj["fields"] and obj["fields"]["username"] in user_ignore_list) or \
   128                         if not ((m=="auth.user" and "username" in obj["fields"] and obj["fields"]["username"] in user_ignore_list) or \
   129                                 (m=="auth.group" and "name" in obj["fields"] and obj["fields"]["name"] in group_ignore_list) or \
   129                                 (m=="auth.group" and "name" in obj["fields"] and obj["fields"]["name"] in group_ignore_list) or \
   130                                 (m=="ldt_utils.media" and "src" in obj["fields"] and any(s in obj["fields"]["src"] for s in content_ignore_list)) or \
   130                                 (m=="ldt_utils.media" and "src" in obj["fields"] and any((s+".") in obj["fields"]["src"] for s in content_ignore_list)) or \
   131                                 (m=="ldt_utils.content" and "iri_id" in obj["fields"] and obj["fields"]["iri_id"] in content_ignore_list)):
   131                                 (m=="ldt_utils.content" and "iri_id" in obj["fields"] and obj["fields"]["iri_id"] in content_ignore_list)):
   132                             data_file1.append(obj)
   132                             data_file1.append(obj)
   133                         #else:
   133                         #else:
   134                         #   print("I don't keep from datas %s, pk = %s" % (m, obj["pk"]))
   134                         #    print("I don't keep from datas %s, pk = %s" % (m, obj["pk"]))
   135                         if "pk" in obj:
   135                         if "pk" in obj:
   136                             # For both contents and projects, we save 2 dicts [id]=pk and [pk]=id
   136                             # For both contents and projects, we save 2 dicts [id]=pk and [pk]=id
   137                             # It will enable to parse and replace easily the old pk by the new ones in the permission datas
   137                             # It will enable to parse and replace easily the old pk by the new ones in the permission datas
   138                             if m=="ldt_utils.project":
   138                             if m=="ldt_utils.project":
   139                                 pk = str(obj["pk"])
   139                                 pk = str(obj["pk"])
   153             json_file.close()
   153             json_file.close()
   154             #data_file1.append(project_pk_id)
   154             #data_file1.append(project_pk_id)
   155             #data_file1.append(project_id_pk)
   155             #data_file1.append(project_id_pk)
   156             #data_file1.append(content_pk_id)
   156             #data_file1.append(content_pk_id)
   157             #data_file1.append(content_id_pk)
   157             #data_file1.append(content_id_pk)
       
   158             
       
   159             # Check if import will fail with the usernames
       
   160             existing_usernames = User.objects.all().values_list("username", flat=True)
       
   161             for un in usernames:
       
   162                 if un in existing_usernames and un not in user_ignore_list:
       
   163                     print("import will fail with username : %s" % str(un))
       
   164                     do_import = False
       
   165             
       
   166             # Check if import will fail with the contents's iri_id
       
   167             existing_iri_ids = Content.objects.all().values_list("iri_id", flat=True)
       
   168             new_iri_ids = list(content_pk_id.values())
       
   169             for iri_id in new_iri_ids:
       
   170                 if iri_id in existing_iri_ids and iri_id not in content_ignore_list:
       
   171                     print("import will fail with iri_id : %s" % str(iri_id))
       
   172                     do_import = False
       
   173             if not do_import:
       
   174                 print("Add the usernames and iri_id to the ignore parameters -u and -c")
       
   175                 return ""
   158             
   176             
   159             # We save the datas in a file in order to simply call loaddata
   177             # We save the datas in a file in order to simply call loaddata
   160             print("Writing %s..." % path_file1)
   178             print("Writing %s..." % path_file1)
   161             file1 =  open(path_file1, 'w')
   179             file1 =  open(path_file1, 'w')
   162             json.dump(data_file1, file1, indent=2)
   180             json.dump(data_file1, file1, indent=2)
   184                     except:
   202                     except:
   185                         # The dumpdata can contain permissions for removed contents
   203                         # The dumpdata can contain permissions for removed contents
   186                         ignored_content_pks.append(old_pk)
   204                         ignored_content_pks.append(old_pk)
   187                         continue
   205                         continue
   188                     # Keeping only valuables objs avoids errors when we we get the new pks
   206                     # Keeping only valuables objs avoids errors when we we get the new pks
       
   207                     obj_id = obj["fields"]["object_pk"]
       
   208                     model = obj["model"] # "guardian.groupobjectpermission" or "guardian.userobjectpermission"
       
   209                     if obj_id in content_ignore_list:
       
   210                         if model=="guardian.groupobjectpermission":
       
   211                             if obj["fields"]["group"][0] in group_ignore_list:
       
   212                                 #print("permissions : j'ignore %s pour le groupe %s ..." % (obj_id, obj["fields"]["group"][0]))
       
   213                                 continue
       
   214                         elif model=="guardian.userobjectpermission":
       
   215                             if obj["fields"]["user"][0] in user_ignore_list:
       
   216                                 #print("permissions : j'ignore %s pour le user %s ..." % (obj_id, obj["fields"]["user"][0]))
       
   217                                 continue
   189                     perm_data.append(obj)
   218                     perm_data.append(obj)
   190             # We inform the user
   219             # We inform the user
   191             print("%d project permissions were ignored because projects do not exist in the current datas." % len(ignored_project_pks))
   220             print("%d project permissions were ignored because projects do not exist in the current datas." % len(ignored_project_pks))
   192             print("%d content permissions were ignored because contents do not exist in the current datas." % len(ignored_content_pks))
   221             print("%d content permissions were ignored because contents do not exist in the current datas." % len(ignored_content_pks))
   193             print("Loading datas from temporary file %s ..." % path_file1)
   222             print("Loading datas from temporary file %s ..." % path_file1)