src/ldt/ldt/ldt_utils/models.py
author verrierj
Fri, 06 Jan 2012 17:46:23 +0100
changeset 340 5f919a978f50
parent 333 4ddf8c0eeab4
child 341 9d74fc6c3244
permissions -rwxr-xr-x
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
     1
from django.conf import settings
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
     2
from django.contrib.auth.models import User, Group
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
     3
from django.db import models
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
     4
from django.utils.translation import ugettext_lazy as _
128
503e365a3af7 Starting 'group_management' branch (CAUTION : STILL SOME BUGS)
cavaliet
parents: 125
diff changeset
     5
#from ldt.core.models import Document, Owner
503e365a3af7 Starting 'group_management' branch (CAUTION : STILL SOME BUGS)
cavaliet
parents: 125
diff changeset
     6
from ldt.core.models import Document
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
     7
from guardian.shortcuts import assign, remove_perm, get_perms
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
     8
import ldt.indexation
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
     9
from ldt.security.models import SafeModel
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
    10
from ldt.security.manager import SafeManager
314
1a8620e5ebb0 Add memcached and sorl thumbnail pour thumbnail management. Set default pict on content, project and user.
cavaliet
parents: 286
diff changeset
    11
from sorl.thumbnail import ImageField
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    12
from utils import (create_ldt, copy_ldt, create_empty_iri, update_iri, 
330
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
    13
    generate_uuid)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    14
import lucene
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    15
import lxml.etree
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    16
import mimetypes
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    17
import os.path
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    18
import tagging.fields
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    19
import uuid
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    20
330
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
    21
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
    22
class Author(SafeModel):
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    23
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    24
    handle = models.CharField(max_length=512, unique=True, blank=True, null=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    25
    email = models.EmailField(unique=False, blank=True, null=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    26
    firstname = models.CharField(max_length=512, blank=True, null=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    27
    lastname = models.CharField(max_length=512, blank=True, null=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    28
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    29
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    30
        return unicode(self.id) + " - " + self.handle + ", " + self.email + ", " + self.firstname + " " + self.lastname
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    31
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    32
    class Meta:
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    33
        permissions = (
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    34
                       ('view_author', 'Can view author'),
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    35
                       )
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    36
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
    37
class Media(SafeModel):
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    38
    external_id = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('media.external_id'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    39
    external_permalink = models.URLField(max_length=1024, verify_exists=False, null=True, blank=True, verbose_name=_('media.external_permalink'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    40
    external_publication_url = models.URLField(max_length=1024, verify_exists=True, null=True, blank=True, verbose_name=_('media.external_publication_url'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    41
    external_src_url = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('media.external_src_url'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    42
    creation_date = models.DateTimeField(auto_now_add=True, verbose_name=_('media.creation_date'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    43
    media_creation_date = models.DateTimeField(null=True, blank=True, verbose_name=_('media.media_creation_date'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    44
    update_date = models.DateTimeField(auto_now=True, verbose_name=_('media.update_date'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    45
    videopath = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('media.videopath'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    46
    duration = models.IntegerField(null=True, blank=True, verbose_name=_('media.duration'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    47
    creator = models.ForeignKey(User, blank=True, null=True, verbose_name=_('media.creator'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    48
    description = models.TextField(null=True, blank=True, verbose_name=_('description'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    49
    title = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('title'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    50
    src = models.CharField(max_length=1024, unique=True, verbose_name=_('media.src'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    51
    mimetype_field = models.CharField(max_length=512, null=True, blank=True, verbose_name=_('media.mimetype'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    52
    
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    53
    class Meta:
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    54
        permissions = (
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    55
                       ('view_media', 'Can view media'),
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    56
                       )
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    57
        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    58
    def mimetype(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    59
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    60
            if self.mimetype_field :
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    61
                return self.mimetype_field
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    62
            elif self.src:
150
fd591c597e6d correct mimetype problem
ymh <ymh.work@gmail.com>
parents: 128
diff changeset
    63
                return mimetypes.guess_type(self.src.rstrip())[0]
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    64
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    65
                return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    66
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    67
        def fset(self, value):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    68
            self.mimetype_field = value
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    69
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    70
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    71
    mimetype = property(**mimetype())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    72
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    73
    def stream_src(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    74
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    75
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    76
            res_src = self.src.rstrip()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    77
            if self.videopath and self.videopath.startswith("rtmp://") and "mp3:" not in res_src and "mp4:" not in res_src:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    78
                extension = res_src.split(".")[-1]
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    79
                res_src = {
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    80
                    'flv': lambda s: s,
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    81
                    'mp3': lambda s: "%s:%s" % ("mp3", res_src[:-4]),
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    82
                    'mp4': lambda s: "%s:%s" % ("mp4", res_src),
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    83
                    'f4v': lambda s: "%s:%s" % ("mp4", res_src),
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    84
                    'mov': lambda s: "%s:%s" % ("mp4", res_src),
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    85
                }.get(extension, lambda s:s)(res_src)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    86
            return res_src
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    87
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    88
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    89
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    90
    stream_src = property(**stream_src())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    91
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    92
    def save(self, *args, **kwargs):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    93
        super(Media, self).save(*args, **kwargs)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    94
        for content in self.content_set.all():
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    95
            content.sync_iri_file()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    96
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    97
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    98
        strings = []
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    99
        if self.title:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   100
            strings.append(unicode(self.title))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   101
        else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   102
            strings.append(unicode(self.src))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   103
        if self.external_id:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   104
            strings.append(unicode(self.external_id))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   105
        return "|".join(strings)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   106
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   107
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   108
class ContentManager(SafeManager):
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   109
    
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   110
    def __init__(self):
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   111
        super(ContentManager, self).__init__(check_perm=False)
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   112
        
125
5c3ed9c919bb New search feature on top right corner. Advanced search included in published projects tab. Colored icons. Change css to avoid scroll on contents and projects table.
cavaliet
parents: 112
diff changeset
   113
    def get_by_natural_key(self, iri_id):
5c3ed9c919bb New search feature on top right corner. Advanced search included in published projects tab. Colored icons. Change css to avoid scroll on contents and projects table.
cavaliet
parents: 112
diff changeset
   114
        return self.get(iri_id=iri_id)
5c3ed9c919bb New search feature on top right corner. Advanced search included in published projects tab. Colored icons. Change css to avoid scroll on contents and projects table.
cavaliet
parents: 112
diff changeset
   115
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
   116
class Content(SafeModel):
125
5c3ed9c919bb New search feature on top right corner. Advanced search included in published projects tab. Colored icons. Change css to avoid scroll on contents and projects table.
cavaliet
parents: 112
diff changeset
   117
    objects = ContentManager()
5c3ed9c919bb New search feature on top right corner. Advanced search included in published projects tab. Colored icons. Change css to avoid scroll on contents and projects table.
cavaliet
parents: 112
diff changeset
   118
    
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   119
    iri_id = models.CharField(max_length=1024, unique=True, default=generate_uuid, verbose_name=_('content.iri_id'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   120
    iriurl = models.CharField(max_length=1024, verbose_name=_('content.iriurl'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   121
    creation_date = models.DateTimeField(auto_now_add=True, verbose_name=_('content.creation_date'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   122
    update_date = models.DateTimeField(auto_now=True, verbose_name=_('content.update_date'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   123
    title = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('content.title'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   124
    description = models.TextField(null=True, blank=True, verbose_name=_('content.description'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   125
    authors = models.ManyToManyField(Author, blank=True, verbose_name=_('content.authors'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   126
    duration = models.IntegerField(null=True, blank=True, verbose_name=_('content.duration'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   127
    content_creation_date = models.DateTimeField(null=True, blank=True, verbose_name=_('content.content_creation_date'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   128
    tags = tagging.fields.TagField(max_length=2048, null=True, blank=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   129
    media_obj = models.ForeignKey('Media', blank=True, null=True)
330
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   130
    image = ImageField(upload_to=settings.MEDIA_ROOT+"thumbnails/contents/", default=settings.DEFAULT_CONTENT_ICON)
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   131
    stat_annotation = models.CommaSeparatedIntegerField(max_length=1024, null=True, blank=True, verbose_name=_("content.stat_annotation")) 
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   132
        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   133
    class Meta:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   134
        ordering = ["title"]
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 178
diff changeset
   135
        permissions = (
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   136
                       ('view_content', 'Can view content'),
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 178
diff changeset
   137
                       )
125
5c3ed9c919bb New search feature on top right corner. Advanced search included in published projects tab. Colored icons. Change css to avoid scroll on contents and projects table.
cavaliet
parents: 112
diff changeset
   138
    
5c3ed9c919bb New search feature on top right corner. Advanced search included in published projects tab. Colored icons. Change css to avoid scroll on contents and projects table.
cavaliet
parents: 112
diff changeset
   139
    def natural_key(self):
5c3ed9c919bb New search feature on top right corner. Advanced search included in published projects tab. Colored icons. Change css to avoid scroll on contents and projects table.
cavaliet
parents: 112
diff changeset
   140
        return self.iri_id
5c3ed9c919bb New search feature on top right corner. Advanced search included in published projects tab. Colored icons. Change css to avoid scroll on contents and projects table.
cavaliet
parents: 112
diff changeset
   141
        
5c3ed9c919bb New search feature on top right corner. Advanced search included in published projects tab. Colored icons. Change css to avoid scroll on contents and projects table.
cavaliet
parents: 112
diff changeset
   142
    # added for import
5c3ed9c919bb New search feature on top right corner. Advanced search included in published projects tab. Colored icons. Change css to avoid scroll on contents and projects table.
cavaliet
parents: 112
diff changeset
   143
    def get_by_natural_key(self, iri_id):
5c3ed9c919bb New search feature on top right corner. Advanced search included in published projects tab. Colored icons. Change css to avoid scroll on contents and projects table.
cavaliet
parents: 112
diff changeset
   144
        return self.get(iri_id=iri_id)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   145
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   146
    def get_duration(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   147
        if self.duration is None:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   148
            doc = lxml.etree.parse(self.iri_file_path())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   149
            res = doc.xpath("/iri/body/medias/media[@id='video']/video")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   150
            if len(res) > 0:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   151
                try:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   152
                    self.duration = int(res[0].get(u'dur', 0) or 0)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   153
                except:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   154
                    self.duration = 0
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   155
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   156
                self.duration = 0
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   157
            self.save()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   158
        return self.duration
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   159
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   160
    def mimetype(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   161
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   162
            if self.media_obj:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   163
                return self.media_obj.mimetype
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   164
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   165
                return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   166
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   167
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   168
    mimetype = property(**mimetype())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   169
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   170
    def delete(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   171
        super(Content, self).delete()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   172
        writer = ldt.indexation.get_writer()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   173
        try:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   174
            writer.deleteDocuments(lucene.Term("iri_id", self.iri_id))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   175
            writer.commit()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   176
        finally:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   177
            writer.close()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   178
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   179
    def sync_iri_file(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   180
        # create iri file if needed
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   181
        created = False
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   182
        try:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   183
            iri_file_path = self.iri_file_path()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   184
            if not os.path.exists(iri_file_path):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   185
                dir = os.path.dirname(iri_file_path)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   186
                if not os.path.exists(dir):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   187
                    os.makedirs(dir)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   188
                created = True
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   189
                file = open(iri_file_path, "w")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   190
                create_empty_iri(file, self, "IRI")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   191
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   192
                created = False
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   193
                update_iri(iri_file_path, self, "IRI")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   194
                
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   195
        except Exception, e:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   196
            if created:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   197
                if os.path.exists(iri_file_path):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   198
                    os.remove(iri_file_path)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   199
            raise e
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   200
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   201
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   202
    #TODO: better manage the change in .iri name and error scenario (save in temp file + rename
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   203
    def save(self, *args, **kwargs):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   204
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   205
        self.sync_iri_file()        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   206
        # update it 
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   207
        super(Content, self).save(*args, **kwargs)
232
2878499a372b Security layer moved to middleware and ldt_utils __init__ file
verrierj
parents: 231
diff changeset
   208
            
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   209
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   210
        return str(self.id) + ": " + self.iri_id
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   211
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   212
    def iri_url(self, web_url=settings.WEB_URL):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   213
        if 'http' in self.iriurl or 'https' in self.iriurl:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   214
            return self.iriurl
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   215
        else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   216
            return unicode(web_url) + unicode(settings.MEDIA_URL) + u"ldt/" + unicode(self.iriurl)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   217
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   218
    def iri_file_path(self):
112
9886ab183b09 add permalink + corrcetion config with static and media path. update urls
ymh <ymh.work@gmail.com>
parents: 111
diff changeset
   219
        return os.path.join(os.path.join(os.path.join(settings.MEDIA_ROOT, "ldt"), self.iri_id), os.path.basename(self.iriurl))
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   220
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   221
    def iri_url_template(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   222
        return "${web_url}${media_url}ldt/" + unicode(self.iri_id) + "/" + os.path.basename(self.iriurl)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   223
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   224
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   225
    def __get_empty_media(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   226
        if settings.EMPTY_MEDIA_EXTERNALID:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   227
            empty_media = Media.objects.get(external_id=settings.EMPTY_MEDIA_EXTERNALID)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   228
            return empty_media
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   229
        else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   230
            return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   231
            
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   232
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   233
    def stream_src(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   234
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   235
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   236
            if self.media_obj is not None:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   237
                return self.media_obj.stream_src
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   238
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   239
                empty_media = self.__get_empty_media()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   240
                if empty_media:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   241
                    return empty_media.stream_src
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   242
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   243
                    return ""
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   244
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   245
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   246
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   247
    stream_src = property(**stream_src())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   248
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   249
    def videopath(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   250
        doc = """simulate videopath""" #@UnusedVariable
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   251
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   252
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   253
            if self.media_obj is None:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   254
                empty_media = self.__get_empty_media()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   255
                if empty_media:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   256
                    return empty_media.videopath
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   257
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   258
                    return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   259
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   260
                return self.media_obj.videopath
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   261
           
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   262
        def fset(self, value):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   263
            if self.media_obj is not None:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   264
                self.media_obj.videopath = value
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   265
                      
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   266
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   267
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   268
    videopath = property(**videopath())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   269
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   270
    def src(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   271
        doc = """simulate videopath""" #@UnusedVariable
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   272
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   273
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   274
            if self.media_obj is None:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   275
                empty_media = self.__get_empty_media()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   276
                if empty_media:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   277
                    return empty_media.src
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   278
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   279
                    return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   280
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   281
                return self.media_obj.src
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   282
           
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   283
        def fset(self, value):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   284
            if self.media_obj is None or self.media_obj.src != value:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   285
                media, created = Media.objects.get_or_create(src=value, defaults={'src':value})                #@UnusedVariable
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   286
                self.media_obj = media
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   287
                self.save()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   288
                      
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   289
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   290
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   291
    src = property(**src())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   292
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   293
    def external_id(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   294
        doc = """simulate externalid""" #@UnusedVariable
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   295
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   296
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   297
            if self.media_obj is None:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   298
                empty_media = self.__get_empty_media()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   299
                if empty_media:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   300
                    return empty_media.external_id
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   301
                else: 
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   302
                    return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   303
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   304
                return self.media_obj.external_id
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   305
           
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   306
        def fset(self, value):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   307
            if self.media_obj is not None:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   308
                self.media_obj.external_id = value
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   309
                      
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   310
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   311
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   312
    external_id = property(**external_id())
231
535ce952e51c Stub of html template for project creation containing a group selection
verrierj
parents: 229
diff changeset
   313
    
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   314
    def is_public(): #@NoSelf
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   315
        
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   316
        def fget(self):
271
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   317
            if self.pk:
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   318
                everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   319
                if 'view_content' in get_perms(everyone, self):
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   320
                    return True
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   321
            return False
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   322
        
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   323
        def fset(self, value):
271
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   324
            if self.pk:
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   325
                everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   326
                if value:
274
80375a7b7e14 Improve permissions of medias + fix css
verrierj
parents: 271
diff changeset
   327
                    assign('view_content', everyone, self) 
282
7512c33b64be Add cache to userlist
verrierj
parents: 274
diff changeset
   328
                    assign('view_media', everyone, self.media_obj)               
271
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   329
                else:
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   330
                    remove_perm('view_content', everyone, self)
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   331
                    remove_perm('view_media', everyone, self.media_obj)
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   332
        
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   333
        return locals()
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   334
    
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   335
    is_public = property(**is_public())
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   336
    
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   337
    def nb_annotation(): #@NoSelf
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   338
        
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   339
        def fget(self):
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   340
            if self.stat_annotation:
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   341
                return sum([int(x) for x in self.stat_annotation.split(',')])
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   342
            else:
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   343
                return 0
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   344
            
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   345
        return locals()
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   346
        
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   347
    nb_annotation = property(**nb_annotation())
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 333
diff changeset
   348
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   349
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   350
    
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
   351
class Project(Document, SafeModel):  
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   352
    STATE_CHOICES = (
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   353
    (1, 'edition'),
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   354
    (2, 'published'),
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   355
    (3, 'moderated'),
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   356
    (4, 'rejected'),
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   357
    (5, 'deleted')
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   358
    )
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   359
    ldt_id = models.CharField(max_length=1024, unique=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   360
    ldt = models.TextField(null=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   361
    title = models.CharField(max_length=1024)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   362
    contents = models.ManyToManyField(Content)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   363
    creation_date = models.DateTimeField(auto_now_add=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   364
    modification_date = models.DateTimeField(auto_now=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   365
    created_by = models.CharField(_("created by"), max_length=70)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   366
    changed_by = models.CharField(_("changed by"), max_length=70)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   367
    state = models.IntegerField(choices=STATE_CHOICES, default=1)
178
4b83c370dc8a Added project description and tooltip
verrierj
parents: 167
diff changeset
   368
    description = models.TextField(null=True, blank=True)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   369
    
330
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   370
    image = ImageField(upload_to=settings.MEDIA_ROOT+"thumbnails/projects/", default=settings.DEFAULT_PROJECT_ICON)
314
1a8620e5ebb0 Add memcached and sorl thumbnail pour thumbnail management. Set default pict on content, project and user.
cavaliet
parents: 286
diff changeset
   371
    
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   372
    class Meta:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   373
        ordering = ["title"]
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 178
diff changeset
   374
        permissions = (
231
535ce952e51c Stub of html template for project creation containing a group selection
verrierj
parents: 229
diff changeset
   375
                       ('view_project', 'Can view project'),
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 178
diff changeset
   376
                       )
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   377
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   378
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   379
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   380
        return unicode(self.id) + u"::" + unicode(self.ldt_id) + u"::" + unicode(self.title)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   381
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   382
    def get_description(self, doc=None):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   383
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   384
        if doc is None:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   385
            doc = lxml.etree.fromstring(self.ldt)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   386
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   387
        res = doc.xpath("/iri/project")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   388
        if len(res) > 0:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   389
            return res[0].get(u'abstract')
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   390
        else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   391
            return None
232
2878499a372b Security layer moved to middleware and ldt_utils __init__ file
verrierj
parents: 231
diff changeset
   392
        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   393
    def stream_mode(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   394
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   395
            modes = []
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   396
            for content in self.contents.all():
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   397
                mimetype = content.mimetype
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   398
                if mimetype:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   399
                    mode = mimetype.split("/")[0]
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   400
                    if "audio" == mode or "video" == mode:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   401
                        modes.append(mode)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   402
                    else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   403
                        modes.append("video")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   404
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   405
                    modes.append("video")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   406
            def filter_video(current, item):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   407
                if not current:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   408
                    return item
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   409
                elif current == item:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   410
                    return item
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   411
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   412
                    return "video"
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   413
            return reduce(filter_video, modes)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   414
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   415
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   416
    stream_mode = property(**stream_mode())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   417
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   418
    @staticmethod
333
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 330
diff changeset
   419
    def create_project(user, title, contents, description='', groups=[], set_icon=True):
128
503e365a3af7 Starting 'group_management' branch (CAUTION : STILL SOME BUGS)
cavaliet
parents: 125
diff changeset
   420
#        owner = Owner.objects.get(user=user) #@UndefinedVariable
503e365a3af7 Starting 'group_management' branch (CAUTION : STILL SOME BUGS)
cavaliet
parents: 125
diff changeset
   421
        owner = user
178
4b83c370dc8a Added project description and tooltip
verrierj
parents: 167
diff changeset
   422
        project = Project(title=title, owner=owner, description=description)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   423
        project.ldt_id = str(uuid.uuid1()) #@UndefinedVariable
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   424
        project.created_by = user.username
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   425
        project.changed_by = user.username
330
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   426
        project.state = 1                        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   427
        project.save()
231
535ce952e51c Stub of html template for project creation containing a group selection
verrierj
parents: 229
diff changeset
   428
        assign('view_project', user, project)
330
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   429
        assign('change_project', user, project)           
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   430
                        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   431
        for content in contents:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   432
            project.contents.add(content)
333
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 330
diff changeset
   433
        
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 330
diff changeset
   434
        if set_icon:
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 330
diff changeset
   435
            project.set_icon()
330
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   436
        project.save()
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   437
                        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   438
        return create_ldt(project, user)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   439
256
fd20ce3c5fbe Project are copied in the group they are displayed in group page
verrierj
parents: 247
diff changeset
   440
    def copy_project(self, user, title, description='', group=None):
fd20ce3c5fbe Project are copied in the group they are displayed in group page
verrierj
parents: 247
diff changeset
   441
        project = Project(title=title, owner=user, description=description)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   442
        project = copy_ldt(self, project, user)
229
fce9a02cc0a2 Basic access control for Projects
verrierj
parents: 228
diff changeset
   443
        assign('view_project', user, project)
fce9a02cc0a2 Basic access control for Projects
verrierj
parents: 228
diff changeset
   444
        assign('change_project', user, project)
256
fd20ce3c5fbe Project are copied in the group they are displayed in group page
verrierj
parents: 247
diff changeset
   445
        if group:
fd20ce3c5fbe Project are copied in the group they are displayed in group page
verrierj
parents: 247
diff changeset
   446
            assign('view_project', group, project)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   447
        for content in self.contents.all():
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   448
            project.contents.add(content)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   449
        return project
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   450
    
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   451
    def publish(self):
330
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   452
        if not self.pk:
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   453
            self.save()
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   454
        self.state = 2
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   455
        everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   456
        assign('ldt_utils.view_project', everyone, self)
330
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   457
        self.save()
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   458
        
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   459
    def unpublish(self):
330
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   460
        if not self.pk():
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   461
            self.save()
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   462
        self.state = 1
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   463
        everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   464
        remove_perm('ldt_utils.view_project', everyone, self)
330
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   465
        self.save()
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   466
        
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   467
    def set_icon(self):
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   468
        default_image = os.path.basename(settings.DEFAULT_CONTENT_ICON)
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   469
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   470
        for content in self.contents.all():
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   471
            if os.path.basename(content.image.file.name) != default_image:
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   472
                self.image = content.image
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   473
                return True
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   474
            
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   475
        self.image = settings.DEFAULT_PROJECT_ICON
806188af5027 Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
verrierj
parents: 314
diff changeset
   476
        return False
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   477
    
167
fe00e7302efe Change class and functions names to follow PEP8 formatting standards
verrierj
parents: 150
diff changeset
   478
    def check_access(self, user):
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   479
        if (user and user.is_staff) or self.state == 2: 
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   480
            return True
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   481
        else:
256
fd20ce3c5fbe Project are copied in the group they are displayed in group page
verrierj
parents: 247
diff changeset
   482
            return False        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   483
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   484
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
   485
class Segment(SafeModel):
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   486
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   487
    project_obj = models.ForeignKey(Project, null=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   488
    content = models.ForeignKey(Content)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   489
    project_id = models.CharField(max_length=1024, unique=False, blank=True, null=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   490
    iri_id = models.CharField(max_length=1024, unique=False)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   491
    ensemble_id = models.CharField(max_length=1024, unique=False)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   492
    cutting_id = models.CharField(max_length=1024, unique=False)    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   493
    element_id = models.CharField(max_length=1024, unique=False)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   494
    tags = tagging.fields.TagField(max_length=2048, null=True, blank=True, unique=False)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   495
    title = models.CharField(max_length=2048, unique=False, null=True, blank=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   496
    duration = models.IntegerField(null=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   497
    start_ts = models.IntegerField(null=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   498
    author = models.CharField(max_length=1024, unique=False, null=True, blank=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   499
    date = models.CharField(max_length=128, unique=False, null=True, blank=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   500
    abstract = models.TextField(null=True, blank=True)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   501
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   502
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   503
        return "/".join((unicode(self.project_id), unicode(self.iri_id), unicode(self.ensemble_id), unicode(self.cutting_id), unicode(self.element_id)))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   504
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   505
    class Meta:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   506
        unique_together = (('project_id', 'iri_id', 'ensemble_id', 'cutting_id', 'element_id'),)
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   507
        permissions = (
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   508
                       ('view_segment', 'Can view segment'),
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   509
                       )
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   510
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   511