src/ldt/ldt/ldt_utils/models.py
author ymh <ymh.work@gmail.com>
Thu, 31 Oct 2013 11:56:12 +0100
changeset 1248 d8d4c721523b
parent 1199 b4559d14b9ac
child 1289 1f6844db7d4d
permissions -rwxr-xr-x
Remove xml declaration from ldt
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1248
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
     1
import datetime
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
     2
import mimetypes
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
     3
import os.path
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
     4
import re
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
     5
from shutil import move
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
     6
import uuid
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
     7
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
     8
from django.conf import settings
1191
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 1180
diff changeset
     9
from django.contrib.auth import get_user_model
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 1180
diff changeset
    10
from django.contrib.auth.models import Group
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 1180
diff changeset
    11
from django.core.files.storage import default_storage
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    12
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
    13
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
    14
from guardian.shortcuts import assign, remove_perm, get_perms
1248
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
    15
import lxml.etree #@UnresolvedImport
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
    16
from sorl.thumbnail import ImageField
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
    17
import tagging.fields
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
    18
from tagging.models import Tag
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
    19
128
503e365a3af7 Starting 'group_management' branch (CAUTION : STILL SOME BUGS)
cavaliet
parents: 125
diff changeset
    20
from ldt.core.models import Document
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
    21
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
    22
    get_current_user)
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
    23
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
    24
from ldt.security.models import SafeModel
1191
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 1180
diff changeset
    25
from ldt.utils import generate_hash, url as url_utils
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 1180
diff changeset
    26
from ldt.utils.web_url_management import get_web_url
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    27
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
    28
    generate_uuid)
1248
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
    29
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
    30
from .events import post_project_save
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
    31
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    32
1191
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 1180
diff changeset
    33
User = get_user_model()
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
    34
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
    35
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
    36
731
aba6c30b6d2a first step of mysql migration
ymh <ymh.work@gmail.com>
parents: 719
diff changeset
    37
    handle = models.CharField(max_length=255, unique=True, blank=True, null=True)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    38
    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
    39
    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
    40
    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
    41
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    42
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    43
        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
    44
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    45
    class Meta:
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    46
        permissions = (
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    47
                       ('view_author', 'Can view author'),
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    48
                       )
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    49
1141
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    50
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    51
class MediaManager(SafeManager):
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    52
    
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    53
    def __init__(self):
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    54
        super(MediaManager, self).__init__(check_perm=False)
1134
e31dd7ea24bb command to load and add data from another platform. Enable to add user,group,media,content,project and guardian permissions.
cavaliet
parents: 1117
diff changeset
    55
        
1141
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    56
    def get_by_natural_key(self, src_hash):
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    57
        return self.get(src_hash=src_hash)
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    58
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    59
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
    60
class Media(SafeModel):
1141
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    61
    objects = MediaManager()
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    62
    external_id = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('media.external_id'))
1117
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
    63
    external_permalink = models.URLField(max_length=1024, null=True, blank=True, verbose_name=_('media.external_permalink'))
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
    64
    external_publication_url = models.URLField(max_length=1024, null=True, blank=True, verbose_name=_('media.external_publication_url'))
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    65
    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
    66
    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
    67
    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
    68
    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
    69
    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
    70
    duration = models.IntegerField(null=True, blank=True, verbose_name=_('media.duration'))
1191
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 1180
diff changeset
    71
    creator = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, verbose_name=_('media.creator'))
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    72
    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
    73
    title = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('title'))
731
aba6c30b6d2a first step of mysql migration
ymh <ymh.work@gmail.com>
parents: 719
diff changeset
    74
    src = models.CharField(max_length=1024, verbose_name=_('media.src'))
733
43c9571752f6 correct models. Add blank to generated fields
ymh <ymh.work@gmail.com>
parents: 731
diff changeset
    75
    src_hash = models.CharField(max_length=128, unique=True, verbose_name=_('media.src_hash'), blank=True)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    76
    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
    77
    
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    78
    class Meta:
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    79
        permissions = (
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    80
                       ('view_media', 'Can view media'),
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    81
                       )
1141
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    82
    
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    83
    # Natural key management
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    84
    def natural_key(self):
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
    85
        return (self.src_hash,)
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
    86
        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    87
    def mimetype(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    88
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    89
            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
    90
                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
    91
            elif self.src:
150
fd591c597e6d correct mimetype problem
ymh <ymh.work@gmail.com>
parents: 128
diff changeset
    92
                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
    93
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    94
                return None
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 fset(self, value):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    97
            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
    98
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
    99
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   100
    mimetype = property(**mimetype())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   101
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   102
    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
   103
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   104
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   105
            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
   106
            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
   107
                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
   108
                res_src = {
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   109
                    '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
   110
                    '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
   111
                    '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
   112
                    '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
   113
                    '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
   114
                }.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
   115
            return res_src
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   116
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   117
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   118
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   119
    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
   120
    
795
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   121
    def is_public(): #@NoSelf
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   122
        
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   123
        def fget(self):
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   124
            if self.pk:
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   125
                everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   126
                if 'view_media' in get_perms(everyone, self):
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   127
                    return True
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   128
            return False
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   129
        
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   130
        def fset(self, value):
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   131
            if self.pk:
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   132
                everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   133
                if value:
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   134
                    assign('ldt_utils.view_media', everyone, self.media_obj)               
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   135
                else:
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   136
                    remove_perm('ldt_utils.view_media', everyone, self.media_obj)
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   137
        
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   138
        return locals()
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   139
    
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   140
    is_public = property(**is_public())
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   141
    
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   142
    def save(self, *args, **kwargs):
731
aba6c30b6d2a first step of mysql migration
ymh <ymh.work@gmail.com>
parents: 719
diff changeset
   143
        self.src_hash = generate_hash(self.src)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   144
        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
   145
        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
   146
            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
   147
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   148
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   149
        strings = []
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   150
        if self.title:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   151
            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
   152
        else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   153
            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
   154
        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
   155
            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
   156
        return "|".join(strings)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   157
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   158
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   159
class ContentManager(SafeManager):
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   160
    
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   161
    def __init__(self):
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   162
        super(ContentManager, self).__init__(check_perm=False)
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   163
        
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
   164
    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
   165
        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
   166
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
   167
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
   168
    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
   169
    
731
aba6c30b6d2a first step of mysql migration
ymh <ymh.work@gmail.com>
parents: 719
diff changeset
   170
    iri_id = models.CharField(max_length=255, unique=True, default=generate_uuid, verbose_name=_('content.iri_id'))
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   171
    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
   172
    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
   173
    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
   174
    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
   175
    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
   176
    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
   177
    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
   178
    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
   179
    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
   180
    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
   181
    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
   182
    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
   183
        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   184
    class Meta:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   185
        ordering = ["title"]
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 178
diff changeset
   186
        permissions = (
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   187
                       ('view_content', 'Can view content'),
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 178
diff changeset
   188
                       )
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   189
        
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   190
    def __init__(self, *args, **kwargs):
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   191
        
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   192
        super(Content, self).__init__(*args, **kwargs)
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   193
        
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   194
        if not hasattr(Content, 'pol_positive'):
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   195
            self.__add_polemic_attributes()
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   196
        
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   197
    def delete(self):
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   198
        super(Content, self).delete()
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   199
        iri_file_path = self.iri_file_path()
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   200
        thumbnail = os.path.join(settings.MEDIA_ROOT, unicode(self.image))
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   201
        if os.path.exists(iri_file_path):
908
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   202
            iri_dir = os.path.dirname(iri_file_path)
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   203
            temp = os.path.join(os.path.join(os.path.dirname(iri_dir), "temp"), self.iri_id)             
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   204
            try:
908
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   205
                move(iri_dir, temp)
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   206
            except Exception, e:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   207
                raise e
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   208
        if os.path.exists(thumbnail):
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   209
            if os.path.basename(thumbnail) != os.path.basename(settings.DEFAULT_CONTENT_ICON):
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   210
                temp_thumbnail = os.path.join(os.path.dirname(thumbnail), "temp")
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   211
                try:    
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   212
                    if not os.path.exists(temp_thumbnail):
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   213
                        os.makedirs(temp_thumbnail)
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   214
                    move(thumbnail, os.path.join(temp_thumbnail, os.path.basename(thumbnail)))            
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   215
                except Exception, e:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   216
                    raise e
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   217
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   218
    #del .iri, and .png from temp directory
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   219
    def commit(self):
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   220
        iri_file_path=self.iri_file_path()
908
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   221
        iri_dir = os.path.dirname(iri_file_path)
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   222
        temp = os.path.join(os.path.join(os.path.dirname(iri_dir), "temp"), self.iri_id)
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   223
        thumbnail = os.path.join(settings.MEDIA_ROOT, unicode(self.image))
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   224
        temp_thumbnail = os.path.join(os.path.dirname(thumbnail), "temp")
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   225
        if os.path.exists(temp):
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   226
            default_storage.delete(os.path.join(temp, os.path.basename(self.iriurl)))
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   227
            os.rmdir(temp)
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   228
        if os.path.exists(temp_thumbnail):
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   229
            default_storage.delete(os.path.join(temp_thumbnail, os.path.basename(thumbnail)))
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   230
            os.rmdir(temp_thumbnail)
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   231
        
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   232
    #move .iri, and .png to there original directory
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   233
    def rollback(self):
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   234
        iri_file_path=self.iri_file_path()
908
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   235
        iri_dir = os.path.dirname(iri_file_path)
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   236
        temp = os.path.join(os.path.join(os.path.dirname(iri_dir), "temp"), self.iri_id)
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   237
        thumbnail = os.path.join(settings.MEDIA_ROOT, unicode(self.image))
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   238
        temp_thumbnail = os.path.join(os.path.dirname(thumbnail), "temp")
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   239
        if os.path.exists(temp):
908
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   240
            move(temp, iri_dir)
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   241
            os.rmdir(os.path.dirname(temp))
1199
b4559d14b9ac little debug in content delete
cavaliet
parents: 1191
diff changeset
   242
        if os.path.exists(temp_thumbnail) and os.path.exists(os.path.join(temp_thumbnail, os.path.basename(thumbnail))):
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   243
            move(os.path.join(temp_thumbnail, os.path.basename(thumbnail)), os.path.dirname(thumbnail))
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   244
            os.rmdir(temp_thumbnail)
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
   245
    
1141
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
   246
    
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
   247
    def natural_key(self):
1141
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
   248
        return (self.iri_id,)
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
   249
    
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   250
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   251
    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
   252
        if self.duration is None:
1117
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   253
            doc = lxml.etree.parse(self.iri_file_path()) #@UndefinedVariable
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   254
            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
   255
            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
   256
                try:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   257
                    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
   258
                except:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   259
                    self.duration = 0
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   260
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   261
                self.duration = 0
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   262
            self.save()
344
76724cebff95 Merge with 631c0edee9ea995d0f2fa3b93742f715933c3800
verrierj
parents: 343 338
diff changeset
   263
        return self.duration    
76724cebff95 Merge with 631c0edee9ea995d0f2fa3b93742f715933c3800
verrierj
parents: 343 338
diff changeset
   264
    
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   265
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   266
    def mimetype(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   267
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   268
            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
   269
                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
   270
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   271
                return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   272
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   273
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   274
    mimetype = property(**mimetype())
718
5e27a39d3742 replace lucene by haystack, remove references to lucene
ymh <ymh.work@gmail.com>
parents: 715
diff changeset
   275
            
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   276
    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
   277
        # 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
   278
        created = False
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   279
        try:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   280
            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
   281
            if not os.path.exists(iri_file_path):
908
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   282
                iri_dir = os.path.dirname(iri_file_path)
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   283
                if not os.path.exists(iri_dir):
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   284
                    os.makedirs(iri_dir)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   285
                created = True
908
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   286
                iri_file = open(iri_file_path, "w")
03a00e8f2e3b Remove warnings
ymh <ymh.work@gmail.com>
parents: 873
diff changeset
   287
                create_empty_iri(iri_file, self, "IRI")
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   288
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   289
                created = False
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   290
                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
   291
                
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   292
        except Exception, e:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   293
            if created:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   294
                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
   295
                    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
   296
            raise e
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   297
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   298
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   299
    #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
   300
    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
   301
        
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   302
        create_front_project = False
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   303
        
844
1f69157772a1 bug correction on creation of content
rougeronj
parents: 840
diff changeset
   304
        # update it
1f69157772a1 bug correction on creation of content
rougeronj
parents: 840
diff changeset
   305
        self.sync_iri_file()        
1f69157772a1 bug correction on creation of content
rougeronj
parents: 840
diff changeset
   306
        
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   307
        if not self.pk:
844
1f69157772a1 bug correction on creation of content
rougeronj
parents: 840
diff changeset
   308
            create_front_project = True  
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 354
diff changeset
   309
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   310
        super(Content, self).save(*args, **kwargs)
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 354
diff changeset
   311
        
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   312
        if create_front_project: 
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   313
            # We need a primary key for self in create_project, so
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   314
            # save() has to be called first
392
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   315
            self.create_front_project()
491
b0da8cecb6b3 View annot_content always finds a project
verrierj
parents: 482
diff changeset
   316
            assign('ldt_utils.change_content', get_current_user(), self)
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   317
            
232
2878499a372b Security layer moved to middleware and ldt_utils __init__ file
verrierj
parents: 231
diff changeset
   318
            
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   319
    def __unicode__(self):
950
f08d8b3e78b8 Correct authentication on projects
ymh <ymh.work@gmail.com>
parents: 925
diff changeset
   320
        return str(self.id) + ":" + self.iri_id + ":" + self.title.replace("\n", " ")
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   321
        
1009
760f6643d4cf correction of a big bug concerning the get_web_url call in models
grandjoncl
parents: 1007
diff changeset
   322
    def iri_url(self, web_url=None):
760f6643d4cf correction of a big bug concerning the get_web_url call in models
grandjoncl
parents: 1007
diff changeset
   323
        if not web_url:
1010
a8a42538ba0e modification of get_web_url to have less dependances and be usable in ldt_utils->models
grandjoncl
parents: 1009
diff changeset
   324
            web_url=get_web_url()
922
cba34a867804 correct bug on indexation.
ymh <ymh.work@gmail.com>
parents: 908
diff changeset
   325
        if url_utils.is_absolute(self.iriurl):
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   326
            return self.iriurl
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   327
        else:
922
cba34a867804 correct bug on indexation.
ymh <ymh.work@gmail.com>
parents: 908
diff changeset
   328
            res_url = unicode(settings.MEDIA_URL) + u"ldt/" + unicode(self.iriurl)
cba34a867804 correct bug on indexation.
ymh <ymh.work@gmail.com>
parents: 908
diff changeset
   329
            if not url_utils.is_absolute(res_url):
925
fd2d4a7a5de6 - secure access to content and project
ymh <ymh.work@gmail.com>
parents: 922
diff changeset
   330
                res_url = unicode(web_url) + res_url
922
cba34a867804 correct bug on indexation.
ymh <ymh.work@gmail.com>
parents: 908
diff changeset
   331
            return res_url 
1043
34af1cdcf746 Starting 'modifications_relative_ldtxml' branch
grandjoncl
parents: 1011
diff changeset
   332
        
34af1cdcf746 Starting 'modifications_relative_ldtxml' branch
grandjoncl
parents: 1011
diff changeset
   333
    def relative_iri_url(self): #this function is called when we create a project 
34af1cdcf746 Starting 'modifications_relative_ldtxml' branch
grandjoncl
parents: 1011
diff changeset
   334
        res_url =  u"ldt/" + unicode(self.iriurl)
34af1cdcf746 Starting 'modifications_relative_ldtxml' branch
grandjoncl
parents: 1011
diff changeset
   335
        return res_url 
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   336
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   337
    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
   338
        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
   339
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   340
    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
   341
        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
   342
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   343
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   344
    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
   345
        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
   346
            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
   347
            return empty_media
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   348
        else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   349
            return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   350
            
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   351
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   352
    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
   353
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   354
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   355
            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
   356
                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
   357
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   358
                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
   359
                if empty_media:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   360
                    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
   361
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   362
                    return ""
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   363
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   364
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   365
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   366
    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
   367
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   368
    def videopath(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   369
        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
   370
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   371
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   372
            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
   373
                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
   374
                if empty_media:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   375
                    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
   376
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   377
                    return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   378
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   379
                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
   380
           
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   381
        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
   382
            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
   383
                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
   384
                      
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   385
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   386
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   387
    videopath = property(**videopath())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   388
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   389
    def src(): #@NoSelf
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   390
        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
   391
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   392
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   393
            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
   394
                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
   395
                if empty_media:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   396
                    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
   397
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   398
                    return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   399
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   400
                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
   401
           
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   402
        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
   403
            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
   404
                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
   405
                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
   406
                self.save()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   407
                      
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   408
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   409
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   410
    src = property(**src())
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   411
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   412
    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
   413
        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
   414
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   415
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   416
            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
   417
                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
   418
                if empty_media:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   419
                    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
   420
                else: 
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   421
                    return None
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   422
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   423
                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
   424
           
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   425
        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
   426
            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
   427
                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
   428
                      
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   429
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   430
       
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   431
    external_id = property(**external_id())
231
535ce952e51c Stub of html template for project creation containing a group selection
verrierj
parents: 229
diff changeset
   432
    
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   433
    def is_public(): #@NoSelf
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   434
        
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   435
        def fget(self):
271
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   436
            if self.pk:
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   437
                everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   438
                if 'view_content' in get_perms(everyone, self):
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   439
                    return True
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   440
            return False
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   441
        
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   442
        def fset(self, value):
271
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   443
            if self.pk:
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   444
                everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   445
                if value:
795
923429f142ea Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents: 741
diff changeset
   446
                    assign('ldt_utils.view_content', everyone, self)              
271
99347d5275b6 Contents can be shared with groups
verrierj
parents: 266
diff changeset
   447
                else:
458
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   448
                    remove_perm('ldt_utils.view_content', everyone, self)
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   449
        
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   450
        return locals()
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   451
    
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   452
    is_public = property(**is_public())
392
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   453
    
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   454
    def create_front_project(self):
491
b0da8cecb6b3 View annot_content always finds a project
verrierj
parents: 482
diff changeset
   455
        old_user = get_current_user_or_admin()
392
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   456
            
491
b0da8cecb6b3 View annot_content always finds a project
verrierj
parents: 482
diff changeset
   457
        if old_user.is_superuser:
b0da8cecb6b3 View annot_content always finds a project
verrierj
parents: 482
diff changeset
   458
            admin = old_user
392
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   459
        else:
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   460
            admin = User.objects.filter(is_superuser=True)[0]
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   461
            
65c1898141da Front projects are created by admins
verrierj
parents: 384
diff changeset
   462
        set_current_user(admin)
411
d126a67897c0 Small bugfixes
verrierj
parents: 409
diff changeset
   463
        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
   464
        self.front_project.publish(allow_write=True)            
d1be4a4f6639 Fix bugs in permissions
verrierj
parents: 505
diff changeset
   465
        self.save()
491
b0da8cecb6b3 View annot_content always finds a project
verrierj
parents: 482
diff changeset
   466
        set_current_user(old_user)
741
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   467
    
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   468
    
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   469
    def get_or_create_front_project(self):
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   470
        front_proj = self.front_project
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   471
        if front_proj:
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   472
            proj = front_proj
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   473
        else:
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   474
            # The main project for the content
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   475
            proj = Project.safe_objects.filter(contents__in=[self], state=2)
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   476
            if not proj:
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   477
                self.create_front_project()
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   478
                proj = self.front_project
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   479
            else:
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   480
                proj = proj[0]
45d814511958 Content PUT api endpoint to add an annotation to a content without giving a project id. #35
cavaliet
parents: 738
diff changeset
   481
        return proj
515
d1be4a4f6639 Fix bugs in permissions
verrierj
parents: 505
diff changeset
   482
409
e4855d669c55 First step of tag management for contents.
cavaliet
parents: 393
diff changeset
   483
    
e4855d669c55 First step of tag management for contents.
cavaliet
parents: 393
diff changeset
   484
    # Tag management
e4855d669c55 First step of tag management for contents.
cavaliet
parents: 393
diff changeset
   485
    def get_tags(self):
e4855d669c55 First step of tag management for contents.
cavaliet
parents: 393
diff changeset
   486
        return Tag.objects.get_for_object(self)
458
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   487
    
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   488
    
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   489
    # 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
   490
    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
   491
        for element in POL_INDICES.keys():
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   492
            if element.startswith('pol_'):                    
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   493
                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
   494
                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
   495
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   496
    def __make_getter(self, i):
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   497
        def inner_getter(self):
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   498
            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
   499
                return 0;
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   500
            else:                
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   501
                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
   502
                return l[POL_INDICES[i]]
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   503
        return inner_getter
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   504
        
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   505
    def __make_rate(self, i):
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   506
        def inner_rate(self):
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   507
            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
   508
                return 0
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   509
            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
   510
        return inner_rate   
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   511
    
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   512
                    
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   513
    def annotation_volume(): #@NoSelf        
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   514
        def fget(self):
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   515
            if self.stat_annotation is None:
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   516
                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
   517
            else:
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   518
                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
   519
                    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   520
        return locals()
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   521
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   522
    annotation_volume = property(**annotation_volume())
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   523
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   524
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   525
    def nb_annotations(): #@NoSelf
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   526
        def fget(self):
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   527
            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
   528
                return 0
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   529
            else:
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   530
                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
   531
            
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   532
        return locals()
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   533
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   534
    nb_annotations = property(**nb_annotations())
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   535
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   536
POL_INDICES = {
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   537
    'pol_positive' : 0,
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   538
    'pol_negative' : 1,
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   539
    'pol_reference' : 2,
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   540
    'pol_question' : 3,               
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   541
}    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   543
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   544
class ContentStat(models.Model):
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   545
    
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   546
    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
   547
        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
   548
        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
   549
            self.__init_empty_stat()
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   550
    
551
c447d863b6ad change stat_annotation to one to one field
ymh <ymh.work@gmail.com>
parents: 542
diff changeset
   551
    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
   552
    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
   553
    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
   554
    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
   555
    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
   556
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   557
    def __init_empty_stat(self):
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   558
        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
   559
        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
   560
        self.nb_annotations = 0
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   561
        self.last_annotated = None
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   562
        
470
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   563
    def __list2str(self, l):
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   564
        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
   565
        
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   566
    def __str2list(self, s):
b1dd78b59750 add all polemic attributes and polemic attribute rates on contents
verrierj
parents: 468
diff changeset
   567
        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
   568
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   569
    def annotation_volume(): #@NoSelf
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   570
        
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   571
        def fget(self):
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   572
            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
   573
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   574
        def fset(self, value):
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   575
            self.annotation_volume_str = self.__list2str(value)
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   576
            
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   577
        return locals()
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   578
    
482
c802e00c7131 Lot of bugfixes
verrierj
parents: 480
diff changeset
   579
    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
   580
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   581
    def polemics_volume(): #@NoSelf
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   582
        
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   583
        def fget(self):
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   584
            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
   585
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   586
        def fset(self, value):
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   587
            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
   588
            
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   589
        return locals()
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   590
    
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   591
    polemics_volume = property(**polemics_volume())
266
a35924820af7 Contents can be published for everyone
verrierj
parents: 264
diff changeset
   592
    
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   593
    
1072
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   594
class Project(Document, SafeModel):
542
54dfa397baa3 export stat to extarnal object. does not fully work
ymh <ymh.work@gmail.com>
parents: 521
diff changeset
   595
    
1072
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   596
    EDITION = 1
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   597
    PUBLISHED = 2
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   598
    MODERATED = 3
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   599
    REJECTED = 4
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   600
    DELETED = 5
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   601
      
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   602
    STATE_CHOICES = (
1074
36f657714851 optimize adding an annotation
ymh <ymh.work@gmail.com>
parents: 1072
diff changeset
   603
    (EDITION, 'edition'),
36f657714851 optimize adding an annotation
ymh <ymh.work@gmail.com>
parents: 1072
diff changeset
   604
    (PUBLISHED, 'published'),
36f657714851 optimize adding an annotation
ymh <ymh.work@gmail.com>
parents: 1072
diff changeset
   605
    (MODERATED, 'moderated'),
36f657714851 optimize adding an annotation
ymh <ymh.work@gmail.com>
parents: 1072
diff changeset
   606
    (REJECTED, 'rejected'),
36f657714851 optimize adding an annotation
ymh <ymh.work@gmail.com>
parents: 1072
diff changeset
   607
    (DELETED, 'deleted')
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   608
    )
731
aba6c30b6d2a first step of mysql migration
ymh <ymh.work@gmail.com>
parents: 719
diff changeset
   609
    ldt_id = models.CharField(max_length=255, unique=True)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   610
    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
   611
    title = models.CharField(max_length=1024)
467
a1e9f5e91791 Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents: 458
diff changeset
   612
    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
   613
    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
   614
    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
   615
    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
   616
    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
   617
    state = models.IntegerField(choices=STATE_CHOICES, default=1)
342
17d615b49a91 Extend image fields size to 200 characters + minor bugs
verrierj
parents: 341
diff changeset
   618
    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
   619
    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
   620
    
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   621
    class Meta:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   622
        ordering = ["title"]
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 178
diff changeset
   623
        permissions = (
231
535ce952e51c Stub of html template for project creation containing a group selection
verrierj
parents: 229
diff changeset
   624
                       ('view_project', 'Can view project'),
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 178
diff changeset
   625
                       )
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   626
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   627
    def __setattr__(self, name, value):
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   628
        super(Project, self).__setattr__(name,value)
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   629
        if name == "ldt" and hasattr(self, "__ldt_encoded"):
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   630
            del self.__ldt_encoded
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   631
        
1248
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
   632
    def get_xml_doc(self):
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
   633
        #remove the xml header declaration
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
   634
        return lxml.etree.fromstring(re.sub(r"^<\?\s*xml .*\?>", "", self.ldt))
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   635
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   636
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   637
        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
   638
    
1141
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
   639
    # added for import
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
   640
    def get_by_natural_key(self, ldt_id):
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
   641
        return self.get(ldt_id=ldt_id)
e0dbffc4a443 modifs from main head applied to the maintenance head
cavaliet
parents: 1011
diff changeset
   642
    
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   643
    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
   644
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   645
        if doc is None:
1248
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
   646
            doc =  self.get_xml_doc()#@UndefinedVariable
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   647
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   648
        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
   649
        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
   650
            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
   651
        else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   652
            return None
232
2878499a372b Security layer moved to middleware and ldt_utils __init__ file
verrierj
parents: 231
diff changeset
   653
        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   654
    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
   655
        def fget(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   656
            modes = []
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   657
            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
   658
                mimetype = content.mimetype
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   659
                if mimetype:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   660
                    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
   661
                    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
   662
                        modes.append(mode)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   663
                    else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   664
                        modes.append("video")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   665
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   666
                    modes.append("video")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   667
            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
   668
                if not current:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   669
                    return item
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   670
                elif current == item:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   671
                    return item
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   672
                else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   673
                    return "video"
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   674
            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
   675
        return locals()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   676
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   677
    stream_mode = property(**stream_mode())
1072
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   678
    
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   679
    def save(self, *args, **kwargs):
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   680
        
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   681
        must_reindex = kwargs.pop("must_reindex", True)
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   682
        super(Project, self).save(*args, **kwargs)
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   683
        
1074
36f657714851 optimize adding an annotation
ymh <ymh.work@gmail.com>
parents: 1072
diff changeset
   684
        post_project_save.send(self, instance=self, must_reindex = must_reindex)
1072
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   685
            
687dabdd25a7 Add an argument to project save to avoid indexation
ymh <ymh.work@gmail.com>
parents: 1011
diff changeset
   686
            
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   687
    @staticmethod
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 354
diff changeset
   688
    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
   689
#        owner = Owner.objects.get(user=user) #@UndefinedVariable
503e365a3af7 Starting 'group_management' branch (CAUTION : STILL SOME BUGS)
cavaliet
parents: 125
diff changeset
   690
        owner = user
178
4b83c370dc8a Added project description and tooltip
verrierj
parents: 167
diff changeset
   691
        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
   692
        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
   693
        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
   694
        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
   695
        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
   696
        project.save()
231
535ce952e51c Stub of html template for project creation containing a group selection
verrierj
parents: 229
diff changeset
   697
        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
   698
        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
   699
                        
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   700
        for content in contents:
480
5cc5afa71d7e Minor bugs
verrierj
parents: 470
diff changeset
   701
            project.contents.add(content)        
333
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 330
diff changeset
   702
        
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 330
diff changeset
   703
        if set_icon:
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 330
diff changeset
   704
            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
   705
        project.save()
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   706
               
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 354
diff changeset
   707
        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
   708
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   709
256
fd20ce3c5fbe Project are copied in the group they are displayed in group page
verrierj
parents: 247
diff changeset
   710
    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
   711
        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
   712
        project = copy_ldt(self, project, user)
229
fce9a02cc0a2 Basic access control for Projects
verrierj
parents: 228
diff changeset
   713
        assign('view_project', user, project)
fce9a02cc0a2 Basic access control for Projects
verrierj
parents: 228
diff changeset
   714
        assign('change_project', user, project)
256
fd20ce3c5fbe Project are copied in the group they are displayed in group page
verrierj
parents: 247
diff changeset
   715
        if group:
fd20ce3c5fbe Project are copied in the group they are displayed in group page
verrierj
parents: 247
diff changeset
   716
            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
   717
        for content in self.contents.all():
480
5cc5afa71d7e Minor bugs
verrierj
parents: 470
diff changeset
   718
            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
   719
        return project
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   720
    
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   721
    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
   722
        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
   723
            self.save()
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   724
        self.state = 2
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   725
        everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   726
        assign('ldt_utils.view_project', everyone, self)
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   727
        if allow_write:
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   728
            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
   729
        self.save()
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   730
        
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   731
    def unpublish(self):
349
63f729155d81 Enhance search and front template : add begin and duration to searched segments.
cavaliet
parents: 344
diff changeset
   732
        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
   733
            self.save()
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   734
        self.state = 1
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   735
        everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   736
        remove_perm('ldt_utils.view_project', everyone, self)
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   737
        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
   738
        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
   739
        
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
   740
        
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
   741
    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
   742
        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
   743
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
   744
        for content in self.contents.all():
458
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   745
            add_image = False
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   746
            try:
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   747
                current_image = content.image.file.name
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   748
            except IOError:
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   749
                add_image = True
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   750
            
5d35581192cb Little cleanup for migrations
verrierj
parents: 424
diff changeset
   751
            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
   752
                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
   753
                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
   754
            
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
   755
        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
   756
        return False
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 244
diff changeset
   757
    
167
fe00e7302efe Change class and functions names to follow PEP8 formatting standards
verrierj
parents: 150
diff changeset
   758
    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
   759
        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
   760
            return True
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   761
        else:
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 342
diff changeset
   762
            return False
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   763
        
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   764
    def has_annotations(self):
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   765
        nb_annot = 0
1248
d8d4c721523b Remove xml declaration from ldt
ymh <ymh.work@gmail.com>
parents: 1199
diff changeset
   766
        doc = self.get_xml_doc()
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   767
        res = doc.xpath("/iri/annotations/content/ensemble/decoupage")
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   768
        for r in res:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   769
            nb_annot = nb_annot + r.find('elements').__len__()
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   770
        if nb_annot == 0:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   771
            return False
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   772
        else:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   773
            return True
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 810
diff changeset
   774
        
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   775
    def ldt_encoded(): #@NoSelf
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   776
        
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   777
        def fget(self):
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   778
            if self.ldt is None:
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   779
                return None
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   780
            if not hasattr(self, "__ldt_encoded"):
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   781
                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
   782
                if m and m.group(1):
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   783
                    encoding = m.group(1)
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   784
                else:
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   785
                    encoding = 'utf-8'
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   786
                self.__ldt_encoded = self.ldt.encode(encoding)
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   787
            return self.__ldt_encoded
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   788
            
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   789
        return locals()
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   790
    
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   791
    ldt_encoded = property(**ldt_encoded())
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 551
diff changeset
   792
                
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   793
    
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   794
    
240
a46cb257d8ee Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents: 235
diff changeset
   795
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
   796
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   797
    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
   798
    content = models.ForeignKey(Content)
731
aba6c30b6d2a first step of mysql migration
ymh <ymh.work@gmail.com>
parents: 719
diff changeset
   799
    project_id = models.CharField(max_length=255, unique=False, blank=True, null=True, db_index=True)
aba6c30b6d2a first step of mysql migration
ymh <ymh.work@gmail.com>
parents: 719
diff changeset
   800
    iri_id = models.CharField(max_length=255, unique=False, db_index=True)
aba6c30b6d2a first step of mysql migration
ymh <ymh.work@gmail.com>
parents: 719
diff changeset
   801
    ensemble_id = models.CharField(max_length=512, unique=False, db_index=True)
aba6c30b6d2a first step of mysql migration
ymh <ymh.work@gmail.com>
parents: 719
diff changeset
   802
    cutting_id = models.CharField(max_length=512, unique=False, db_index=True)    
aba6c30b6d2a first step of mysql migration
ymh <ymh.work@gmail.com>
parents: 719
diff changeset
   803
    element_id = models.CharField(max_length=512, unique=False, db_index=True)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   804
    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
   805
    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
   806
    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
   807
    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
   808
    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
   809
    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
   810
    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
   811
    polemics = models.IntegerField(null=True, blank=True, default=0)
733
43c9571752f6 correct models. Add blank to generated fields
ymh <ymh.work@gmail.com>
parents: 731
diff changeset
   812
    id_hash = models.CharField(max_length=128, unique=True, blank=True)
810
e7546394653c add audio annotation to segment api and correct reindex command.
cavaliet
parents: 795
diff changeset
   813
    audio_src = models.CharField(max_length=255, unique=False, null=True, blank=True)
e7546394653c add audio annotation to segment api and correct reindex command.
cavaliet
parents: 795
diff changeset
   814
    audio_href = models.CharField(max_length=512, unique=False, null=True, blank=True)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   815
    
1117
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   816
    @classmethod
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   817
    def create(cls, **kwargs):
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   818
        seg = cls(**kwargs)
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   819
        seg.set_hash()
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   820
        return seg
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   821
    
570
361ecea621e0 Little clean up for search
verrierj
parents: 560
diff changeset
   822
    # All combinations of polemic hashtags can be represented by a combination of 
361ecea621e0 Little clean up for search
verrierj
parents: 560
diff changeset
   823
    # 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
   824
    # and convert the resulting string into an integer to store the polemic values.
361ecea621e0 Little clean up for search
verrierj
parents: 560
diff changeset
   825
    # mask contains all possible polemic values
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   826
    mask = {
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   827
            'OK': set([8,9,10,11,12,13,14,15]),
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   828
            'KO': set([4,5,6,7,12,13,14,15]),
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   829
            'Q': set([2,3,6,7,10,11,14,15]),
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   830
            'REF': set([1,3,5,7,9,11,13,15]),
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   831
            }
1117
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   832
        
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   833
    def is_polemic(self, polemic_keyword): # OK, KO, Q, REF 
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   834
        if self.polemics in self.mask[polemic_keyword]:
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   835
            return True
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   836
        return False
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   837
    
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   838
    def get_polemic(self, polemic_keywords):
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   839
        value = set(range(16))
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   840
        
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   841
        for keyword in self.mask.keys():
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   842
            if keyword in polemic_keywords:
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   843
                value = value.intersection(self.mask[keyword])
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   844
            else:
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   845
                value.difference_update(self.mask[keyword])
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   846
            
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   847
        return value.pop()
738
51294f20e8ad Correct south migrations
ymh <ymh.work@gmail.com>
parents: 733
diff changeset
   848
    
1117
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   849
    def set_hash(self):
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   850
        try:
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   851
            self.id_hash = generate_hash(self.__unicode__())
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   852
        except AttributeError:
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   853
            self.id_hash = None
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   854
            
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   855
    def __unicode__(self):
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   856
        return "/".join((
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   857
            unicode(self.project_id if self.project_id is not None else ""),
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   858
            unicode(self.iri_id if self.iri_id is not None else ""),
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   859
            unicode(self.ensemble_id if self.ensemble_id is not None else ""),
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   860
            unicode(self.cutting_id if self.cutting_id is not None else ""),
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   861
            unicode(self.element_id if self.element_id is not None else "")
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   862
            ))
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   863
            
731
aba6c30b6d2a first step of mysql migration
ymh <ymh.work@gmail.com>
parents: 719
diff changeset
   864
    def save(self, *args, **kwargs):
1117
3bab1e42acfa - update haystack
ymh <ymh.work@gmail.com>
parents: 1081
diff changeset
   865
        self.set_hash()                
731
aba6c30b6d2a first step of mysql migration
ymh <ymh.work@gmail.com>
parents: 719
diff changeset
   866
        super(Segment, self).save(*args, **kwargs)
468
d1ff0694500b Add polemic properties to contents
verrierj
parents: 467
diff changeset
   867
    
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   868
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 106
diff changeset
   869
    class Meta:
244
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   870
        permissions = (
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   871
                       ('view_segment', 'Can view segment'),
bc1dd5fea0b6 Added permissions to all objects in ldt_utils
verrierj
parents: 240
diff changeset
   872
                       )