src/ldt/ldt/ldt_utils/views/content.py
author rougeronj
Tue, 06 Nov 2012 14:58:51 +0100
changeset 898 94ad1a394732
parent 883 0d9c00ae2755
child 899 2f1fa9b1aa58
permissions -rw-r--r--
update of error management in the creation of content
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
     1
from django.conf import settings
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
     2
from django.contrib.auth.decorators import login_required
319
c45aed3c7a89 Remove unused imports in views
verrierj
parents: 315
diff changeset
     3
from django.contrib.auth.models import Group
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
     4
from django.core.urlresolvers import reverse
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
     5
from django.forms.models import model_to_dict
792
e4180c2d11eb Modifications in order to download the thumbnail picture and put it as default picture when creating youtube, dailymotion or vimeo contents
grandjoncl
parents: 791
diff changeset
     6
from django.core.files import File
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
     7
from django.db import transaction
792
e4180c2d11eb Modifications in order to download the thumbnail picture and put it as default picture when creating youtube, dailymotion or vimeo contents
grandjoncl
parents: 791
diff changeset
     8
#from django.core.files.temp import NamedTemporaryFile
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
     9
from django.forms.util import ErrorList
814
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
    10
from django.http import HttpResponse, HttpResponseRedirect
791
12ba862f33fa Creation of a page instead of pop up for the content creation, update and suppression
grandjoncl
parents: 786
diff changeset
    11
from django.shortcuts import render_to_response, redirect
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    12
from django.template import RequestContext
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    13
from django.utils.translation import ugettext as _, ungettext
319
c45aed3c7a89 Remove unused imports in views
verrierj
parents: 315
diff changeset
    14
from ldt.ldt_utils.forms import ContentForm, MediaForm
c45aed3c7a89 Remove unused imports in views
verrierj
parents: 315
diff changeset
    15
from guardian.shortcuts import remove_perm
404
4adc42ab55fd Fix minor bugs
verrierj
parents: 384
diff changeset
    16
from ldt.ldt_utils.models import Content, Media, Project
384
0e410517b311 Front projects are hidden from user interface
verrierj
parents: 383
diff changeset
    17
from ldt.security.utils import assign_perm_to_obj, add_change_attr, get_userlist, get_userlist_model
319
c45aed3c7a89 Remove unused imports in views
verrierj
parents: 315
diff changeset
    18
from ldt.security.cache import cached_assign
325
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 321
diff changeset
    19
from ldt.user.forms import PictureForm
710
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
    20
from tagging.models import Tag, TaggedItem
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    21
import ldt.utils.path as ldt_utils_path
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    22
import logging
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    23
import mimetypes
792
e4180c2d11eb Modifications in order to download the thumbnail picture and put it as default picture when creating youtube, dailymotion or vimeo contents
grandjoncl
parents: 791
diff changeset
    24
import os, stat
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    25
import urllib2
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    26
import subprocess
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    27
import re
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    28
import datetime
644
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
    29
import math
779
129b1c8b0088 Commit Louise :
Louise Grandjonc <louise.grandjonc@iri.centrepompidou.fr>
parents: 734
diff changeset
    30
import requests
129b1c8b0088 Commit Louise :
Louise Grandjonc <louise.grandjonc@iri.centrepompidou.fr>
parents: 734
diff changeset
    31
import django.utils.simplejson as simplejson
792
e4180c2d11eb Modifications in order to download the thumbnail picture and put it as default picture when creating youtube, dailymotion or vimeo contents
grandjoncl
parents: 791
diff changeset
    32
import urlparse  
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
    33
import tempfile
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    34
import traceback
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    35
import sys
792
e4180c2d11eb Modifications in order to download the thumbnail picture and put it as default picture when creating youtube, dailymotion or vimeo contents
grandjoncl
parents: 791
diff changeset
    36
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
    37
@transaction.commit_manually
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    38
def write_content_base(request, iri_id=None):
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    39
    logging.debug("coucou entre")
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: 791
diff changeset
    40
    if iri_id:
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    41
        instance_content = Content.safe_objects.get(iri_id=iri_id) #@UndefinedVariable
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    42
        instance_media = instance_content.media_obj
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    43
        logging.debug("write_content_base : valid form: for instance : media -> " + repr(instance_media) + " content : for instance : " + repr(instance_content)) #@UndefinedVariable
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    44
    else:
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    45
        logging.debug("No iri_id") #@UndefinedVariable
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    46
        instance_content = None
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
    47
        instance_media = None
734
93af6b5ad5ac little debugs #30, #33, #34
cavaliet
parents: 710
diff changeset
    48
    current_front_project = None
93af6b5ad5ac little debugs #30, #33, #34
cavaliet
parents: 710
diff changeset
    49
    if instance_content:
93af6b5ad5ac little debugs #30, #33, #34
cavaliet
parents: 710
diff changeset
    50
        current_front_project = instance_content.front_project
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: 791
diff changeset
    51
    form_status = 'none'
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
    52
    errors_transaction = []
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
    53
    
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
    54
    if request.method == "POST":
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    55
        logging.debug("coucou entre POST")
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    56
        try:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    57
            if instance_content is not None:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    58
                content_instance_val = model_to_dict(instance_content, exclude=ContentForm.Meta.exclude)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    59
            else:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    60
                content_instance_val = {}
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    61
            
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    62
            if instance_media is not None:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    63
                media_instance_val = model_to_dict(instance_media, exclude=MediaForm.Meta.exclude)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    64
            else:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    65
                media_instance_val = {}
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    66
            #add prefix
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    67
            
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    68
            def add_prefix(_dict, prefix):
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    69
                return dict([('%s-%s' % (prefix, key), value) for key,value in _dict.items()])
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
    70
            
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    71
            content_instance_val = add_prefix(content_instance_val, "content")
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    72
            media_instance_val= add_prefix(media_instance_val, "media")    
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    73
                    
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    74
            for k in request.POST.keys():
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    75
                value = request.POST.get(k)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    76
                content_instance_val[k] = value
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    77
                media_instance_val[k] = value
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    78
                
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    79
            content_instance_val['read_list'] = request.POST.getlist('read_list')
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    80
            content_instance_val['write_list'] = request.POST.getlist('write_list')
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    81
            content_instance_val['share'] = request.POST.get('share', True)
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
    82
            
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    83
            content_form = ContentForm(content_instance_val, prefix="content", instance=instance_content)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    84
            media_form = MediaForm(media_instance_val, request.FILES, prefix="media", instance=instance_media)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    85
            picture_form = PictureForm(None, request.POST, request.FILES)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    86
       
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    87
            if request.user.is_staff:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    88
                content_form.fields['front_project'].queryset = Project.objects.filter(contents__in=[instance_content])
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    89
            
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    90
            media_valid = media_form.is_valid()
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    91
            content_valid = content_form.is_valid()
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    92
            picture_valid = picture_form.is_valid()
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    93
            if 'image' in request.POST.keys():
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    94
                image_link = request.POST.get('url_image')
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    95
                if picture_valid and image_link!='' :
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    96
                    try :
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    97
                        r = requests.get(image_link)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    98
                        img_temp = tempfile.NamedTemporaryFile(suffix='.png')
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
    99
                        if img_temp:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   100
                            img_temp.write(r.content)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   101
                            img_temp.flush()
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   102
                            picture_form.cleaned_data["image"]=File(img_temp) 
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   103
                    except Exception as inst:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   104
                        logging.debug("couldn't download video thumbnail from image_link : " + str(image_link))
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   105
            logging.debug("write_content_base : valid form: for instance : " + repr(instance_media) + " -> media " + str(media_valid) + " content : for instance : " + repr(instance_content) + " : " + str(content_valid) + "picture : valid :" +str(picture_valid)) #@UndefinedVariable
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   106
            
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   107
            if media_valid and content_valid and picture_valid:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   108
    
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   109
                # see if media must be created
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   110
                cleaned_data = {}
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   111
                cleaned_data.update(media_form.cleaned_data)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   112
                cleaned_data.pop("media_public")
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   113
                
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   114
                media_input_type = content_form.cleaned_data["media_input_type"]
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   115
                
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   116
                if media_input_type == "none":
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   117
                    media = None
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   118
                elif media_input_type == "link":
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   119
                    media = content_form.cleaned_data["media_obj"]
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   120
                    created = False
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   121
                elif media_input_type == "create":
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   122
                    del cleaned_data["media_file"]
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   123
                    if not cleaned_data['videopath']:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   124
                        cleaned_data['videopath'] = settings.STREAM_URL
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   125
                    # if the source is already http:// or rtmp:// we don't have to add STREAM_URL
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   126
                    if cleaned_data['src'].startswith("rtmp://") or cleaned_data['src'].startswith("http://"):
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   127
                        cleaned_data['videopath'] = ''
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   128
                        
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   129
                        try:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   130
                            media, created = Media.objects.get_or_create(src=cleaned_data['src'], defaults=cleaned_data) #@UndefinedVariable
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   131
                        except Exception, e:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   132
                            raise e
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   133
                
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   134
                elif media_input_type == "url" or media_input_type == "upload" :
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   135
                    # copy file
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   136
                    #complet src
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   137
                    destination_file = None
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   138
                    source_file = None
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   139
                    try:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   140
                        if media_input_type == "url":
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   141
                            url = cleaned_data["external_src_url"]
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   142
                            source_file = urllib2.urlopen(url)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   143
                            source_filename = source_file.info().get('Content-Disposition', None)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   144
                            if not source_filename:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   145
                                source_filename = urlparse.urlparse(url).path.rstrip("/").split('/')[-1]
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   146
                        elif media_input_type == "upload":
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   147
                            #source_file = request.FILES['media-media_file']
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   148
                            # At this point the file has already be uploaded thanks to the upload view, and original file name is sent through a post var
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   149
                            source_filename = request.POST["media-local_file_name"]
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   150
                        
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   151
                        source_filename = ldt_utils_path.sanitize_filename(source_filename)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   152
                        destination_filepath = os.path.join(settings.STREAM_PATH, source_filename)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   153
                        base_source_filename = source_filename
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   154
                        extension = base_source_filename.split(".")[-1]
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   155
                        if extension == base_source_filename:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   156
                            extension = ""
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   157
                            base_basename_filename = base_source_filename
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   158
                        else:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   159
                            base_basename_filename = base_source_filename[:-1 * (len(extension) + 1)]
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   160
                        i = 0
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   161
                        
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   162
                        while os.path.exists(destination_filepath):
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   163
                            base_source_filename = "%s.%d.%s" % (base_basename_filename, i, extension)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   164
                            destination_filepath = os.path.join(settings.STREAM_PATH, base_source_filename)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   165
                            i += 1
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   166
                        
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   167
                        if media_input_type == "url":
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   168
                            # we upload the file if we are in url case
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   169
                            destination_file = open(destination_filepath, "wb")
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   170
                            chunck = source_file.read(2048)
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   171
                            while chunck:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   172
                                destination_file.write(chunck)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   173
                                chunck = source_file.read(2048)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   174
                            
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   175
                        elif media_input_type == "upload":
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   176
                            # The media file has been uploaded in the session temp folder 
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   177
                            # so we just have to move to the regular folder and rename it.
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   178
                            if os.path.exists(os.path.join(settings.STREAM_PATH, "tmp/" + request.COOKIES[settings.SESSION_COOKIE_NAME] + "/", source_filename)):
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   179
                                os.rename(os.path.join(settings.STREAM_PATH, "tmp/" + request.COOKIES[settings.SESSION_COOKIE_NAME] + "/", source_filename), os.path.join(settings.STREAM_PATH, base_source_filename))
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   180
                        
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   181
                        
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   182
                        src_prefix = settings.STREAM_SRC_PREFIX.rstrip("/")
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   183
                        if len(src_prefix) > 0:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   184
                            cleaned_data["src"] = src_prefix + "/" + base_source_filename
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   185
                        else:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   186
                            cleaned_data["src"] = base_source_filename
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   187
                        
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   188
                        
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   189
                    except Exception as inst:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   190
                        logging.debug("write_content_base : POST error when processing file:" + str(inst)) #@UndefinedVariable
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   191
                        form_status = "error"
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   192
                        #set error for form
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   193
                        if media_input_type == "url":
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   194
                            errors = media_form._errors.setdefault("external_src_url", ErrorList())
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   195
                            errors.append(_("Problem when downloading file from url : ") + url)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   196
                        elif media_input_type == "upload":
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   197
                            errors = media_form._errors.setdefault("media_file", ErrorList())
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   198
                            errors.append(_("Problem when uploading file : ") + str(inst))
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   199
                    finally:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   200
                        if media_input_type == "url":
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   201
                            if destination_file:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   202
                                destination_file.close()
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   203
                            if source_file:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   204
                                source_file.close()
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   205
                    
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   206
                    
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   207
                    if form_status != "error":
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   208
                        try:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   209
                            del cleaned_data["media_file"]
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   210
                            if not cleaned_data['videopath']:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   211
                                cleaned_data['videopath'] = settings.STREAM_URL
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   212
                            mimetype = cleaned_data.get('mimetype_field', None)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   213
                            if not mimetype:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   214
                                mimetype = mimetypes.guess_type(cleaned_data['src'])
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   215
                            cleaned_data['mimetype_field'] = mimetype
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   216
                            media, created = Media.safe_objects.get_or_create(src=cleaned_data['src'], defaults=cleaned_data) #@UndefinedVariable
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   217
                            cached_assign('view_media', request.user, media)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   218
                        except Exception, e:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   219
                            raise e
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   220
                    else:
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   221
                        media = None
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   222
                        
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   223
    
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   224
                if media and not created:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   225
                    for attribute in ('external_id', 'external_permalink', 'external_publication_url', 'external_src_url', 'media_creation_date', 'videopath', 'duration', 'description', 'title', 'front_project'):
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   226
                        setattr(media, attribute, cleaned_data.get(attribute))
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   227
                    mimetype = cleaned_data.get('mimetype_field', None)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   228
                    if not mimetype:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   229
                        mimetype = mimetypes.guess_type(media.src)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   230
                    media.mimetype_field = mimetype
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   231
                    cached_assign('view_media', request.user, media)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   232
                    cached_assign('change_media', request.user, media)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   233
                    media.save()
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   234
                
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   235
                if form_status != "error": 
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   236
                    content_defaults = {}
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   237
                    content_defaults.update(content_form.cleaned_data)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   238
                    content_defaults['media_obj'] = media
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   239
                                   
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   240
                    for key in ["media_input_type", "groups", "is_public", "read_list", "write_list", "share" ]:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   241
                        del content_defaults[key]
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   242
                    try:
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   243
                        content, created = Content.safe_objects.get_or_create(iri_id=content_form.cleaned_data['iri_id'], defaults=content_defaults) #@UndefinedVariable
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   244
                    except Exception, e:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   245
                        raise e
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   246
                                      
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   247
                    if not created and not request.user.has_perm('ldt_utils.change_content', content):
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   248
                        raise AttributeError("%s is not allowed to change content %s" % (request.user, content))
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   249
                    
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   250
                    cached_assign('change_content', request.user, content)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   251
                    cached_assign('view_content', request.user, content)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   252
                    everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   253
                    
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   254
                    if media_form.cleaned_data['media_public']:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   255
                        cached_assign('view_content', everyone, content)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   256
                        if media:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   257
                            cached_assign('view_media', everyone, media)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   258
                    else:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   259
                        remove_perm('ldt_utils.view_media', everyone, media)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   260
                        assign_perm_to_obj(content, content_form.cleaned_data['read_list'], content_form.cleaned_data['write_list'], request.user)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   261
                        if media:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   262
                            assign_perm_to_obj(media, content_form.cleaned_data['read_list'], content_form.cleaned_data['write_list'], request.user)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   263
                        if content_form.cleaned_data['is_public']:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   264
                            cached_assign('view_content', everyone, content)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   265
                        else:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   266
                            remove_perm('ldt_utils.view_content', everyone, content)
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   267
                    
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   268
                    if not created:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   269
                        for attribute in ('iriurl', 'title', 'description', 'duration', 'content_creation_date', 'tags', 'media_obj'):
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   270
                            setattr(content, attribute, content_defaults[attribute])
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   271
                            
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   272
                        if request.user.is_staff and content_defaults.has_key('front_project'):
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   273
                            content.front_project = content_defaults['front_project']
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   274
                    
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   275
                    try:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   276
                        content.save()
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   277
                        picture_form.model = content
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   278
                        picture_form.save()  
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   279
                        form_status = 'saved'
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   280
                        media_form = MediaForm(instance=media, prefix="media")
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   281
                        content_form = ContentForm(instance=content, prefix="content")
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   282
                        picture_form = PictureForm()
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   283
                    except Exception, e:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   284
                        raise e
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   285
            else:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   286
                form_status = 'error'
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   287
        except Exception, e:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   288
            transaction.rollback()
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   289
            type, value, tb = sys.exc_info()
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   290
            errors_transaction.append(e)
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   291
            errors_transaction.append(traceback.format_tb(tb))
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   292
            return False, False, False, False, False, errors_transaction
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   293
            
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   294
    else:
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   295
        logging.debug("coucou entree non Post")
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   296
        form_status = 'empty'
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   297
        initial_c = { 'media_input_type':"link"}
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   298
        initial_m = {}
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   299
        if instance_media:
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   300
            initial_m['media_public'] = instance_media.is_public
544
610524c5d1e3 Fix bug in content edition form
verrierj
parents: 517
diff changeset
   301
        else:
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   302
            initial_m['media_public'] = True
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   303
        if instance_content:
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   304
            initial_c['is_public'] = instance_content.is_public
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   305
        else:
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   306
            initial_c['is_public'] = True
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   307
        content_form = ContentForm(prefix="content", instance=instance_content, initial=initial_c)
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   308
        media_form = MediaForm(prefix="media", instance=instance_media, initial=initial_m) 
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   309
        picture_form = PictureForm() 
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   310
                
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   311
        if instance_content is not None:
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   312
            content_form.media_input_type = "link"
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   313
                   
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   314
            if request.user.is_staff:
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   315
                content_form.fields['front_project'].queryset = Project.objects.filter(contents__in=[instance_content])
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   316
    
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   317
    try:
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   318
        transaction.commit()
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   319
    except Exception, e:
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   320
        transaction.rollback()
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   321
        errors_transaction.append(e)
882
9520fb2810e2 Add of unit test for ldt_utils
rougeronj
parents: 843
diff changeset
   322
        return False, False, False, False, False, errors_transaction
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   323
        
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   324
    return content_form, media_form, picture_form, form_status, current_front_project, errors_transaction
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   325
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   326
@login_required
791
12ba862f33fa Creation of a page instead of pop up for the content creation, update and suppression
grandjoncl
parents: 786
diff changeset
   327
def write_content(request, iri_id=None):  
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   328
    submit_action = request.REQUEST.get("submit_button", False) 
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   329
    member_list = admin_list = []
734
93af6b5ad5ac little debugs #30, #33, #34
cavaliet
parents: 710
diff changeset
   330
    current_front_project = None
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   331
    deleted = None
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   332
    
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   333
    if submit_action == "prepare_delete":
843
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   334
        errors, titles, message_temp = prepare_delete_content(request, iri_id)
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   335
        if errors and len(errors) > 0:
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   336
            message = ungettext("There is %(count)d error when deleting content", "There are %(count)d errors when deleting content", len(errors)) % { 'count': len(errors)}
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   337
            title_msg = _('title error deleting content')
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   338
        else:
843
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   339
            if len(message_temp)>0:
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   340
                message = message_temp
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   341
            else:    
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   342
                message = _("Confirm delete content %(titles)s") % { 'titles' : ",".join(titles) }
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   343
            title_msg = _("confirm delete content")
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   344
        return render_to_response('ldt/ldt_utils/error_confirm.html', {'errors':errors, 'message':message, 'title': title_msg}, context_instance=RequestContext(request))  
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   345
    elif submit_action == "delete":
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   346
        deleted, errors_transaction = delete_content(request, iri_id)
843
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   347
        content_form = ContentForm()
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   348
        form_status = "deleted"
814
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   349
    elif submit_action == "prepare_reset":
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   350
        errors=[]
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   351
        content_temp = Content.objects.get(iri_id=iri_id)
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   352
        if content_temp.front_project is not None:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   353
            if content_temp.front_project.state==2:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   354
                errors.append(_("Please unpublish the front project %(title)s") % {'title':content_temp.front_project.title})
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   355
                message=_("The front project is published")
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   356
                title = _("confirm reset")
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   357
            else:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   358
                message = _("please confirm reseting project %(title)s") % {'title':content_temp.front_project.title}
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   359
                title = _("confirm reset")
814
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   360
        else:
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   361
            content_temp.create_front_project()
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   362
            return redirect("root-view")
814
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   363
        return render_to_response('ldt/ldt_utils/reset_confirm.html', {'errors':errors, 'message':message, 'title': title}, context_instance=RequestContext(request))
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   364
    elif submit_action == "reset":
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   365
        #TODO : verifier index de la recherche maj
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   366
        content = Content.objects.get(iri_id=iri_id)
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   367
        project_temp = content.front_project
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   368
        content.create_front_project()
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   369
        content.save()
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   370
        project_temp.delete()
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   371
        return HttpResponseRedirect(reverse('ldt.ldt_utils.views.content.write_content', kwargs={'iri_id':iri_id}))
791
12ba862f33fa Creation of a page instead of pop up for the content creation, update and suppression
grandjoncl
parents: 786
diff changeset
   372
    elif submit_action=="close":
814
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   373
        return redirect("root-view")
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   374
    else:
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   375
        content_form, media_form, picture_form, form_status, current_front_project, errors_transaction = write_content_base(request, iri_id)        
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   376
        if iri_id:
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: 791
diff changeset
   377
            content_temp = Content.objects.get(iri_id=iri_id)
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: 791
diff changeset
   378
            media_temp = content_temp.media_obj
807
06891536f619 bug correction when trying to access a content without media
rougeronj
parents: 803
diff changeset
   379
            if media_temp:
06891536f619 bug correction when trying to access a content without media
rougeronj
parents: 803
diff changeset
   380
                member_list, admin_list = get_userlist_model(media_temp, request.user)
06891536f619 bug correction when trying to access a content without media
rougeronj
parents: 803
diff changeset
   381
            else:
06891536f619 bug correction when trying to access a content without media
rougeronj
parents: 803
diff changeset
   382
                member_list, admin_list = get_userlist_model(content_temp, request.user)
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   383
    
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   384
    # Deleted is False if an error occurred during deletion
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   385
    if (deleted == False)  or (content_form == False and media_form == False and picture_form == False and form_status == False and current_front_project == False):
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   386
        message=_("An error occurred - Please try again or contact webmaster")
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   387
        title = _("Error")
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   388
        return render_to_response('ldt/ldt_utils/error_confirm.html', {'errors':errors_transaction, 'message':message, 'title': title}, context_instance=RequestContext(request))
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   389
    
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   390
    if iri_id:
321
c68744402249 Change urls used in reverse to be compatible with the new organization
verrierj
parents: 319
diff changeset
   391
        create_content_action = reverse('ldt.ldt_utils.views.content.write_content', kwargs={'iri_id':iri_id})
327
e801e50edcce A description can be added to a group
verrierj
parents: 325
diff changeset
   392
        img_container = content_form.instance
791
12ba862f33fa Creation of a page instead of pop up for the content creation, update and suppression
grandjoncl
parents: 786
diff changeset
   393
814
fb232b3266b0 Add "reset front project button" in the content creation/update form
rougeronj
parents: 807
diff changeset
   394
    else:
321
c68744402249 Change urls used in reverse to be compatible with the new organization
verrierj
parents: 319
diff changeset
   395
        create_content_action = reverse('ldt.ldt_utils.views.content.write_content')
327
e801e50edcce A description can be added to a group
verrierj
parents: 325
diff changeset
   396
        img_container = ''
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   397
    
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   398
    session_key = request.COOKIES[settings.SESSION_COOKIE_NAME]
410
35453efed7ed Fix bug when saving content whith front_project
verrierj
parents: 404
diff changeset
   399
    cookie_name = settings.SESSION_COOKIE_NAME
702
d50cb79f96ee Add category cloud in front and media page. Correct js pagination in workspace. Languages updated.
cavaliet
parents: 646
diff changeset
   400
    # Media.safe_objects.all() does not return the good list of media, so we get them from the Content.safe_objects
d50cb79f96ee Add category cloud in front and media page. Correct js pagination in workspace. Languages updated.
cavaliet
parents: 646
diff changeset
   401
    content_form.fields["media_obj"].queryset = Media.objects.filter(id__in=Content.safe_objects.values_list('media_obj', flat=True))
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   402
    
791
12ba862f33fa Creation of a page instead of pop up for the content creation, update and suppression
grandjoncl
parents: 786
diff changeset
   403
    if form_status=='saved' or form_status=='deleted':
12ba862f33fa Creation of a page instead of pop up for the content creation, update and suppression
grandjoncl
parents: 786
diff changeset
   404
        return redirect("root-view") 
12ba862f33fa Creation of a page instead of pop up for the content creation, update and suppression
grandjoncl
parents: 786
diff changeset
   405
    else:
801
cdcb93282637 List group display in the "create content from"
rougeronj
parents: 796
diff changeset
   406
        group_list = Group.objects.all()
cdcb93282637 List group display in the "create content from"
rougeronj
parents: 796
diff changeset
   407
        group_list = group_list.exclude(name=settings.PUBLIC_GROUP_NAME)
cdcb93282637 List group display in the "create content from"
rougeronj
parents: 796
diff changeset
   408
        group_list = [{'name': u.name, 'id': u.id, 'type': 'group'} for u in group_list[0:settings.MAX_USERS_SEARCH]]
cdcb93282637 List group display in the "create content from"
rougeronj
parents: 796
diff changeset
   409
        elem_list = get_userlist(request.user) + group_list
791
12ba862f33fa Creation of a page instead of pop up for the content creation, update and suppression
grandjoncl
parents: 786
diff changeset
   410
        return render_to_response('ldt/ldt_utils/create_content.html', {'content_form': content_form, 'media_form': media_form, 'form_status': form_status, 'create_content_': create_content_action,
801
cdcb93282637 List group display in the "create content from"
rougeronj
parents: 796
diff changeset
   411
                                                                    'elem_list':elem_list, 'member_list': member_list, 'admin_list': admin_list,  'iri_id': iri_id, 'session_key':session_key,
734
93af6b5ad5ac little debugs #30, #33, #34
cavaliet
parents: 710
diff changeset
   412
                                                                    'cookie_name':cookie_name, 'img_container': img_container, 'profile_picture_form': picture_form,
93af6b5ad5ac little debugs #30, #33, #34
cavaliet
parents: 710
diff changeset
   413
                                                                    'current_front_project':current_front_project}, context_instance=RequestContext(request))
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   414
@login_required
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   415
def prepare_delete_content(request, iri_id=None): 
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   416
    errors = []
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   417
    titles = []
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   418
    delete = False
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   419
    message={}
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   420
    if not iri_id:
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   421
        iri_id = request.REQUEST.get("iri_id", None)
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   422
        
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   423
    if iri_id:
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   424
        for content in Content.safe_objects.filter(iri_id=iri_id): 
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   425
            titles.append(unicode(content.title))
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   426
            projects = content.project_set.all()
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   427
            projects_nb = len(projects)
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   428
            if projects_nb > 1:
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   429
                project_titles = map(lambda p: unicode(p.title), projects)
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   430
                errors.append(ungettext("Content '%(title)s' is referenced by this project : %(project_titles)s. Please delete it beforehand.", "Content '%(title)s' is referenced by %(count)d projects: %(project_titles)s. Please delete them beforehand.", projects_nb) % {'title':unicode(content.title), 'count':projects_nb, 'project_titles': ",".join(project_titles)})
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   431
            elif projects_nb == 1:
843
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   432
                if projects[0].has_annotations():
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   433
                    message = _("The project '%(project_title)s' pointing on the content '%(title)s' has several annotations. Do you want to delete the content and the project anyway ?")% {'project_title':unicode(projects[0].title), 'title':unicode(content.title)}
843
77f740eaf247 Bug correction on deletion content. Bug correction : forbidden_stream_url was saved in the xml of the project
rougeronj
parents: 840
diff changeset
   434
    return errors, titles, message
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   435
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   436
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   437
@login_required
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   438
@transaction.commit_manually
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   439
def delete_content(request, iri_id=None):
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   440
    #Delete the project, the media if exists, and the content
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   441
    errors_transaction=[]  
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   442
    if not iri_id:
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   443
        iri_id = request.REQUEST.get("iri_id", None)  
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   444
    if iri_id:
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   445
        content = Content.safe_objects.get(iri_id=iri_id)
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   446
        try:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   447
            if content.media_obj is not None:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   448
                contents_media = Content.safe_objects.filter(media_obj = content.media_obj)
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   449
                #check if the media is ref by severals content
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   450
                if len(contents_media) < 2:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   451
                    Media.safe_objects.get(id=content.media_obj.id).delete()
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   452
            project = content.front_project
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   453
            content.delete()
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   454
            if project is not None:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   455
                Project.safe_objects.get(ldt_id= project.ldt_id).delete()
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   456
        except Exception, e:
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   457
            content.rollback()
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   458
            transaction.rollback()
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   459
            errors_transaction.append(_("Content deletion failure"))
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   460
            errors_transaction.append(e)
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   461
            return False, errors_transaction
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   462
        else:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   463
            try:
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   464
                transaction.commit()
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   465
                content.commit()
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   466
                return True, errors_transaction
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   467
            except:
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   468
                content.rollback()
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   469
                transaction.rollback()
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   470
                errors_transaction.append(_("Commit of the content deletion failed"))
898
94ad1a394732 update of error management in the creation of content
rougeronj
parents: 883
diff changeset
   471
                errors_transaction.append(e)
840
b38c88bdefa2 Creation and deletion of content, iri file, and thumbnail are now transactional
rougeronj
parents: 814
diff changeset
   472
                return False, errors_transaction
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   473
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   474
def upload(request):
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   475
    if request.method == 'POST':
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   476
        for field_name in request.FILES:
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   477
            # We get the file name
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   478
            source_file = request.FILES[field_name]
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   479
            source_filename = source_file.name
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   480
            # We sanitize the file name : no space, only lower case.
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   481
            source_filename = ldt_utils_path.sanitize_filename(source_filename)
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   482
            # We create the session temp folder if necessary
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   483
            if not os.path.exists(os.path.join(settings.STREAM_PATH, "tmp/" + request.COOKIES[settings.SESSION_COOKIE_NAME])):
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   484
                os.makedirs(os.path.join(settings.STREAM_PATH, "tmp/" + request.COOKIES[settings.SESSION_COOKIE_NAME]))
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   485
            destination_filepath = os.path.join(settings.STREAM_PATH, "tmp/" + request.COOKIES[settings.SESSION_COOKIE_NAME] + "/", source_filename)
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   486
            # We delete the existing file if necessary
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   487
            if os.path.exists(destination_filepath):
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   488
                os.remove(destination_filepath)
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   489
            
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   490
            destination_file = open(destination_filepath, "wb")
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   491
            
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   492
            for chunk in source_file.chunks():
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   493
                destination_file.write(chunk)
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   494
            destination_file.close()
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   495
            
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   496
        # indicate that everything is OK for SWFUpload
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   497
        return HttpResponse("ok", mimetype="text/plain")
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   498
    else:
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   499
        return HttpResponse("notok", mimetype="text/plain")
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   500
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   501
def remove_temp_file(request):
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   502
    # The filename arrives with a GET var.
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   503
    file_path = os.path.join(settings.STREAM_PATH, "tmp/" + request.COOKIES[settings.SESSION_COOKIE_NAME] + "/", ldt_utils_path.sanitize_filename(request.GET["filename"]))
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   504
    if os.path.exists(file_path):
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   505
        os.remove(file_path)
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   506
    return HttpResponse("remove ok", mimetype="text/plain")
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   507
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   508
def get_duration(request):
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   509
    try:
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   510
        # The filename arrives with a GET var.
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   511
        file_path = os.path.join(settings.STREAM_PATH, "tmp/" + request.COOKIES[settings.SESSION_COOKIE_NAME] + "/", ldt_utils_path.sanitize_filename(request.GET["filename"]))
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   512
        if hasattr(settings, 'FFMPEG_PATH') and os.path.exists(file_path):
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   513
            output = str(subprocess.Popen([settings.FFMPEG_PATH, "-i", file_path], stderr=subprocess.PIPE).communicate()[1])
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   514
            m = re.search("Duration:\s*?([\d.:]+)", output, re.M)
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   515
            dur_arr = m.group(1).split(":")
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   516
            td = datetime.timedelta(hours=int(dur_arr[0]), minutes=int(dur_arr[1]), seconds=float(dur_arr[2]))
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   517
            str_duration = str((td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / 10 ** 3)
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   518
            return HttpResponse(str_duration, mimetype="text/plain")
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   519
        else:
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   520
            return HttpResponse("", mimetype="text/plain")
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   521
    except Exception as inst:
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   522
        return HttpResponse(str(inst), mimetype="text/plain")
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   523
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   524
    
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   525
@login_required
786
623bc24bee49 change filter to filter_c to avoid name collision with the built-in filter fuction
ymh <ymh.work@gmail.com>
parents: 785
diff changeset
   526
def contents_filter(request, filter_c): 
623bc24bee49 change filter to filter_c to avoid name collision with the built-in filter fuction
ymh <ymh.work@gmail.com>
parents: 785
diff changeset
   527
    if filter_c and len(filter_c) > 0 and filter_c[0] == '_':
623bc24bee49 change filter to filter_c to avoid name collision with the built-in filter fuction
ymh <ymh.work@gmail.com>
parents: 785
diff changeset
   528
        filter_c = filter_c[1:]
646
97f5db87d71c Enhance pagination with public project and quick search.
cavaliet
parents: 644
diff changeset
   529
    
97f5db87d71c Enhance pagination with public project and quick search.
cavaliet
parents: 644
diff changeset
   530
    num_page = 0
97f5db87d71c Enhance pagination with public project and quick search.
cavaliet
parents: 644
diff changeset
   531
    if request.GET.has_key('num_page'):
97f5db87d71c Enhance pagination with public project and quick search.
cavaliet
parents: 644
diff changeset
   532
        num_page = int(request.GET["num_page"])
710
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   533
    tag_filter = ""
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   534
    if request.GET.has_key('tag_filter'):
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   535
        tag_filter = request.GET["tag_filter"]
646
97f5db87d71c Enhance pagination with public project and quick search.
cavaliet
parents: 644
diff changeset
   536
    
786
623bc24bee49 change filter to filter_c to avoid name collision with the built-in filter fuction
ymh <ymh.work@gmail.com>
parents: 785
diff changeset
   537
    # We paginate the content list, in case of filter_c or not
623bc24bee49 change filter to filter_c to avoid name collision with the built-in filter fuction
ymh <ymh.work@gmail.com>
parents: 785
diff changeset
   538
    if filter_c and not tag_filter :
623bc24bee49 change filter to filter_c to avoid name collision with the built-in filter fuction
ymh <ymh.work@gmail.com>
parents: 785
diff changeset
   539
        content_nb = Content.safe_objects.filter(title__icontains=filter_c).count()
710
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   540
        nb_ct_pages = int(math.ceil(content_nb / settings.LDT_MAX_CONTENTS_PER_PAGE)) + 1
786
623bc24bee49 change filter to filter_c to avoid name collision with the built-in filter fuction
ymh <ymh.work@gmail.com>
parents: 785
diff changeset
   541
        content_list = Content.safe_objects.filter(title__icontains=filter_c)[(num_page*settings.LDT_MAX_CONTENTS_PER_PAGE):((num_page+1)*settings.LDT_MAX_CONTENTS_PER_PAGE)] #@UndefinedVariable
623bc24bee49 change filter to filter_c to avoid name collision with the built-in filter fuction
ymh <ymh.work@gmail.com>
parents: 785
diff changeset
   542
    elif filter_c and tag_filter :
710
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   543
        #TaggedItem.objects.get_by_model(Content.objects.all(), '"'+tag_filter+'"')
786
623bc24bee49 change filter to filter_c to avoid name collision with the built-in filter fuction
ymh <ymh.work@gmail.com>
parents: 785
diff changeset
   544
        content_nb = TaggedItem.objects.get_by_model(Content.safe_objects.filter(title__icontains=filter_c), '"'+tag_filter+'"').count()
710
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   545
        nb_ct_pages = int(math.ceil(content_nb / settings.LDT_MAX_CONTENTS_PER_PAGE)) + 1
786
623bc24bee49 change filter to filter_c to avoid name collision with the built-in filter fuction
ymh <ymh.work@gmail.com>
parents: 785
diff changeset
   546
        content_list = TaggedItem.objects.get_by_model(Content.safe_objects.filter(title__icontains=filter_c), '"'+tag_filter+'"')[(num_page*settings.LDT_MAX_CONTENTS_PER_PAGE):((num_page+1)*settings.LDT_MAX_CONTENTS_PER_PAGE)] #@UndefinedVariable
623bc24bee49 change filter to filter_c to avoid name collision with the built-in filter fuction
ymh <ymh.work@gmail.com>
parents: 785
diff changeset
   547
    elif tag_filter and not filter_c:
710
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   548
        content_nb = TaggedItem.objects.get_by_model(Content.safe_objects.all(), '"'+tag_filter+'"').count()
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   549
        nb_ct_pages = int(math.ceil(content_nb / settings.LDT_MAX_CONTENTS_PER_PAGE)) +1
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   550
        content_list = TaggedItem.objects.get_by_model(Content.safe_objects.all(), '"'+tag_filter+'"')[(num_page*settings.LDT_MAX_CONTENTS_PER_PAGE):((num_page+1)*settings.LDT_MAX_CONTENTS_PER_PAGE)] #@UndefinedVariable
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   551
    else:
646
97f5db87d71c Enhance pagination with public project and quick search.
cavaliet
parents: 644
diff changeset
   552
        content_nb, nb_ct_pages, content_list = get_contents_page(num_page, request.user)
97f5db87d71c Enhance pagination with public project and quick search.
cavaliet
parents: 644
diff changeset
   553
    #Change attributes with object permissions
97f5db87d71c Enhance pagination with public project and quick search.
cavaliet
parents: 644
diff changeset
   554
    content_list = add_change_attr(request.user, content_list)
710
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   555
    # Get the all tags list
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   556
    tag_cloud = get_content_tags()
644
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
   557
    
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
   558
    is_gecko = ((request.META['HTTP_USER_AGENT'].lower().find("firefox")) > -1);
315
877f89ec1efa Split big views.py files into several views grouped by "make sense".
cavaliet
parents:
diff changeset
   559
    return render_to_response("ldt/ldt_utils/partial/contentslist.html",
644
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
   560
                              {'contents': content_list, 'nb_ct_pages': nb_ct_pages, 'content_nb': content_nb, 'current_content_page':float(num_page),
710
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   561
                               'tag_cloud': tag_cloud, 'current_content_tag':tag_filter, 'is_gecko': is_gecko
644
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
   562
                               },
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
   563
                              context_instance=RequestContext(request))
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
   564
    
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
   565
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
   566
def get_contents_page(num_page, user):
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
   567
    content_nb = float(Content.safe_objects.count()) #@UndefinedVariable
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
   568
    nb_ct_pages = int(math.ceil(content_nb / settings.LDT_MAX_CONTENTS_PER_PAGE))
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
   569
    content_list = add_change_attr(user, Content.safe_objects.all()[(num_page*settings.LDT_MAX_CONTENTS_PER_PAGE):((num_page+1)*settings.LDT_MAX_CONTENTS_PER_PAGE)]) #@UndefinedVariable
94429061bbfa First step of pagination for contents and projet in workspace home.
cavaliet
parents: 544
diff changeset
   570
    return content_nb, nb_ct_pages, content_list
710
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   571
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   572
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   573
def get_content_tags(limit=None, steps=10):
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   574
    if limit is None:
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   575
        return Tag.objects.cloud_for_model(Content, steps=steps)
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   576
    else :
eea2dcb555c1 Content tag cloud and filter in workspace.
cavaliet
parents: 702
diff changeset
   577
        return Tag.objects.cloud_for_model(Content, steps=steps)[:limit]
779
129b1c8b0088 Commit Louise :
Louise Grandjonc <louise.grandjonc@iri.centrepompidou.fr>
parents: 734
diff changeset
   578