src/ldt/ldt/ldt_utils/models.py
author ymh <ymh.work@gmail.com>
Thu, 19 Jul 2012 19:21:05 +0200
changeset 715 f21459554182
parent 658 5f22830ffcd7
child 718 5e27a39d3742
permissions -rwxr-xr-x
Remove lucene dependancies in model
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 _
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
     5
from guardian.shortcuts import assign, remove_perm, get_perms
128
503e365a3af7 Starting 'group_management' branch (CAUTION : STILL SOME BUGS)
cavaliet
parents: 125
diff changeset
     6
from ldt.core.models import Document
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
     7
from ldt.security import (get_current_user_or_admin, set_current_user, 
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
     8
    get_current_user)
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
     9
from ldt.security.manager import SafeManager
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
    10
from ldt.security.models import SafeModel
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
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
    12
from tagging.models import Tag
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    13
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
    14
    generate_uuid)
354
ecd4d57d0b40 Last annotated contents can be retrieved using the field content.last_annotated
verrierj
parents: 349
diff changeset
    15
import datetime
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
    16
import ldt.indexation
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    17
import lxml.etree
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    18
import mimetypes
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    19
import os.path
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
    20
import re
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    21
import tagging.fields
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    22
import uuid
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
    23
#from ldt.core.models import Document, Owner
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    24
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
    25
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
    26
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
    27
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    28
    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
    29
    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
    30
    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
    31
    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
    32
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    33
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    34
        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
    35
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    36
    class Meta:
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    37
        permissions = (
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    38
                       ('view_author', 'Can view author'),
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    39
                       )
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    40
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
    41
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
    42
    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
    43
    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
    44
    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
    45
    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
    46
    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
    47
    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
    48
    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
    49
    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
    50
    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
    51
    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
    52
    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
    53
    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
    54
    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
    55
    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
    56
    
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    57
    class Meta:
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    58
        permissions = (
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    59
                       ('view_media', 'Can view media'),
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    60
                       )
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    61
        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    62
    def mimetype(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    63
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    64
            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
    65
                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
    66
            elif self.src:
150
fd591c597e6d correct mimetype problem
ymh <ymh.work@gmail.com>
parents: 128
diff changeset
    67
                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
    68
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    69
                return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    70
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    71
        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
    72
            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
    73
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    74
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    75
    mimetype = property(**mimetype())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    76
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    77
    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
    78
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    79
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    80
            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
    81
            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
    82
                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
    83
                res_src = {
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    84
                    '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
    85
                    '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
    86
                    '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
    87
                    '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
    88
                    '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
    89
                }.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
    90
            return res_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
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    93
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    94
    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
    95
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    96
    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
    97
        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
    98
        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
    99
            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
   100
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   101
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   102
        strings = []
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   103
        if self.title:
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.title))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   105
        else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   106
            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
   107
        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
   108
            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
   109
        return "|".join(strings)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   110
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   111
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   112
class ContentManager(SafeManager):
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   113
    
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   114
    def __init__(self):
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   115
        super(ContentManager, self).__init__(check_perm=False)
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   116
        
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
    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
   118
        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
   119
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
   120
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
   121
    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
   122
    
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   123
    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
   124
    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
   125
    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
   126
    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
   127
    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
   128
    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
   129
    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
   130
    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
   131
    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
   132
    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
   133
    media_obj = models.ForeignKey('Media', blank=True, null=True)
658
5f22830ffcd7 correct absolute path in images fields. add data migration
ymh <ymh.work@gmail.com>
parents: 570
diff changeset
   134
    image = ImageField(upload_to="thumbnails/contents/", default=settings.DEFAULT_CONTENT_ICON, max_length=200)
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 354
diff changeset
   135
    front_project = models.ForeignKey('Project', null=True, blank=True)
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
   136
        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   137
    class Meta:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   138
        ordering = ["title"]
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 178
diff changeset
   139
        permissions = (
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   140
                       ('view_content', 'Can view content'),
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 178
diff changeset
   141
                       )
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   142
        
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   143
    def __init__(self, *args, **kwargs):
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   144
        
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   145
        super(Content, self).__init__(*args, **kwargs)
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   146
        
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   147
        if not hasattr(Content, 'pol_positive'):
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   148
            self.__add_polemic_attributes()
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
   149
    
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
   150
    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
   151
        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
   152
        
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
   153
    # 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
   154
    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
   155
        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
   156
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   157
    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
   158
        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
   159
            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
   160
            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
   161
            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
   162
                try:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   163
                    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
   164
                except:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   165
                    self.duration = 0
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   166
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   167
                self.duration = 0
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   168
            self.save()
344
76724cebff95 Merge with 631c0edee9ea995d0f2fa3b93742f715933c3800
verrierj
parents: 343 338
diff changeset
   169
        return self.duration    
76724cebff95 Merge with 631c0edee9ea995d0f2fa3b93742f715933c3800
verrierj
parents: 343 338
diff changeset
   170
    
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   171
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   172
    def mimetype(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   173
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   174
            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
   175
                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
   176
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   177
                return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   178
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   179
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   180
    mimetype = property(**mimetype())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   181
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   182
    def delete(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   183
        super(Content, self).delete()
715
f21459554182 Remove lucene dependancies in model
ymh <ymh.work@gmail.com>
parents: 658
diff changeset
   184
        ldt.indexation.delete_document("iri_id",self.iri_id)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   185
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   186
    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
   187
        # 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
   188
        created = False
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   189
        try:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   190
            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
   191
            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
   192
                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
   193
                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
   194
                    os.makedirs(dir)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   195
                created = True
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   196
                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
   197
                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
   198
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   199
                created = False
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   200
                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
   201
                
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   202
        except Exception, e:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   203
            if created:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   204
                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
   205
                    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
   206
            raise e
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   207
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   208
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   209
    #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
   210
    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
   211
        
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   212
        create_front_project = False
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   213
        # update it
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   214
        self.sync_iri_file()
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   215
        
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   216
        if not self.pk:
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   217
            create_front_project = True
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   218
              
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 354
diff changeset
   219
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   220
        super(Content, self).save(*args, **kwargs)
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 354
diff changeset
   221
        
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   222
        if create_front_project: 
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   223
            # We need a primary key for self in create_project, so
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   224
            # save() has to be called first
392
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   225
            self.create_front_project()
491
b0da8cecb6b3 View annot_content always finds a project
verrierj
parents: 482
diff changeset
   226
            assign('ldt_utils.change_content', get_current_user(), self)
b0da8cecb6b3 View annot_content always finds a project
verrierj
parents: 482
diff changeset
   227
232
2878499a372b Security layer moved to middleware and ldt_utils __init__ file
verrierj
parents: 231
diff changeset
   228
            
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   229
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   230
        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
   231
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   232
    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
   233
        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
   234
            return self.iriurl
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   235
        else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   236
            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
   237
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   238
    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
   239
        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
   240
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   241
    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
   242
        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
   243
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
    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
   246
        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
   247
            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
   248
            return empty_media
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   249
        else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   250
            return None
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
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   253
    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
   254
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   255
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   256
            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
   257
                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
   258
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   259
                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
   260
                if empty_media:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   261
                    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
   262
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   263
                    return ""
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   264
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   265
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   266
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   267
    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
   268
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   269
    def videopath(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   270
        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
   271
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   272
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   273
            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
   274
                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
   275
                if empty_media:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   276
                    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
   277
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   278
                    return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   279
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   280
                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
   281
           
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   282
        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
   283
            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
   284
                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
   285
                      
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   286
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   287
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   288
    videopath = property(**videopath())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   289
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   290
    def src(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   291
        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
   292
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   293
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   294
            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
   295
                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
   296
                if empty_media:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   297
                    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
   298
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   299
                    return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   300
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   301
                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
   302
           
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   303
        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
   304
            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
   305
                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
   306
                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
   307
                self.save()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   308
                      
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   309
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   310
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   311
    src = property(**src())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   312
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   313
    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
   314
        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
   315
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   316
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   317
            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
   318
                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
   319
                if empty_media:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   320
                    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
   321
                else: 
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   322
                    return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   323
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   324
                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
   325
           
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   326
        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
   327
            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
   328
                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
   329
                      
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   330
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   331
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   332
    external_id = property(**external_id())
231
535ce952e51c Stub of html template for project creation containing a group selection
verrierj
parents: 229
diff changeset
   333
    
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   334
    def is_public(): #@NoSelf
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   335
        
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   336
        def fget(self):
271
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   337
            if self.pk:
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   338
                everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   339
                if 'view_content' in get_perms(everyone, self):
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   340
                    return True
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   341
            return False
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   342
        
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   343
        def fset(self, value):
271
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   344
            if self.pk:
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   345
                everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   346
                if value:
458
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   347
                    assign('ldt_utils.view_content', everyone, self) 
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   348
                    assign('ldt_utils.view_media', everyone, self.media_obj)               
271
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   349
                else:
458
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   350
                    remove_perm('ldt_utils.view_content', everyone, self)
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   351
                    remove_perm('ldt_utils.view_media', everyone, self.media_obj)
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   352
        
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   353
        return locals()
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   354
    
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   355
    is_public = property(**is_public())
392
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   356
    
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   357
    def create_front_project(self):
491
b0da8cecb6b3 View annot_content always finds a project
verrierj
parents: 482
diff changeset
   358
        old_user = get_current_user_or_admin()
392
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   359
            
491
b0da8cecb6b3 View annot_content always finds a project
verrierj
parents: 482
diff changeset
   360
        if old_user.is_superuser:
b0da8cecb6b3 View annot_content always finds a project
verrierj
parents: 482
diff changeset
   361
            admin = old_user
392
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   362
        else:
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   363
            admin = User.objects.filter(is_superuser=True)[0]
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   364
            
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   365
        set_current_user(admin)
411
d126a67897c0 Small bugfixes
verrierj
parents: 409
diff changeset
   366
        self.front_project = Project.create_project(admin, 'front project : %s' % self.title, [self], cuttings=['chapitrage', 'contributions'] )
515
d1be4a4f6639 Fix bugs in permissions
verrierj
parents: 505
diff changeset
   367
        self.front_project.publish(allow_write=True)            
d1be4a4f6639 Fix bugs in permissions
verrierj
parents: 505
diff changeset
   368
        self.save()
491
b0da8cecb6b3 View annot_content always finds a project
verrierj
parents: 482
diff changeset
   369
        set_current_user(old_user)
515
d1be4a4f6639 Fix bugs in permissions
verrierj
parents: 505
diff changeset
   370
409
e4855d669c55 First step of tag management for contents.
cavaliet
parents: 393
diff changeset
   371
    
e4855d669c55 First step of tag management for contents.
cavaliet
parents: 393
diff changeset
   372
    # Tag management
e4855d669c55 First step of tag management for contents.
cavaliet
parents: 393
diff changeset
   373
    def get_tags(self):
e4855d669c55 First step of tag management for contents.
cavaliet
parents: 393
diff changeset
   374
        return Tag.objects.get_for_object(self)
458
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   375
    
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   376
    
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   377
    # add polemic attributes and polemic attribute rates to class Content
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   378
    def __add_polemic_attributes(self):
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   379
        for element in POL_INDICES.keys():
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   380
            if element.startswith('pol_'):                    
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   381
                Content.add_to_class(element, property(self.__make_getter(element)))
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   382
                Content.add_to_class("%s_rate" % element, property(self.__make_rate(element)))  
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   383
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   384
    def __make_getter(self, i):
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   385
        def inner_getter(self):
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   386
            if self.stat_annotation is None:
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   387
                return 0;
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   388
            else:                
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   389
                l = self.stat_annotation.polemics_volume
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   390
                return l[POL_INDICES[i]]
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   391
        return inner_getter
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   392
        
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   393
    def __make_rate(self, i):
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   394
        def inner_rate(self):
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   395
            if self.stat_annotation is None or self.stat_annotation.nb_annotations <= 0:
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   396
                return 0
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   397
            return int(getattr(self, i) / float(self.stat_annotation.nb_annotations) * 100 )
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   398
        return inner_rate   
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   399
    
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   400
                    
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   401
    def annotation_volume(): #@NoSelf        
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   402
        def fget(self):
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   403
            if self.stat_annotation is None:
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   404
                return [0]*settings.DIVISIONS_FOR_STAT_ANNOTATION
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   405
            else:
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   406
                return self.stat_annotation.annotation_volume
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   407
                    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   408
        return locals()
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   409
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   410
    annotation_volume = property(**annotation_volume())
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   411
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   412
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   413
    def nb_annotations(): #@NoSelf
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   414
        def fget(self):
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   415
            if self.stat_annotation is None:
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   416
                return 0
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   417
            else:
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   418
                return self.stat_annotation.nb_annotations
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   419
            
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   420
        return locals()
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   421
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   422
    nb_annotations = property(**nb_annotations())
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   423
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   424
POL_INDICES = {
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   425
    'pol_positive' : 0,
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   426
    'pol_negative' : 1,
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   427
    'pol_reference' : 2,
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   428
    'pol_question' : 3,               
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   429
}    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   430
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   431
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   432
class ContentStat(models.Model):
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   433
    
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   434
    def __init__(self, *args, **kwargs):                
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   435
        super(ContentStat, self).__init__(*args, **kwargs)
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   436
        if self.annotation_volume_str is None and self.polemics_volume_str is None:
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   437
            self.__init_empty_stat()
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   438
    
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   439
    content = models.OneToOneField(Content, blank=False, null=False, related_name='stat_annotation', verbose_name=_("content_stat.content"), unique=True, db_index=True)
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   440
    annotation_volume_str = models.CommaSeparatedIntegerField(max_length=1024, null=True, blank=True, verbose_name=_("content_stat.annotations_volume"))
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   441
    polemics_volume_str = models.CommaSeparatedIntegerField(max_length=1024, null=True, blank=True, verbose_name=_("content_stat.polemics_volume"))
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   442
    nb_annotations = models.IntegerField(null=False, blank=False, verbose_name=_('content.nb_annotation'), default=0, db_index=True)
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   443
    last_annotated = models.DateTimeField(default=datetime.datetime.now, verbose_name=_('content.last_annotated'), blank=True, null=True) #@UndefinedVariable
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   444
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   445
    def __init_empty_stat(self):
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   446
        self.annotation_volume_str = ','.join(['0']*settings.DIVISIONS_FOR_STAT_ANNOTATION)
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   447
        self.polemics_volume_str = ','.join(['0']*len(settings.SYNTAX.keys()))
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   448
        self.nb_annotations = 0
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   449
        self.last_annotated = None
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   450
        
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   451
    def __list2str(self, l):
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   452
        return ','.join([str(c) for c in l])
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   453
        
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   454
    def __str2list(self, s):
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   455
        return [int(x) for x in s.split(',')] 
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   456
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   457
    def annotation_volume(): #@NoSelf
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   458
        
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   459
        def fget(self):
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   460
            return self.__str2list(self.annotation_volume_str)
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   461
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   462
        def fset(self, value):
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   463
            self.annotation_volume_str = self.__list2str(value)
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   464
            
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   465
        return locals()
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   466
    
482
c802e00c7131 Lot of bugfixes
verrierj
parents: 480
diff changeset
   467
    annotation_volume = property(**annotation_volume())
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   468
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   469
    def polemics_volume(): #@NoSelf
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   470
        
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   471
        def fget(self):
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   472
            return self.__str2list(self.polemics_volume_str)
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   473
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   474
        def fset(self, value):
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   475
            self.polemics_volume_str = self.__list2str(value)
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   476
            
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   477
        return locals()
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   478
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   479
    polemics_volume = property(**polemics_volume())
344
76724cebff95 Merge with 631c0edee9ea995d0f2fa3b93742f715933c3800
verrierj
parents: 343 338
diff changeset
   480
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   481
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   482
    
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   483
    
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
   484
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
   485
    STATE_CHOICES = (
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   486
    (1, 'edition'),
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   487
    (2, 'published'),
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   488
    (3, 'moderated'),
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   489
    (4, 'rejected'),
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   490
    (5, 'deleted')
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   491
    )
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   492
    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
   493
    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
   494
    title = models.CharField(max_length=1024)
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 458
diff changeset
   495
    contents = models.ManyToManyField(Content)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   496
    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
   497
    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
   498
    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
   499
    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
   500
    state = models.IntegerField(choices=STATE_CHOICES, default=1)
342
17d615b49a91 Extend image fields size to 200 characters + minor bugs
verrierj
parents: 341
diff changeset
   501
    description = models.TextField(null=True, blank=True)    
658
5f22830ffcd7 correct absolute path in images fields. add data migration
ymh <ymh.work@gmail.com>
parents: 570
diff changeset
   502
    image = ImageField(upload_to="thumbnails/projects/", default=settings.DEFAULT_PROJECT_ICON, max_length=200)
314
1a8620e5ebb0 Add memcached and sorl thumbnail pour thumbnail management. Set default pict on content, project and user.
cavaliet
parents: 286
diff changeset
   503
    
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   504
    class Meta:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   505
        ordering = ["title"]
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 178
diff changeset
   506
        permissions = (
231
535ce952e51c Stub of html template for project creation containing a group selection
verrierj
parents: 229
diff changeset
   507
                       ('view_project', 'Can view project'),
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 178
diff changeset
   508
                       )
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   509
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   510
    def __setattr__(self, name, value):
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   511
        super(Project, self).__setattr__(name,value)
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   512
        if name == "ldt" and hasattr(self, "__ldt_encoded"):
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   513
            del self.__ldt_encoded
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   514
        
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   515
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   516
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   517
        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
   518
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   519
    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
   520
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   521
        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
   522
            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
   523
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   524
        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
   525
        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
   526
            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
   527
        else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   528
            return None
232
2878499a372b Security layer moved to middleware and ldt_utils __init__ file
verrierj
parents: 231
diff changeset
   529
        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   530
    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
   531
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   532
            modes = []
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   533
            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
   534
                mimetype = content.mimetype
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   535
                if mimetype:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   536
                    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
   537
                    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
   538
                        modes.append(mode)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   539
                    else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   540
                        modes.append("video")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   541
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   542
                    modes.append("video")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   543
            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
   544
                if not current:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   545
                    return item
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   546
                elif current == item:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   547
                    return item
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   548
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   549
                    return "video"
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   550
            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
   551
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   552
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   553
    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
   554
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   555
    @staticmethod
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 354
diff changeset
   556
    def create_project(user, title, contents, description='', groups=[], set_icon=True, cuttings=[]):
128
503e365a3af7 Starting 'group_management' branch (CAUTION : STILL SOME BUGS)
cavaliet
parents: 125
diff changeset
   557
#        owner = Owner.objects.get(user=user) #@UndefinedVariable
503e365a3af7 Starting 'group_management' branch (CAUTION : STILL SOME BUGS)
cavaliet
parents: 125
diff changeset
   558
        owner = user
178
4b83c370dc8a Added project description and tooltip
verrierj
parents: 167
diff changeset
   559
        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
   560
        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
   561
        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
   562
        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
   563
        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
   564
        project.save()
231
535ce952e51c Stub of html template for project creation containing a group selection
verrierj
parents: 229
diff changeset
   565
        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
   566
        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
   567
                        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   568
        for content in contents:
480
5cc5afa71d7e Minor bugs
verrierj
parents: 470
diff changeset
   569
            project.contents.add(content)        
333
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 330
diff changeset
   570
        
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 330
diff changeset
   571
        if set_icon:
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 330
diff changeset
   572
            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
   573
        project.save()
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   574
               
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 354
diff changeset
   575
        return create_ldt(project, user, cuttings)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   576
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   577
256
fd20ce3c5fbe Project are copied in the group they are displayed in group page
verrierj
parents: 247
diff changeset
   578
    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
   579
        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
   580
        project = copy_ldt(self, project, user)
229
fce9a02cc0a2 Basic access control for Projects
verrierj
parents: 228
diff changeset
   581
        assign('view_project', user, project)
fce9a02cc0a2 Basic access control for Projects
verrierj
parents: 228
diff changeset
   582
        assign('change_project', user, project)
256
fd20ce3c5fbe Project are copied in the group they are displayed in group page
verrierj
parents: 247
diff changeset
   583
        if group:
fd20ce3c5fbe Project are copied in the group they are displayed in group page
verrierj
parents: 247
diff changeset
   584
            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
   585
        for content in self.contents.all():
480
5cc5afa71d7e Minor bugs
verrierj
parents: 470
diff changeset
   586
            project.contents.add(content)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   587
        return project
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   588
    
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   589
    def publish(self, allow_write=False):
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
   590
        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
   591
            self.save()
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   592
        self.state = 2
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   593
        everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   594
        assign('ldt_utils.view_project', everyone, self)
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   595
        if allow_write:
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   596
            assign('ldt_utils.change_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
   597
        self.save()
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   598
        
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   599
    def unpublish(self):
349
63f729155d81 Enhance search and front template : add begin and duration to searched segments.
cavaliet
parents: 344
diff changeset
   600
        if not self.pk:
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
   601
            self.save()
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   602
        self.state = 1
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   603
        everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   604
        remove_perm('ldt_utils.view_project', everyone, self)
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   605
        remove_perm('ldt_utils.change_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
   606
        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
   607
        
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   608
        
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
   609
    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
   610
        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
   611
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
   612
        for content in self.contents.all():
458
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   613
            add_image = False
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   614
            try:
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   615
                current_image = content.image.file.name
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   616
            except IOError:
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   617
                add_image = True
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   618
            
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   619
            if add_image or current_image != default_image:
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
   620
                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
   621
                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
   622
            
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
   623
        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
   624
        return False
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   625
    
167
fe00e7302efe Change class and functions names to follow PEP8 formatting standards
verrierj
parents: 150
diff changeset
   626
    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
   627
        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
   628
            return True
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   629
        else:
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 342
diff changeset
   630
            return False
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   631
        
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   632
    def ldt_encoded(): #@NoSelf
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   633
        
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   634
        def fget(self):
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   635
            if self.ldt is None:
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   636
                return None
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   637
            if not hasattr(self, "__ldt_encoded"):
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   638
                m = re.match("<\?xml.*?encoding\s*?=\s*?['\"]([A-Za-z][A-Za-z0-9._-]*)['\"].*?\?>", self.ldt.lstrip(), re.I|re.M|re.U)
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   639
                if m and m.group(1):
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   640
                    encoding = m.group(1)
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   641
                else:
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   642
                    encoding = 'utf-8'
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   643
                self.__ldt_encoded = self.ldt.encode(encoding)
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   644
            return self.__ldt_encoded
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   645
            
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   646
        return locals()
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   647
    
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   648
    ldt_encoded = property(**ldt_encoded())
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   649
                
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   650
    
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   651
    
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
   652
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
   653
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   654
    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
   655
    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
   656
    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
   657
    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
   658
    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
   659
    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
   660
    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
   661
    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
   662
    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
   663
    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
   664
    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
   665
    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
   666
    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
   667
    abstract = models.TextField(null=True, blank=True)
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 458
diff changeset
   668
    polemics = models.IntegerField(null=True, blank=True, default=0)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   669
    
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   670
    
570
361ecea621e0 Little clean up for search
verrierj
parents: 560
diff changeset
   671
    # All combinations of polemic hashtags can be represented by a combination of 
361ecea621e0 Little clean up for search
verrierj
parents: 560
diff changeset
   672
    # 4 bits, 1 if the hashtag is in the tweet, 0 else. We use the order OK, KO, Q, REF
361ecea621e0 Little clean up for search
verrierj
parents: 560
diff changeset
   673
    # and convert the resulting string into an integer to store the polemic values.
361ecea621e0 Little clean up for search
verrierj
parents: 560
diff changeset
   674
    # mask contains all possible polemic values
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   675
    mask = {
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   676
            'OK': set([8,9,10,11,12,13,14,15]),
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   677
            'KO': set([4,5,6,7,12,13,14,15]),
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   678
            'Q': set([2,3,6,7,10,11,14,15]),
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   679
            'REF': set([1,3,5,7,9,11,13,15]),
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   680
            }
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   681
    
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   682
    def is_polemic(self, polemic_keyword): # OK, KO, Q, REF 
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   683
        if self.polemics in self.mask[polemic_keyword]:
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   684
            return True
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   685
        return False
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   686
    
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   687
    def get_polemic(self, polemic_keywords):
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   688
        value = set(range(16))
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   689
        
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   690
        for keyword in self.mask.keys():
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   691
            if keyword in polemic_keywords:
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   692
                value = value.intersection(self.mask[keyword])
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   693
            else:
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   694
                value.difference_update(self.mask[keyword])
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   695
            
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   696
        return value.pop()
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   697
        
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   698
    
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   699
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   700
        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
   701
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   702
    class Meta:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   703
        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
   704
        permissions = (
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   705
                       ('view_segment', 'Can view segment'),
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   706
                       )
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   707
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   708
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 458
diff changeset
   709