src/ldtplatform/management/commands/loadandadddata.py
author ymh <ymh.work@gmail.com>
Thu, 23 May 2013 00:24:23 +0200
changeset 102 e2968797bdae
parent 74 0ad5b530b306
permissions -rw-r--r--
upgrade to django 1.5
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 django.contrib.auth.models import User, Group
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
     9
from django.core.management import call_command
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    10
from django.core.management.base import BaseCommand, CommandError
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    11
from ldt.ldt_utils.models import Content, Project
102
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    12
from ldt.security.cache import cached_assign
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    13
from optparse import make_option
102
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    14
import json
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    15
import os.path
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    16
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
class Command(BaseCommand):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    19
    '''
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    20
    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
    21
    '''
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
    args = 'json_file'
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    24
    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
    25
    
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    26
    option_list = BaseCommand.option_list + (
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    27
        make_option('-u', '--ignore-users',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    28
            dest= 'ignore_users',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    29
            default= None,
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    30
            help= 'list of usernames to ignore (separated by comma)'
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    31
        ),
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    32
        make_option('-g', '--ignore-groups',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    33
            dest= 'ignore_groups',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    34
            default= None,
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    35
            help= 'list of group names to ignore (separated by comma)'
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    36
        ),
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    37
        make_option('-c', '--ignore-contents',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    38
            dest= 'ignore_contents',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    39
            default= None,
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    40
            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
    41
        ),
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    42
        make_option('-n', '--new-group',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    43
            dest= 'new_group',
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    44
            default= None,
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    45
            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
    46
        ),
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
    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
    51
        val = dict_arg.get(key, default)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    52
        return conv(val) if val else default
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    53
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    54
    def __safe_decode(self, s):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    55
        if not isinstance(s, basestring):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    56
            return s
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    57
        try:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    58
            return s.decode('utf8')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    59
        except:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    60
            try:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    61
                return s.decode('latin1')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    62
            except:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    63
                return s.decode('utf8','replace')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    64
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    65
    def handle(self, *args, **options):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    66
        
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    67
        # Test path
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    68
        if len(args) != 1:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    69
            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
    70
        
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    71
        # Check if temporary files already exist
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    72
        path = os.path.abspath(args[0])
102
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    73
        dirpath = os.path.dirname(path)
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    74
        path_file1 = os.path.join(dirpath, 'temp_data1.json')
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    75
        path_file2 = os.path.join(dirpath, 'temp_data2.json')
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    76
        do_import = True
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    77
        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
    78
            confirm = raw_input(("""
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    79
    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
    80
    
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    81
    Do you want to continue ?
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    82
102
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    83
    Type 'y' to continue, or 'n' to quit: """) % dirpath)
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    84
            do_import = (confirm == "y")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    85
        
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    86
        # Continue
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    87
        if do_import:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    88
            # Init ignore list
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    89
            user_ignore_list = ["admin","AnonymousUser"]
71
51dadd455326 modif on import script
cavaliet
parents: 70
diff changeset
    90
            group_ignore_list = ["everyone","Hashcut IRI","Hashcut BPI"]
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    91
            content_ignore_list = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    92
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    93
            # Update ignore list
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    94
            ignore_users = options.get('ignore_users', None)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    95
            ignore_groups = options.get('ignore_groups', None)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    96
            ignore_contents = options.get('ignore_contents', None)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    97
            if ignore_users:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    98
                for u in ignore_users.split(","):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
    99
                    user_ignore_list.append(u)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   100
            if ignore_groups:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   101
                for g in ignore_groups.split(","):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   102
                    group_ignore_list.append(g)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   103
            if ignore_contents:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   104
                for c in ignore_contents.split(","):
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   105
                    content_ignore_list.append(c)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   106
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   107
            # Begin work...
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   108
            print("Opening file...")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   109
            json_file = open(path,'rb')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   110
            print("Loading datas...")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   111
            data = json.load(json_file)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   112
            print("%d objects found..." % len(data))
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   113
            content_pk_id = {}
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   114
            project_pk_id = {}
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   115
            # datas for file 1 : users, medias, contents, projects 
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   116
            data_file1 = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   117
            # datas for file 2 : guardian permissions
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   118
            data_file2 = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   119
            # users
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   120
            usernames = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   121
            for obj in data:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   122
                if "model" in obj:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   123
                    m = obj["model"]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   124
                    if m!="guardian.userobjectpermission" and m!="guardian.groupobjectpermission":
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   125
                        # 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
   126
                        # (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
   127
                        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
   128
                                (m=="auth.group" and "name" in obj["fields"] and obj["fields"]["name"] in group_ignore_list) or \
70
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   129
                                (m=="ldt_utils.media" and "src" in obj["fields"] and any((s+".") in obj["fields"]["src"] for s in content_ignore_list)) or \
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   130
                                (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
   131
                            data_file1.append(obj)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   132
                        #else:
70
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   133
                        #    print("I don't keep from datas %s, pk = %s" % (m, obj["pk"]))
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   134
                        if "pk" in obj:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   135
                            # 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
   136
                            # 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
   137
                            if m=="ldt_utils.project":
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   138
                                pk = str(obj["pk"])
102
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   139
                                ldt_id = obj["fields"]["ldt_id"]
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   140
                                project_pk_id[pk] = ldt_id
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   141
                            elif m=="ldt_utils.content":
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   142
                                pk = str(obj["pk"])
102
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   143
                                ldt_id = obj["fields"]["iri_id"]
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   144
                                content_pk_id[pk] = ldt_id
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   145
                            obj["pk"] = None
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   146
                    else:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   147
                        obj["pk"] = None
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   148
                        data_file2.append(obj)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   149
                    # Save usernames except AnonymousUser 
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   150
                    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
   151
                        usernames.append(obj["fields"]["username"])
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   152
            json_file.close()
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   153
            #data_file1.append(project_pk_id)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   154
            #data_file1.append(project_id_pk)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   155
            #data_file1.append(content_pk_id)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   156
            #data_file1.append(content_id_pk)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   157
            
70
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   158
            # Check if import will fail with the usernames
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   159
            existing_usernames = User.objects.all().values_list("username", flat=True)
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   160
            for un in usernames:
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   161
                if un in existing_usernames and un not in user_ignore_list:
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   162
                    print("import will fail with username : %s" % str(un))
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   163
                    do_import = False
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   164
            
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   165
            # Check if import will fail with the contents's iri_id
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   166
            existing_iri_ids = Content.objects.all().values_list("iri_id", flat=True)
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   167
            new_iri_ids = list(content_pk_id.values())
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   168
            for iri_id in new_iri_ids:
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   169
                if iri_id in existing_iri_ids and iri_id not in content_ignore_list:
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   170
                    print("import will fail with iri_id : %s" % str(iri_id))
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   171
                    do_import = False
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   172
            if not do_import:
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   173
                print("Add the usernames and iri_id to the ignore parameters -u and -c")
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   174
                return ""
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   175
            
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   176
            # 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
   177
            print("Writing %s..." % path_file1)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   178
            file1 =  open(path_file1, 'w')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   179
            json.dump(data_file1, file1, indent=2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   180
            file1.close()
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   181
            print("Updating permissions ids...")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   182
            # 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
   183
            ignored_project_pks = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   184
            ignored_content_pks = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   185
            perm_data = []
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   186
            for obj in data_file2:
102
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   187
                content_type = obj["fields"]["content_type"][1]
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   188
                old_pk = obj["fields"]["object_pk"]
102
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   189
                if content_type =="project":
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   190
                    try:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   191
                        obj["fields"]["object_pk"] = project_pk_id[old_pk]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   192
                    except:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   193
                        # The dumpdata can contain permissions for removed projects
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   194
                        ignored_project_pks.append(old_pk)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   195
                        continue
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   196
                    # 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
   197
                    perm_data.append(obj)
102
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   198
                elif content_type == "content":
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   199
                    try:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   200
                        obj["fields"]["object_pk"] = content_pk_id[old_pk]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   201
                    except:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   202
                        # The dumpdata can contain permissions for removed contents
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   203
                        ignored_content_pks.append(old_pk)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   204
                        continue
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   205
                    # Keeping only valuables objs avoids errors when we we get the new pks
70
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   206
                    obj_id = obj["fields"]["object_pk"]
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   207
                    model = obj["model"] # "guardian.groupobjectpermission" or "guardian.userobjectpermission"
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   208
                    if obj_id in content_ignore_list:
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   209
                        if model=="guardian.groupobjectpermission":
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   210
                            if obj["fields"]["group"][0] in group_ignore_list:
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   211
                                #print("permissions : j'ignore %s pour le groupe %s ..." % (obj_id, obj["fields"]["group"][0]))
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   212
                                continue
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   213
                        elif model=="guardian.userobjectpermission":
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   214
                            if obj["fields"]["user"][0] in user_ignore_list:
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   215
                                #print("permissions : j'ignore %s pour le user %s ..." % (obj_id, obj["fields"]["user"][0]))
6c39cfabefc8 loadandadddata correction
cavaliet
parents: 69
diff changeset
   216
                                continue
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   217
                    perm_data.append(obj)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   218
            # We inform the user
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   219
            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
   220
            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
   221
            print("Loading datas from temporary file %s ..." % path_file1)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   222
            # Loaddata from file 1
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   223
            call_command("loaddata", path_file1)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   224
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   225
            # Now users, medias, contents, projects have been saved.
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   226
            # We can get the new pk for contents and projects
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   227
            # 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
   228
            # 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
   229
            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
   230
            content_id_pk = {}
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   231
            for c in contents:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   232
                content_id_pk[c.iri_id] = str(c.pk)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   233
            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
   234
            project_id_pk = {}
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   235
            for p in projects:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   236
                project_id_pk[p["ldt_id"]] = str(p["pk"])
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   237
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   238
            # 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
   239
            for obj in perm_data:
102
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   240
                content_type = obj["fields"]["content_type"][1]
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   241
                obj_id = obj["fields"]["object_pk"]
102
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   242
                if content_type=="project":
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   243
                    obj["fields"]["object_pk"] = project_id_pk[obj_id]
102
e2968797bdae upgrade to django 1.5
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   244
                elif content_type == "content":
69
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   245
                    obj["fields"]["object_pk"] = content_id_pk[obj_id]
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   246
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   247
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   248
            # 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
   249
            print("Writing %s..." % path_file2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   250
            file2 =  open(path_file2, 'w')
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   251
            json.dump(perm_data, file2, indent=2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   252
            file2.close()
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   253
            print("Loading permissions from temporary file %s ..." % path_file2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   254
            call_command("loaddata", path_file2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   255
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   256
            # Remove temp files
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   257
            print("Removing temporary files...")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   258
            try:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   259
                os.remove(path_file1)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   260
            except:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   261
                print("Removing temporary files %s failed" % path_file1)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   262
            try:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   263
                os.remove(path_file2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   264
            except:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   265
                print("Removing temporary files %s failed" % path_file2)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   266
            
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   267
            # 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
   268
            new_group = options.get('new_group', None)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   269
            if new_group and len(usernames)>0:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   270
                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
   271
                # Get or create group
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   272
                new_grp, _ = Group.objects.get_or_create(name=new_group)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   273
                # Add users to the group
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   274
                users = User.objects.filter(username__in=usernames)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   275
                for u in users:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   276
                    new_grp.user_set.add(u)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   277
                # Get all contents and medias
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   278
                for c in contents:
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   279
                    cached_assign('view_content', new_grp, c)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   280
                    cached_assign('view_media', new_grp, c.media_obj)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   281
                
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   282
            print("Indexing imported projects ...")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   283
            call_command('reindex', projects=True, no_content=True)
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   284
        
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   285
        # This is the end
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   286
        print("This is the end")
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   287
        
329b4aa82e22 right place for this command (and not ldt_utils)
cavaliet
parents:
diff changeset
   288