src/ldtplatform/management/commands/loadandadddata.py
author cavaliet
Mon, 08 Apr 2013 17:03:38 +0200
changeset 69 329b4aa82e22
child 70 6c39cfabefc8
permissions -rw-r--r--
right place for this command (and not ldt_utils)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
     2
'''
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
     3
Created on Mar 22, 2013
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
     4
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
     5
@author: tc
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
     6
'''
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
     7
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
     8
from ..utils import show_progress
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
     9
from django.contrib.auth.models import User, Group
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    10
from django.core.management import call_command
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    11
from django.core.management.base import BaseCommand, CommandError
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    12
from ldt.ldt_utils.models import Content, Project
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    13
from optparse import make_option
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    14
import os.path
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    15
import json
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    16
from ldt.security.cache import cached_assign
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    17
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    18
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    19
class Command(BaseCommand):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    20
    '''
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    21
    Load users, medias, contents, project and guardian permissions from json file generated by dumpdata
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    22
    '''
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    23
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    24
    args = 'json_file'
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    25
    help = 'Load users, medias, contents, project and guardian permissions from json file generated by dumpdata'
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    26
    
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    27
    option_list = BaseCommand.option_list + (
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    28
        make_option('-u', '--ignore-users',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    29
            dest= 'ignore_users',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    30
            default= None,
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    31
            help= 'list of usernames to ignore (separated by comma)'
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    32
        ),
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    33
        make_option('-g', '--ignore-groups',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    34
            dest= 'ignore_groups',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    35
            default= None,
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    36
            help= 'list of group names to ignore (separated by comma)'
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    37
        ),
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    38
        make_option('-c', '--ignore-contents',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    39
            dest= 'ignore_contents',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    40
            default= None,
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    41
            help= 'list of content iri_id to ignore (separated by comma)'
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    42
        ),
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    43
        make_option('-n', '--new-group',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    44
            dest= 'new_group',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    45
            default= None,
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    46
            help= 'The script will create a new group and assign view permission for all new media/contents to all the new users'
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    47
        ),
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    48
    )
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    49
    
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    50
    
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    51
    def __safe_get(self, dict_arg, key, conv = lambda x: x, default= None):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    52
        val = dict_arg.get(key, default)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    53
        return conv(val) if val else default
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    54
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    55
    def __safe_decode(self, s):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    56
        if not isinstance(s, basestring):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    57
            return s
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    58
        try:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    59
            return s.decode('utf8')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    60
        except:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    61
            try:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    62
                return s.decode('latin1')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    63
            except:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    64
                return s.decode('utf8','replace')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    65
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    66
    def handle(self, *args, **options):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    67
        
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    68
        # Test path
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    69
        if len(args) != 1:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    70
            raise CommandError("The command has no argument or too much arguments. Only one is needed : the json file path.")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    71
        
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    72
        # Check if temporary files already exist
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    73
        path = os.path.abspath(args[0])
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    74
        dir = os.path.dirname(path)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    75
        path_file1 = os.path.join(dir, 'temp_data1.json')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    76
        path_file2 = os.path.join(dir, 'temp_data2.json')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    77
        do_import = True
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    78
        if os.path.exists(path_file1) or os.path.exists(path_file2):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    79
            confirm = raw_input(("""
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    80
    The folder %s contains the files temp_data1.json or temp_data2.json. These files will be overwritten.
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    81
    
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    82
    Do you want to continue ?
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    83
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    84
    Type 'y' to continue, or 'n' to quit: """) % dir)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    85
            do_import = (confirm == "y")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    86
        
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    87
        # Continue
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    88
        if do_import:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    89
            # Init ignore list
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    90
            user_ignore_list = ["admin","AnonymousUser"]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    91
            group_ignore_list = ["everyone"]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    92
            content_ignore_list = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    93
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    94
            # Update ignore list
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    95
            ignore_users = options.get('ignore_users', None)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    96
            ignore_groups = options.get('ignore_groups', None)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    97
            ignore_contents = options.get('ignore_contents', None)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    98
            if ignore_users:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    99
                for u in ignore_users.split(","):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   100
                    user_ignore_list.append(u)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   101
            if ignore_groups:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   102
                for g in ignore_groups.split(","):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   103
                    group_ignore_list.append(g)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   104
            if ignore_contents:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   105
                for c in ignore_contents.split(","):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   106
                    content_ignore_list.append(c)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   107
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   108
            # Begin work...
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   109
            print("Opening file...")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   110
            json_file = open(path,'rb')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   111
            print("Loading datas...")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   112
            data = json.load(json_file)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   113
            print("%d objects found..." % len(data))
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   114
            content_pk_id = {}
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   115
            project_pk_id = {}
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   116
            # datas for file 1 : users, medias, contents, projects 
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   117
            data_file1 = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   118
            # datas for file 2 : guardian permissions
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   119
            data_file2 = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   120
            # users
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   121
            usernames = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   122
            for obj in data:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   123
                if "model" in obj:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   124
                    m = obj["model"]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   125
                    if m!="guardian.userobjectpermission" and m!="guardian.groupobjectpermission":
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   126
                        # We remove user admin, user AnonymousUser, group everyone and users and contents in ignore list
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   127
                        # (a bit fuzzy for media and src but good for others)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   128
                        if not ((m=="auth.user" and "username" in obj["fields"] and obj["fields"]["username"] in user_ignore_list) or \
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   129
                                (m=="auth.group" and "name" in obj["fields"] and obj["fields"]["name"] in group_ignore_list) or \
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   130
                                (m=="ldt_utils.media" and "src" in obj["fields"] and any(s in obj["fields"]["src"] for s in content_ignore_list)) or \
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   131
                                (m=="ldt_utils.content" and "iri_id" in obj["fields"] and obj["fields"]["iri_id"] in content_ignore_list)):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   132
                            data_file1.append(obj)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   133
                        #else:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   134
                        #   print("I don't keep from datas %s, pk = %s" % (m, obj["pk"]))
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   135
                        if "pk" in obj:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   136
                            # For both contents and projects, we save 2 dicts [id]=pk and [pk]=id
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   137
                            # It will enable to parse and replace easily the old pk by the new ones in the permission datas
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   138
                            if m=="ldt_utils.project":
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   139
                                pk = str(obj["pk"])
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   140
                                id = obj["fields"]["ldt_id"]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   141
                                project_pk_id[pk] = id
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   142
                            elif m=="ldt_utils.content":
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   143
                                pk = str(obj["pk"])
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   144
                                id = obj["fields"]["iri_id"]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   145
                                content_pk_id[pk] = id
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   146
                            obj["pk"] = None
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   147
                    else:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   148
                        obj["pk"] = None
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   149
                        data_file2.append(obj)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   150
                    # Save usernames except AnonymousUser 
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   151
                    if m=="auth.user" and "username" in obj["fields"] and obj["fields"]["username"]!="AnonymousUser":
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   152
                        usernames.append(obj["fields"]["username"])
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   153
            json_file.close()
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   154
            #data_file1.append(project_pk_id)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   155
            #data_file1.append(project_id_pk)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   156
            #data_file1.append(content_pk_id)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   157
            #data_file1.append(content_id_pk)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   158
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   159
            # We save the datas in a file in order to simply call loaddata
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   160
            print("Writing %s..." % path_file1)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   161
            file1 =  open(path_file1, 'w')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   162
            json.dump(data_file1, file1, indent=2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   163
            file1.close()
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   164
            print("Updating permissions ids...")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   165
            # We replace the old pk by the natural keys in the permission datas
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   166
            ignored_project_pks = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   167
            ignored_content_pks = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   168
            perm_data = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   169
            for obj in data_file2:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   170
                type = obj["fields"]["content_type"][1]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   171
                old_pk = obj["fields"]["object_pk"]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   172
                if type=="project":
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   173
                    try:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   174
                        obj["fields"]["object_pk"] = project_pk_id[old_pk]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   175
                    except:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   176
                        # The dumpdata can contain permissions for removed projects
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   177
                        ignored_project_pks.append(old_pk)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   178
                        continue
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   179
                    # Keeping only valuables objs avoids errors when we we get the new pks
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   180
                    perm_data.append(obj)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   181
                elif type == "content":
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   182
                    try:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   183
                        obj["fields"]["object_pk"] = content_pk_id[old_pk]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   184
                    except:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   185
                        # The dumpdata can contain permissions for removed contents
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   186
                        ignored_content_pks.append(old_pk)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   187
                        continue
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   188
                    # Keeping only valuables objs avoids errors when we we get the new pks
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   189
                    perm_data.append(obj)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   190
            # We inform the user
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   191
            print("%d project permissions were ignored because projects do not exist in the current datas." % len(ignored_project_pks))
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   192
            print("%d content permissions were ignored because contents do not exist in the current datas." % len(ignored_content_pks))
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   193
            print("Loading datas from temporary file %s ..." % path_file1)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   194
            # Loaddata from file 1
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   195
            call_command("loaddata", path_file1)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   196
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   197
            # Now users, medias, contents, projects have been saved.
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   198
            # We can get the new pk for contents and projects
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   199
            # Careful: in Python 3, dict.copy().values() will be prefered to list(dict.values())
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   200
            # We use select_related("media_obj") because it will usefull with the new group
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   201
            contents = Content.objects.filter(iri_id__in=list(content_pk_id.values())).select_related("media_obj")#.values('pk', 'iri_id')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   202
            content_id_pk = {}
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   203
            for c in contents:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   204
                content_id_pk[c.iri_id] = str(c.pk)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   205
            projects = Project.objects.filter(ldt_id__in=list(project_pk_id.values())).values('pk', 'ldt_id')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   206
            project_id_pk = {}
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   207
            for p in projects:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   208
                project_id_pk[p["ldt_id"]] = str(p["pk"])
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   209
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   210
            # Now we reparse the perm_data and update with the new pks
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   211
            for obj in perm_data:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   212
                type = obj["fields"]["content_type"][1]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   213
                obj_id = obj["fields"]["object_pk"]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   214
                if type=="project":
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   215
                    obj["fields"]["object_pk"] = project_id_pk[obj_id]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   216
                elif type == "content":
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   217
                    obj["fields"]["object_pk"] = content_id_pk[obj_id]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   218
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   219
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   220
            # We save the datas in a file in order to simply call loaddata
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   221
            print("Writing %s..." % path_file2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   222
            file2 =  open(path_file2, 'w')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   223
            json.dump(perm_data, file2, indent=2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   224
            file2.close()
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   225
            print("Loading permissions from temporary file %s ..." % path_file2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   226
            call_command("loaddata", path_file2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   227
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   228
            # Remove temp files
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   229
            print("Removing temporary files...")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   230
            try:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   231
                os.remove(path_file1)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   232
            except:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   233
                print("Removing temporary files %s failed" % path_file1)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   234
            try:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   235
                os.remove(path_file2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   236
            except:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   237
                print("Removing temporary files %s failed" % path_file2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   238
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   239
            # Now that all datas have been imported we can create the new group and assign permissions if asked
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   240
            new_group = options.get('new_group', None)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   241
            if new_group and len(usernames)>0:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   242
                print("Set view permissions for the new group %s ..." % new_group)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   243
                # Get or create group
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   244
                new_grp, _ = Group.objects.get_or_create(name=new_group)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   245
                # Add users to the group
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   246
                users = User.objects.filter(username__in=usernames)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   247
                for u in users:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   248
                    new_grp.user_set.add(u)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   249
                # Get all contents and medias
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   250
                for c in contents:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   251
                    cached_assign('view_content', new_grp, c)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   252
                    cached_assign('view_media', new_grp, c.media_obj)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   253
                
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   254
            print("Indexing imported projects ...")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   255
            call_command('reindex', projects=True, no_content=True)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   256
        
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   257
        # This is the end
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   258
        print("This is the end")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   259
        
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   260