management command first step
authorcavaliet
Tue, 04 Mar 2014 14:48:25 +0100
changeset 7 4681446b722d
parent 6 37d3532fb8e7
child 8 5669fe792a0b
management command first step
.hgignore
integ/css/spel.css
integ/index.html
integ/lib/bootstrap-multiselect.css
integ/lib/bootstrap-multiselect.js
integ/lib/bootstrap/css/bootstrap-theme.css
integ/lib/bootstrap/css/bootstrap-theme.css.map
integ/lib/bootstrap/css/bootstrap-theme.min.css
integ/lib/bootstrap/css/bootstrap.css
integ/lib/bootstrap/css/bootstrap.css.map
integ/lib/bootstrap/css/bootstrap.min.css
integ/lib/bootstrap/fonts/glyphicons-halflings-regular.eot
integ/lib/bootstrap/fonts/glyphicons-halflings-regular.svg
integ/lib/bootstrap/fonts/glyphicons-halflings-regular.ttf
integ/lib/bootstrap/fonts/glyphicons-halflings-regular.woff
integ/lib/bootstrap/js/bootstrap.js
integ/lib/bootstrap/js/bootstrap.min.js
integ/lib/jquery-2.1.0.min.js
integ/spectacle.html
maquette/Interface_web_maquette_25_09_13.pdf
src/manage.py
src/requirement.txt
src/spel/__init__.py
src/spel/config.py.tmpl
src/spel/management/__init__.py
src/spel/management/commands/__init__.py
src/spel/management/commands/loadspeldata.py
src/spel/models.py
src/spel/modwsgi.wsgi
src/spel/settings.py
src/spel/static/spel/css/spel.css
src/spel/static/spel/img/logo_IRI_footer.png
src/spel/static/spel/js/spectacle.js
src/spel/static/spel/lib/bootstrap-multiselect.css
src/spel/static/spel/lib/bootstrap-multiselect.js
src/spel/static/spel/lib/bootstrap/css/bootstrap-theme.css
src/spel/static/spel/lib/bootstrap/css/bootstrap-theme.css.map
src/spel/static/spel/lib/bootstrap/css/bootstrap-theme.min.css
src/spel/static/spel/lib/bootstrap/css/bootstrap.css
src/spel/static/spel/lib/bootstrap/css/bootstrap.css.map
src/spel/static/spel/lib/bootstrap/css/bootstrap.min.css
src/spel/static/spel/lib/bootstrap/fonts/glyphicons-halflings-regular.eot
src/spel/static/spel/lib/bootstrap/fonts/glyphicons-halflings-regular.svg
src/spel/static/spel/lib/bootstrap/fonts/glyphicons-halflings-regular.ttf
src/spel/static/spel/lib/bootstrap/fonts/glyphicons-halflings-regular.woff
src/spel/static/spel/lib/bootstrap/js/bootstrap.js
src/spel/static/spel/lib/bootstrap/js/bootstrap.min.js
src/spel/static/spel/lib/jquery-2.1.0.min.js
src/spel/templates/spel_base.html
src/spel/templates/spel_home.html
src/spel/templates/spel_spectacle.html
src/spel/urls.py
src/spel/views.py
virtualenv/res/lib/lib_create_env.py
virtualenv/res/lib/patch.py
virtualenv/res/src/IRI-django-registration-e23626c256c0.tar.gz
virtualenv/res/src/MySQL-python-1.2.3.win-amd64-py2.7.exe
virtualenv/res/src/MySQL-python-1.2.3.win32-py2.7.exe
virtualenv/res/src/MySQL-python-1.2.4b4.tar.gz
virtualenv/res/src/PIL-1.1.7.win32-py2.7.exe
virtualenv/res/src/Pillow-2.0.0.tar.gz
virtualenv/res/src/PyYAML-3.10.tar.gz
virtualenv/res/src/defusedxml-0.4.1.tar.gz
virtualenv/res/src/distribute-0.6.34.tar.gz
virtualenv/res/src/django-1.5.5-IRI.tar.gz
virtualenv/res/src/django-chunked-uploads-0.5.tar.gz
virtualenv/res/src/django-extensions-1.1.1.tar.gz
virtualenv/res/src/django-guardian-e311bc1040222321ad1bfb74ac8e8fea9748d674.tar.gz
virtualenv/res/src/django-haystack-2.0.0.tar.gz
virtualenv/res/src/django-oauth-plus-2.1.0.tar.gz
virtualenv/res/src/django-openid-consumer-0.1.1.tar.gz
virtualenv/res/src/django-social-auth-0.7.23.tar.gz
virtualenv/res/src/django-tagging-0.3.1.tar.gz
virtualenv/res/src/django-tastypie-0.9.15.tar.gz
virtualenv/res/src/httplib2-0.8.tar.gz
virtualenv/res/src/jezdez-setuptools_hg-b025f18e170b.tar.gz
virtualenv/res/src/jpegsrc.v8d.tar.gz
virtualenv/res/src/lxml-2.3-py2.7-win32.egg
virtualenv/res/src/lxml-3.2.1.tar.bz2
virtualenv/res/src/mimeparse-0.1.3.tar.gz
virtualenv/res/src/oauth2-1.5.211.tar.gz
virtualenv/res/src/psycopg2-2.4.6.win32-py2.7-pg9.2.2-release.exe
virtualenv/res/src/psycopg2-2.5.tar.gz
virtualenv/res/src/pyelasticsearch-0.5.tar.gz
virtualenv/res/src/python-dateutil-2.1.tar.gz
virtualenv/res/src/python-digest-1.7.tar.gz
virtualenv/res/src/python-openid-2.2.5.tar.gz
virtualenv/res/src/requests-1.2.0.tar.gz
virtualenv/res/src/setuptools_hg-0.4.tar.gz
virtualenv/res/src/simplejson-3.3.0.tar.gz
virtualenv/res/src/six-1.3.0.tar.gz
virtualenv/res/src/sorl-thumbnail-v11.12.tar.gz
virtualenv/res/src/south-0.7.6.tar.gz
virtualenv/res/src/whoosh-2.5.tar.gz
virtualenv/res/src/zlib-1.2.8.tar.gz
virtualenv/sync/virtualenv_support/distribute-0.6.34.tar.gz
virtualenv/sync/virtualenv_support/pip-1.3.1.tar.gz
virtualenv/web/create_python_env.py
virtualenv/web/res/README
virtualenv/web/res/patch/.keepme
virtualenv/web/res/patch/django_contrib_auth_views.diff
virtualenv/web/res/patch/oauth2.diff
virtualenv/web/res/patch/social_auth_views_diff.diff
virtualenv/web/res/res_create_env.py
virtualenv/web/res/srvr_requirements.txt
virtualenv/web/virtualenv_support/__init__.py
virtualenv/web/virtualenv_support/distribute-0.6.34.tar.gz
virtualenv/web/virtualenv_support/pip-1.3.1.tar.gz
virtualenv/web/virtualenv_support/setuptools-0.6c11-py2.5.egg
virtualenv/web/virtualenv_support/setuptools-0.6c11-py2.6.egg
virtualenv/web/virtualenv_support/setuptools-0.6c11-py2.7.egg
web/.htaccess.tmpl
web/static/.keepme
--- a/.hgignore	Mon Mar 03 17:47:45 2014 +0100
+++ b/.hgignore	Tue Mar 04 14:48:25 2014 +0100
@@ -14,4 +14,6 @@
 syntax: regexp
 ^\.settings/org\.eclipse\.core\.resources\.prefs$
 syntax: regexp
-^web/\.htaccess$
\ No newline at end of file
+^web/\.htaccess$
+syntax: regexp
+^web/static/media/ldt$
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/spel/management/__init__.py	Tue Mar 04 14:48:25 2014 +0100
@@ -0,0 +1,1 @@
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/spel/management/commands/__init__.py	Tue Mar 04 14:48:25 2014 +0100
@@ -0,0 +1,1 @@
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/spel/management/commands/loadspeldata.py	Tue Mar 04 14:48:25 2014 +0100
@@ -0,0 +1,295 @@
+# -*- coding: utf-8 -*-
+'''
+Created on Mar 22, 2013
+
+@author: tc
+'''
+
+from dircache import listdir
+from django.contrib.auth.models import User, Group
+from django.core.management import call_command
+from django.core.management.base import BaseCommand, CommandError
+from genericpath import isdir, isfile
+from ldt.ldt_utils.models import Content, Project
+from ldt.security.cache import cached_assign
+from ldt.ldt_utils.models import Media, Content, Project
+from optparse import make_option
+from os.path import join
+import json
+import os.path
+from ldt.ldt_utils.utils import generate_uuid
+
+
+class Command(BaseCommand):
+    '''
+    Load medias, contents, project from json generated by ubicast
+    '''
+
+    args = 'folder containing folders containing json files'
+    help = 'Load users, medias, contents, project and guardian permissions from json file generated by dumpdata'
+    
+    
+    
+    
+    def __safe_get(self, dict_arg, key, conv = lambda x: x, default= None):
+        val = dict_arg.get(key, default)
+        return conv(val) if val else default
+
+    def __safe_decode(self, s):
+        if not isinstance(s, basestring):
+            return s
+        try:
+            return s.decode('utf8')
+        except:
+            try:
+                return s.decode('latin1')
+            except:
+                return s.decode('utf8','replace')
+
+    def handle(self, *args, **options):
+        
+        # Test path
+        if len(args) != 1:
+            raise CommandError("The command has no argument or too much arguments. Only one is needed : the folder file path.")
+        
+        # Check if temporary files already exist
+        path = os.path.abspath(args[0])
+        if not os.path.exists(path):
+            raise CommandError("The folder does not exist.")
+        
+        do_import = False
+        confirm = raw_input("""
+    If the database already contains datas, they will be overriden
+    
+    Do you want to continue ?
+
+    Type 'y' to continue, or 'n' to quit: """)
+        do_import = (confirm == "y")
+        
+        
+        # Continue
+        if do_import:
+            print("Parsing folder %s ..." % path)
+            for f in listdir(path):
+                json_path = join(path,f,"cinelab.json")
+                if isfile(json_path):
+                    print("Parsing json file %s ..." % json_path)
+                    try:
+                        file_data = open(json_path)
+                        json_data = json.load(file_data)
+                        file_data.close()
+                    except:
+                        print("    Error while parsing json file.")
+                    
+                    # Save media
+                    media, _ = Media.objects.get_or_create(src=f+"/original.mp4", duration=json_data["medias"][0]["meta"]["duration"])
+                    ctt_id = generate_uuid()
+                    content = Content.objects.create(iri_id=ctt_id, iriurl=ctt_id+u"/"+ctt_id+u".iri", media_obj=media, title=json_data["meta"]["dc:title"])
+                    
+                    
+                    
+                else:
+                    print("Ignoring or not exist %s ..." % json_path)
+            """
+            # Init ignore list
+            user_ignore_list = ["admin","AnonymousUser"]
+            group_ignore_list = ["everyone","Hashcut IRI","Hashcut BPI"]
+            content_ignore_list = []
+            
+            # Update ignore list
+            ignore_users = options.get('ignore_users', None)
+            ignore_groups = options.get('ignore_groups', None)
+            ignore_contents = options.get('ignore_contents', None)
+            if ignore_users:
+                for u in ignore_users.split(","):
+                    user_ignore_list.append(u)
+            if ignore_groups:
+                for g in ignore_groups.split(","):
+                    group_ignore_list.append(g)
+            if ignore_contents:
+                for c in ignore_contents.split(","):
+                    content_ignore_list.append(c)
+            
+            # Begin work...
+            print("Opening file...")
+            json_file = open(path,'rb')
+            print("Loading datas...")
+            data = json.load(json_file)
+            print("%d objects found..." % len(data))
+            content_pk_id = {}
+            project_pk_id = {}
+            # datas for file 1 : users, medias, contents, projects 
+            data_file1 = []
+            # datas for file 2 : guardian permissions
+            data_file2 = []
+            # users
+            usernames = []
+            for obj in data:
+                if "model" in obj:
+                    m = obj["model"]
+                    if m!="guardian.userobjectpermission" and m!="guardian.groupobjectpermission":
+                        # We remove user admin, user AnonymousUser, group everyone and users and contents in ignore list
+                        # (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.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"]))
+                        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
+                            if m=="ldt_utils.project":
+                                pk = str(obj["pk"])
+                                ldt_id = obj["fields"]["ldt_id"]
+                                project_pk_id[pk] = ldt_id
+                            elif m=="ldt_utils.content":
+                                pk = str(obj["pk"])
+                                ldt_id = obj["fields"]["iri_id"]
+                                content_pk_id[pk] = ldt_id
+                            obj["pk"] = None
+                    else:
+                        obj["pk"] = None
+                        data_file2.append(obj)
+                    # Save usernames except AnonymousUser 
+                    if m=="auth.user" and "username" in obj["fields"] and obj["fields"]["username"]!="AnonymousUser":
+                        usernames.append(obj["fields"]["username"])
+            json_file.close()
+            #data_file1.append(project_pk_id)
+            #data_file1.append(project_id_pk)
+            #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')
+            json.dump(data_file1, file1, indent=2)
+            file1.close()
+            print("Updating permissions ids...")
+            # We replace the old pk by the natural keys in the permission datas
+            ignored_project_pks = []
+            ignored_content_pks = []
+            perm_data = []
+            for obj in data_file2:
+                content_type = obj["fields"]["content_type"][1]
+                old_pk = obj["fields"]["object_pk"]
+                if content_type =="project":
+                    try:
+                        obj["fields"]["object_pk"] = project_pk_id[old_pk]
+                    except:
+                        # The dumpdata can contain permissions for removed projects
+                        ignored_project_pks.append(old_pk)
+                        continue
+                    # Keeping only valuables objs avoids errors when we we get the new pks
+                    perm_data.append(obj)
+                elif content_type == "content":
+                    try:
+                        obj["fields"]["object_pk"] = content_pk_id[old_pk]
+                    except:
+                        # The dumpdata can contain permissions for removed contents
+                        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))
+            print("%d content permissions were ignored because contents do not exist in the current datas." % len(ignored_content_pks))
+            print("Loading datas from temporary file %s ..." % path_file1)
+            # Loaddata from file 1
+            call_command("loaddata", path_file1)
+            
+            # Now users, medias, contents, projects have been saved.
+            # We can get the new pk for contents and projects
+            # Careful: in Python 3, dict.copy().values() will be prefered to list(dict.values())
+            # We use select_related("media_obj") because it will usefull with the new group
+            contents = Content.objects.filter(iri_id__in=list(content_pk_id.values())).select_related("media_obj")#.values('pk', 'iri_id')
+            content_id_pk = {}
+            for c in contents:
+                content_id_pk[c.iri_id] = str(c.pk)
+            projects = Project.objects.filter(ldt_id__in=list(project_pk_id.values())).values('pk', 'ldt_id')
+            project_id_pk = {}
+            for p in projects:
+                project_id_pk[p["ldt_id"]] = str(p["pk"])
+            
+            # Now we reparse the perm_data and update with the new pks
+            for obj in perm_data:
+                content_type = obj["fields"]["content_type"][1]
+                obj_id = obj["fields"]["object_pk"]
+                if content_type=="project":
+                    obj["fields"]["object_pk"] = project_id_pk[obj_id]
+                elif content_type == "content":
+                    obj["fields"]["object_pk"] = content_id_pk[obj_id]
+            
+            
+            # We save the datas in a file in order to simply call loaddata
+            print("Writing %s..." % path_file2)
+            file2 =  open(path_file2, 'w')
+            json.dump(perm_data, file2, indent=2)
+            file2.close()
+            print("Loading permissions from temporary file %s ..." % path_file2)
+            call_command("loaddata", path_file2)
+            
+            # Remove temp files
+            print("Removing temporary files...")
+            try:
+                os.remove(path_file1)
+            except:
+                print("Removing temporary files %s failed" % path_file1)
+            try:
+                os.remove(path_file2)
+            except:
+                print("Removing temporary files %s failed" % path_file2)
+            
+            # Now that all datas have been imported we can create the new group and assign permissions if asked
+            new_group = options.get('new_group', None)
+            if new_group and len(usernames)>0:
+                print("Set view permissions for the new group %s ..." % new_group)
+                # Get or create group
+                new_grp, _ = Group.objects.get_or_create(name=new_group)
+                # Add users to the group
+                users = User.objects.filter(username__in=usernames)
+                for u in users:
+                    new_grp.user_set.add(u)
+                # Get all contents and medias
+                for c in contents:
+                    cached_assign('view_content', new_grp, c)
+                    cached_assign('view_media', new_grp, c.media_obj)
+                
+            print("Indexing imported projects ...")
+            call_command('reindex', projects=True, no_content=True)
+            """
+        
+        # This is the end
+        print("This is the end")
+        
+        
\ No newline at end of file