src/ldt/ldt/management/commands/createmediacontent.py
author ymh <ymh.work@gmail.com>
Sun, 19 Jul 2015 23:40:47 +0200
changeset 1402 b248462918e0
parent 1357 dd3b4c9d5035
child 1497 c42847c334a0
permissions -rw-r--r--
Add https to absolute source path
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1357
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Apr 10, 2015
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
@author: ymh
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
import logging
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
from optparse import make_option
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
import time
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
import urlparse
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
from django.conf import settings
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
from django.contrib.auth import get_user_model
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
from django.contrib.auth.models import Group
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
from django.core.management.base import BaseCommand, CommandError
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
from guardian.shortcuts import remove_perm
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
from ldt.ldt_utils.models import Content, Media
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
from ldt.ldt_utils.stat import update_stat_content
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
from ldt.ldt_utils.utils import generate_uuid
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
from ldt.security.cache import cached_assign
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
from ldt.security.utils import assign_perm_to_obj
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
logger = logging.getLogger(__name__)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
class Command(BaseCommand):
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    formats = ["%M:%S", "%H:%M:%S", "%Hh%M", "%Ss", "%Ssec", "%Mmn %Ss"]
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    def get_duration(self, value):
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        dur = None
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        for f in self.formats:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
            try:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
                dur = time.strptime(value, f)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
                dur = (dur.tm_hour*3600 + dur.tm_min*60 + dur.tm_sec) * 1000
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
                break
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
            except:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
                pass
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        return dur
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    help = 'Create a content (and Media)'
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    option_list = BaseCommand.option_list + (
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        #TODO: source is compulsory
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        make_option("-s", "--source",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
                  dest="source",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
                  action="store",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
                  type="string",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
                  help="The media source"),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        #TODO: title is compulsory
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        make_option("-t", "--title",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
                  dest="title",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
                  action="store",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
                  type="string",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
                  help="The content title"),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        #TODO: optional
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        make_option("-V", "--videopath",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
                  dest="videopath",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
                  action="store",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
                  type="string",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
                  default=settings.STREAM_URL,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
                  help="The media videopath"),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        #TODO: optional
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        make_option("--media-title",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
                  dest="media_title",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
                  action="store",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
                  type="string",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
                  help="The media title. Optional, same as content title"),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        make_option("--description",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
                  dest="description",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
                  action="store",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
                  type="string",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
                  default='',
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
                  help="The description. Optional, same as content title"),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
        make_option("--media-description",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
                  dest="media_description",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
                  action="store",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
                  type="string",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
                  help="The media description. Optional, same as content description"),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        #TODO: optional
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
        make_option("-d", "--duration",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
                  dest="duration",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
                  action="store",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
                  type="string",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
                  help="Media duration"),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        make_option("-p", "--public",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
                    dest="public",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
                    action="store_true",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
                    default=False,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
                    help="Is the media/content public ?"
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
                    ),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
        make_option("-r", "--read",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
                    dest="readers",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
                    action="store",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
                    type="string",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
                    default=None,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
                    help="comma separated list of user/group with read access"
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
                    ),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        make_option("-w", "--write",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
                    dest="writers",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
                    action="store",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
                    type="string",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
                    default=None,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
                    help="comma separated list of user/group with write access"
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
                    ),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        make_option("-u", "--update",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
                    dest='update',
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
                    action="store_true",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
                    default=False,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
                    help="update objects when not created"
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
                    ),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
        make_option("-T", "--tags",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
                    dest='tags',
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
                    action="store",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
                    type="string",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
                    default='',
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
                    help="comma separated list of tags"
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
                    ),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
        make_option("-c", "--creator",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
                    dest='creator',
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
                    action="store",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
                    type="string",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
                    default=None,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
                    help="The handle for object creator."
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
                    ),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        make_option("--external-id",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
                    dest='external_id',
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
                    action="store",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
                    type="string",
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
                    default = None,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
                    help="The media external id"
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
                    ),
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
       )
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
    
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
    def handle(self, *args, **options):
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
        #BaseCommand.handle(self, *args, **options)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
        title = options.get('title', '')
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
        if not title:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
            raise CommandError("Title s compulsory")
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
        title = title.decode("utf-8")
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
        media_title = options.get('media_title', '')
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
        media_title = title if not media_title else media_title.decode("utf-8")
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
        src = options.get('source', None)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
        if src:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
            src = src.decode("utf-8")
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
        if not src:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
            raise CommandError("Source option is compulsory")
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
        videopath = options.get('videopath', settings.STREAM_URL)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
        url_parts = urlparse.urlparse(src)
1402
b248462918e0 Add https to absolute source path
ymh <ymh.work@gmail.com>
parents: 1357
diff changeset
   167
        if url_parts.scheme in ['http','rtmp', 'https']:
1357
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
            videopath = ''
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
        #TODO : test url is relative path
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
        duration_str = options.get('duration', '')
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
        duration = self.get_duration(duration_str)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
        if duration is None:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
            raise CommandError("Duration %r not recognized" % duration_str)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
        description = options.get('description', '')
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
        if description:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
            description  = description.decode("utf-8")
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
        media_description = options.get('media_description', '')
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
        if not media_description:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
            media_description = description
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
        else:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
            media_description.decode("utf-8")
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
        public = options.get('public', False)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
        tags_str = options.get('tags', '')
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
        if tags_str:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
            tags_str = tags_str.decode("utf-8")
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
        tags = filter(lambda t: t, [t.strip() for t in tags_str.split(",")]) if tags_str else []
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
        creator_str = options.get("creator", None)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
        User = get_user_model()
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
        creator = None
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
        if creator_str:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
            try:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
                creator = User.objects.get(username=creator_str)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
            except User.DoesNotExist:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
                raise CommandError("Creator user %s not found" % creator_str)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
        else:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
            #TODO: replace by first()
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
            try:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
                creator = User.objects.filter(is_superuser=True)[0]
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
            except IndexError:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
                creator = None
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
        if creator is None:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
            raise CommandError("Creator not found")
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
        readers_str = options.get("readers", "")
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
        writers_str = options.get("writers", "")
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
        readers = filter(lambda s: s, [r.strip() for r in  readers_str.split(',')]) if readers_str else []
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
        writers = filter(lambda s: s, [r.strip() for r in  writers_str.split(',')]) if writers_str else []
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
        readers_list = set(readers+writers)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
        if readers_list:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
            read_list = list(User.objects.filter(username__in=readers_list)) + list(Group.objects.filter(name__in=readers_list))
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
        else:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
            read_list = []
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
        if writers:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
            write_list = list(User.objects.filter(username__in = writers)) + list(Group.objects.filter(name__in = writers))
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
        else:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
            write_list = []
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
        external_id = options.get("external_id", None)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
        update_objects = options.get('update', False)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
        #TODO: wrap everything in transaction
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
        defaults_values = {
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
            'videopath': videopath,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
            'title': media_title,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
            'duration': duration,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
            'description': media_description,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
            'creator': creator,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
            'external_id': external_id
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
        }
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
        media, created = Media.objects.get_or_create(src=src, defaults= defaults_values)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
        if not created and update_objects:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
            for k,v in defaults_values.items():
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
                setattr(media, k, v)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
            media.save()
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
        if created or update_objects:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
            media.is_public = public
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
            assign_perm_to_obj(media, read_list, write_list, creator)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
        content_uuid = generate_uuid()
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
        content_iriurl = "%s/%s.iri" % (content_uuid, content_uuid);
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
        content = Content.objects.create(
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
            iri_id = content_uuid,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
            iriurl = content_iriurl,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
            media_obj=media,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
            title=title,
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
            description=description, 
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
            duration=duration
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
        )
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
        content.tags.add(*tags)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
        everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
        if public:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
            cached_assign('view_content', everyone, content)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
            cached_assign('view_media', everyone, media)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
        else:
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
            remove_perm('ldt_utils.view_media', everyone, media)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
            remove_perm('ldt_utils.view_content', everyone, content)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
            assign_perm_to_obj(content, read_list, write_list, creator)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
            assign_perm_to_obj(media, read_list, write_list, creator)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
        content.is_public = public
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
        content.save()
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
        assign_perm_to_obj(content, read_list, write_list, creator)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
        update_stat_content(content)
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
        
dd3b4c9d5035 add command to create media and content
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
        return content.iri_id