web/ldt/ldt_utils/views.py
author ymh <ymh.work@gmail.com>
Tue, 12 Oct 2010 16:11:15 +0200
changeset 83 ec31a4bd86d3
parent 82 9202807b4cec
child 84 6c3162d9e632
permissions -rw-r--r--
corrections on evolutions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
     1
from Ft.Xml import MarkupWriter
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
     2
from contentindexer import *
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
     3
from django.conf import settings
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
     4
from django.contrib.auth.decorators import login_required
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
     5
from django.core import serializers
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
     6
from django.core.urlresolvers import reverse
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
     7
from django.forms.util import ErrorList
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
     8
from django.http import HttpResponse, HttpResponseRedirect, \
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
     9
    HttpResponseForbidden
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    10
from django.shortcuts import render_to_response, get_object_or_404, \
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    11
    get_list_or_404
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
from django.template import RequestContext
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
from django.utils import simplejson
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
from django.utils.html import escape
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    15
from django.utils.translation import ugettext as _, ungettext
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
from fileimport import *
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    17
from forms import LdtImportForm, LdtAddForm, SearchForm, AddProjectForm, \
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    18
    CopyProjectForm, ContentForm, MediaForm
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
from ldt.core.models import Owner
83
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
    20
from lxml import etree
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
from models import *
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
from projectserializer import *
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
from string import Template
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    24
from urllib2 import urlparse
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    25
from utils import *
83
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
    26
import StringIO
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    27
import base64
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
import cgi
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    29
import django.core.urlresolvers
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    30
import ldt.auth as ldt_auth
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    31
import ldt.utils.path as ldt_utils_path
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    32
import logging
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    33
import lucene
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    34
import tempfile
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
    35
import urllib2
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
import uuid
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
import xml.dom
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
import xml.dom.ext
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
import xml.dom.minidom
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
def searchForm(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    form = SearchForm()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    return render_to_response('ldt/ldt_utils_utils/search_form.html',{'form': form} , context_instance=RequestContext(request))    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
def searchIndex(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    sform = SearchForm(request.POST)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    if sform.is_valid():
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        search = sform.cleaned_data["search"]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        queryStr = base64.urlsafe_b64encode(search.encode('utf8'))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        field = request.POST["field"]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        language_code = request.LANGUAGE_CODE[:2]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        url = settings.WEB_URL + django.core.urlresolvers.reverse("ldt.ldt_utils.views.searchInit", args=[field, queryStr])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        return render_to_response('ldt/ldt_utils/init_ldt.html', {'MEDIA_URL': settings.MEDIA_URL, 'colorurl': settings.MEDIA_URL+'swf/ldt/pkg/color.xml', 'i18nurl': settings.MEDIA_URL+'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.MEDIA_URL+'swf/ldt/', 'url': url}, context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        resp = HttpResponse()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        resp.write("<html><head></head><body>Error : No result</body></html>");
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
def searchIndexGet(request, field, query):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    language_code = request.LANGUAGE_CODE[:2]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    url = settings.WEB_URL + django.core.urlresolvers.reverse("ldt.ldt_utils.views.searchInit", args=[field, query])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    return render_to_response('irisuser/ldt/init_ldt.html', {'MEDIA_URL': settings.MEDIA_URL, 'colorurl': settings.MEDIA_URL+'swf/ldt/pkg/color.xml', 'i18nurl': settings.MEDIA_URL+'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.MEDIA_URL+'swf/ldt/', 'url': url}, context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
def searchInit(request, field, query):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
    ldtgen = LdtUtils()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
    doc = ldtgen.generateInit([field,query], 'ldt.ldt_utils.views.searchLdt', 'ldt.ldt_utils.views.searchSegments')
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    resp = HttpResponse(mimetype="text/xml;charset=utf-8")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
    xml.dom.ext.PrettyPrint(doc, resp)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    return resp
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
def searchLdt(request, field, query, edition=None):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    contentList = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
    resp = HttpResponse(mimetype="text/xml")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    queryStr = ""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
    if query and len(query)>0:        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        queryStr = base64.urlsafe_b64decode(query.encode("ascii")).decode("utf8")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        searcher = LdtSearch()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
        ids = {}
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
        for result in searcher.query(field, queryStr):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
            ids[result["iri_id"]] = ""            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        id_list = ids.keys()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
        if edition is not None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
            ids_editions = map(lambda t:t[0], filter(lambda id: id[0] is not None, Speak.objects.filter(session__day__edition=edition).order_by("session__start_ts", "order").values_list("content__iri_id")))            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
            id_list = filter(lambda id: id in id_list, ids_editions)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        contentList = Content.objects.filter(iri_id__in=id_list)        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
    ldtgen = LdtUtils()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
    ldtgen.generateLdt(contentList, file=resp, title = u"Recherche : " + queryStr)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
    return resp
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
def searchSegments(request, field, query, edition=None):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
    if query and len(query)>0:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
        searcher = LdtSearch()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        queryStr = base64.urlsafe_b64decode(query.encode("ascii")).decode("utf8")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        res = searcher.query(field, queryStr)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        res = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
    iri_ids = None
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
    if edition is not None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
        iri_ids = map(lambda t:t[0], filter(lambda id: id[0] is not None, Speak.objects.filter(session__day__edition=edition).order_by("session__start_ts", "order").values_list("content__iri_id")))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
    doc = xml.dom.getDOMImplementation().createDocument(None, "iri", None)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
    for resultMap in res:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
        if iri_ids is None or resultMap['iri_id'] in iri_ids:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
            elem = doc.createElement('seg')
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
            elem.setAttribute('idctt', resultMap['iri_id'])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
            elem.setAttribute('idens', resultMap['ensemble_id'])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
            elem.setAttribute('iddec', resultMap['decoupage_id'])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
            elem.setAttribute('idseg', resultMap['element_id'])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
            elem.setAttribute('idvue', "")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
            elem.setAttribute('crit', "")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
            doc.documentElement.appendChild(elem)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
    return HttpResponse(doc.toprettyxml(encoding='utf-8'), mimetype="text/xml;charset=utf-8")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
@login_required         
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
def list_ldt(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
    contents = Content.objects.all()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
    try:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
        owner = Owner.objects.get(user=request.user)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
    except:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
        return HttpResponseRedirect(settings.LOGIN_URL)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
    ldtProjects = Project.objects.filter(owner=owner)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
    context={
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
    'contents': contents,
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   148
    'projects': ldtProjects.reverse(),
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
    }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
    return render_to_response('ldt/ldt_utils/ldt_list.html', context, context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   152
@login_required         
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   153
def list_content(request):
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   154
    contents = Content.objects.all()
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   155
    context={
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   156
        'contents': contents,
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   157
    }
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   158
    return render_to_response('ldt/ldt_utils/content_list.html', context, context_instance=RequestContext(request))
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   159
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   160
@login_required
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
def create_ldt_view(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
    if request.method == "POST" :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
        form = LdtAddForm(request.POST)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
        if form.is_valid():
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
            user = request.user
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
            Project.create_project(title=form.cleaned_data['title'], user=user, contents=form.cleaned_data['contents'])
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   167
            form_status = "saved"
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   168
            contents=[]
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   169
            #return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt"))
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
        form = LdtAddForm()
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   172
        contents = Content.objects.all()
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   173
        form_status = "none"
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   174
            
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   175
    return render_to_response('ldt/ldt_utils/create_ldt.html', {'contents': contents, 'form': form, 'form_status':form_status,'create_project_action':reverse(create_ldt_view)}, context_instance=RequestContext(request))
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
     
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
def created_ldt(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
    return render_to_response('ldt/ldt_utils/done.html', context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
def indexProject(request, id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
    urlStr = settings.WEB_URL + reverse("space_ldt_init", args=['ldtProject', id])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
    posturl= settings.WEB_URL + reverse("ldt.ldt_utils.views.save_ldtProject")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
    language_code = request.LANGUAGE_CODE[:2]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
    ldt = get_object_or_404(Project, ldt_id=id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
    if ldt.state ==2: #published
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
        readonly = 'true'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
        readonly = 'false'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
    return render_to_response('ldt/ldt_utils/init_ldt.html', {'MEDIA_URL': settings.MEDIA_URL, 'colorurl': settings.MEDIA_URL+'swf/ldt/pkg/color.xml', 'i18nurl': settings.MEDIA_URL+'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.MEDIA_URL+'swf/ldt/', 'url': urlStr, 'posturl': posturl, 'id': id, 'readonly': readonly}, context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
def init(request, method, url):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
    ldtgen = LdtUtils()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
    doc = ldtgen.generateInit([url], 'ldt.ldt_utils.views.'+method, None)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
    resp = HttpResponse(mimetype="text/xml")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
    resp['Cache-Control']='no-cache, must-revalidate'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
    resp['Pragma']='no-cache'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
    xml.dom.ext.PrettyPrint(doc, resp)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
    return resp
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
       
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
def ldtProject(request, id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
    resp = HttpResponse(mimetype="text/xml")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
    resp['Cache-Control']='no-cache, must-revalidate'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
    resp['Pragma']='no-cache'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
    project = Project.objects.get(ldt_id=id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
    resp.write(project.ldt)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
    return resp
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
def project_json_id(request, id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
    project = get_object_or_404(Project,ldt_id=id)
81
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   218
        
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
    return project_json(request, project)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
def project_json_externalid(request, id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
    res_proj = get_list_or_404(Project.objects.order_by('-modification_date'),contents__external_id = id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
    return project_json(request, res_proj[0])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
def project_json(request, project):
81
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   231
    
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   232
    if not ldt_auth.checkAccess(request.user, project):
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   233
        return HttpResponseForbidden(_("You can not access this project"))
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   234
        
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
    mimetype = request.REQUEST.get("mimetype")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
    if mimetype is None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
        mimetype = "application/json; charset=utf-8"
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
        mimetype = mimetype.encode("utf-8")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
    if "charset" not in mimetype:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
        mimetype += "; charset=utf-8" 
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
    resp = HttpResponse(mimetype=mimetype)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
    resp['Cache-Control']='no-cache, must-revalidate'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
    resp['Pragma']='no-cache'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
    indent = request.REQUEST.get("indent")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
    if indent is None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
        indent = settings.LDT_JSON_DEFAULT_INDENT
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
        indent = int(indent)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
    callback = request.REQUEST.get("callback")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
    escape_str = request.REQUEST.get("escape")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
    escape_bool = False
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
    if escape_str:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
        escape_bool = {'true': True, 'false': False, "0": False, "1": True}.get(escape_str.lower())
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
    ps = ProjectSerializer(project)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
    project_dict = ps.serialize_to_cinelab()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
    json_str = simplejson.dumps(project_dict, ensure_ascii=False, indent=indent)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
    if callback is not None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
        json_str = "%s(%s)" % (callback,json_str)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
    if escape_bool:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
        json_str = escape(json_str)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
    resp.write(json_str)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
    return resp
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
83
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   274
def project_annotations_rdf(request, ldt_id):
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   275
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   276
    project = Project.objects.get(ldt_id=ldt_id);
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   277
    
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   278
    if not ldt_auth.checkAccess(request.user, project):
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   279
        return HttpResponseForbidden(_("You can not access this project"))
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   280
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   281
    mimetype = request.REQUEST.get("mimetype")
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   282
    if mimetype is None:
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   283
        mimetype = "application/rdf+xml; charset=utf-8"
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   284
    else:
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   285
        mimetype = mimetype.encode("utf-8")
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   286
    if "charset" not in mimetype:
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   287
        mimetype += "; charset=utf-8" 
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   288
    resp = HttpResponse(mimetype=mimetype)
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   289
    resp['Cache-Control']='no-cache, must-revalidate'
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   290
    resp['Pragma']='no-cache'
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   291
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   292
    annotations = project.getAnnotations(first_cutting=True)
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   293
    rdf_ns = u"http://www.w3.org/1999/02/22-rdf-syntax-ns#"
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   294
    dc_ns = u"http://purl.org/dc/elements/1.1/"
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   295
    rdf = u"{%s}" % rdf_ns
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   296
    dc = u"{%s}" % dc_ns
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   297
    nsmap = {u'rdf' : rdf_ns, u'dc':dc_ns}
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   298
    
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   299
    rdf_root = etree.Element(rdf+u"RDF", nsmap=nsmap)
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   300
    
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   301
    logging.debug("RDF annotations : " + repr(annotations))
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   302
    
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   303
    for annotation in annotations:
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   304
        uri = u""
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   305
        if 'uri' in annotation and annotation['uri']:
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   306
            uri = unicode(annotation['uri'])
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   307
        annot_desc = etree.SubElement(rdf_root, rdf+u"Description")
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   308
        annot_desc.set(rdf+u'about',uri)
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   309
        if annotation['title']:
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   310
            etree.SubElement(annot_desc, dc+'title').text = etree.CDATA(unicode(annotation['title']))
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   311
        if annotation['desc']:
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   312
            etree.SubElement(annot_desc, dc+'description').text = etree.CDATA(unicode(annotation['desc']))
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   313
        if annotation['tags']:
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   314
            for tag in annotation['tags']:
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   315
                etree.SubElement(annot_desc, dc+'subject').text = etree.CDATA(unicode(tag))
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   316
        etree.SubElement(annot_desc,dc+'coverage').text = u"start=%s, duration=%s" % (annotation['begin'], annotation['duration'])
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   317
        
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   318
    resp.write(u"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   319
    resp.write(u"<!DOCTYPE rdf:RDF PUBLIC \"-//DUBLIN CORE//DCMES DTD 2002/07/31//EN\" \"http://dublincore.org/documents/2002/07/31/dcmes-xml/dcmes-xml-dtd.dtd\">\n")
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   320
    
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   321
    resp.write(etree.tostring(rdf_root, xml_declaration=False, encoding="utf-8", pretty_print=True))
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   322
ec31a4bd86d3 corrections on evolutions
ymh <ymh.work@gmail.com>
parents: 82
diff changeset
   323
    return resp
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
def save_ldtProject(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
    if request.method=="POST":
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
        ldt = request.POST['ldt']
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
        id = request.POST['id']
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
        ldtproject=Project.objects.get(ldt_id=id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
        #save xml ldt
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
        ldtproject.ldt=ldt
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
        #get new title
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
        dom = xml.dom.minidom.parseString(ldt.encode( "utf-8" ))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
        con = xml.xpath.Context.Context(dom, 1, 1, None)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
        result = xml.xpath.Evaluate("/iri/project",context=con)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
        for pnode in result:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
            title=pnode.getAttribute("title")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
            break
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
        #set new title
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
        ldtproject.title=title
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
        #get new content list
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
        new_contents=[]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
        result = xml.xpath.Evaluate("/iri/medias/media", context=con)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
        for medianode in result:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
            id = medianode.attributes['id'].value
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
            new_contents.append(id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
        # set new content list
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
        for c in ldtproject.contents.all():
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
            if not c.iri_id in new_contents:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
                ldtproject.contents.remove(c)           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
        ldtproject.save()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
        ldt = ''
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
    return render_to_response('ldt/ldt_utils/save_done.html', {'ldt': ldt, 'id':id, 'title':title, 'contents': new_contents}, context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
@login_required    
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   357
def publish(request, id, redirect=True):
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
    ldt = get_object_or_404(Project, ldt_id=id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
    ldt.state = 2 #published
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
    ldt.save()
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   361
    redirect = boolean_convert(redirect)
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
    if redirect:
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   363
        return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt"))
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   364
    else:
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   365
        return HttpResponse(simplejson.dumps({'res':True, 'ldt': {'id': ldt.id, 'state':ldt.state,'ldt_id': ldt.ldt_id}}, ensure_ascii=False),mimetype='application/json')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
@login_required    
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   368
def unpublish(request, id, redirect=True):
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
    ldt = get_object_or_404(Project, ldt_id=id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
    ldt.state = 1 #edition
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
    ldt.save()
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   372
    redirect = boolean_convert(redirect)
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   373
    if redirect:
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   374
        return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt"))
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   375
    else:
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   376
        return HttpResponse(simplejson.dumps({'res':True, 'ldt': {'id': ldt.id, 'state':ldt.state,'ldt_id': ldt.ldt_id}}, ensure_ascii=False),mimetype='application/json')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
def index(request, url):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
    urlStr = settings.WEB_URL + reverse("ldt_init", args=['ldt',url])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
    language_code = request.LANGUAGE_CODE[:2]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
    return render_to_response('ldt/ldt_utils/init_ldt.html', {'MEDIA_URL': settings.MEDIA_URL, 'colorurl': settings.MEDIA_URL+'swf/ldt/pkg/color.xml', 'i18nurl': settings.MEDIA_URL+'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.MEDIA_URL+'swf/ldt/', 'url': urlStr, 'weburl':settings.WEB_URL+settings.BASE_URL}, context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
def ldt(request, url, startSegment = None):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
    import Ft
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
    from Ft.Xml import MarkupWriter
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
    resp = HttpResponse(mimetype="text/xml; charset=utf-8")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
    resp['Cache-Control'] = 'no-cache'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
    contentList = Content.objects.filter(iri_id=url)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
    ldtgen = LdtUtils()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
    ldtgen.generateLdt(contentList, file=resp, title = contentList[0].title, startSegment=startSegment)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
    return resp
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
def loading(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
     return render_to_response('ldt/ldt_utils/loading.html', context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
@login_required
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
def create_project(request, iri_id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
    content = get_object_or_404(Content, iri_id=iri_id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
    contents = [ content, ]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
    if request.method == "POST" :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
        form = AddProjectForm(request.POST)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
        if form.is_valid():
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
            user=request.user
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
            project = Project.create_project(title=form.cleaned_data['title'], user=user, contents=contents)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
            return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id]))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
        form = AddProjectForm()
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   420
    return render_to_response('ldt/ldt_utils/create_ldt.html', {'form':form, 'contents':contents, 'create_project_action':reverse("ldt.ldt_utils.views.create_project",args=[iri_id])}, context_instance=RequestContext(request))
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   421
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   422
@login_required
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   423
def update_project(request, ldt_id):
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   424
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   425
    project = get_object_or_404(Project, ldt_id=ldt_id)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   426
    contents = project.contents.all()
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   427
    if request.method == "POST" :
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   428
        submit_action = request.REQUEST.get("submit_button",False)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   429
        if submit_action == "prepare_delete":
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   430
            errors = []
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   431
            if project.state == 2:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   432
                errors.append(_("the project %(title)s is published. please unpublish before deleting.")%{'title':project.title})
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   433
                message = _("can not delete the project. Please correct the following error")
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   434
                title =  _('title error deleting project')
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   435
            else:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   436
                message = _("please confirm deleting project %(title)s")%{'title':project.title}
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   437
                title = _("confirm deletion")
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   438
            return render_to_response('ldt/ldt_utils/error_confirm.html', {'errors':errors, 'message':message, 'title': title}, context_instance=RequestContext(request))
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   439
        elif submit_action == "delete":
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   440
            if project.state != 2:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   441
                project.delete()
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   442
            form_status= 'deleted'
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   443
            form = AddProjectForm()
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   444
        else:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   445
            form_status= 'saved'
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   446
            form = AddProjectForm(request.POST)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   447
            if form.is_valid():
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   448
                project.title=form.cleaned_data['title']
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   449
                project.save()
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   450
    else:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   451
        form = AddProjectForm({'title':unicode(project.title)})
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   452
        form_status= 'none'
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   453
        
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   454
    return render_to_response('ldt/ldt_utils/create_ldt.html', {'form':form, 'form_status':form_status, 'ldt_id': ldt_id, 'contents':contents, 'create_project_action':reverse("ldt.ldt_utils.views.update_project",args=[ldt_id])}, context_instance=RequestContext(request))
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   455
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
@login_required
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
def copy_project(request, ldt_id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
    project = get_object_or_404(Project, ldt_id=ldt_id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
    if request.method == "POST" :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
        form = CopyProjectForm(request.POST)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
        if form.is_valid():
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
            user=request.user
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
            project = project.copy_project(title=request.POST['title'], user=user)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
            return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id]))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
        form = CopyProjectForm
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
    return render_to_response('ldt/ldt_utils/copy_ldt.html', {'form':form, 'project':project}, context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   471
71
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   472
def write_content_base(request, iri_id=None):
67
90fd14c649bb add edition for content
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
   473
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   474
    logging.debug(repr(request))
71
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   475
    if iri_id:        
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   476
        instance_content = Content.objects.get(iri_id=iri_id)
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   477
        instance_media = instance_content.media_obj
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   478
        logging.debug("write_content_base : valid form: for instance : media -> " + repr(instance_media) + " content : for instance : " + repr(instance_content) ) 
67
90fd14c649bb add edition for content
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
   479
    else:
71
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   480
        logging.debug("No iri_id")
67
90fd14c649bb add edition for content
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
   481
        instance_content = None
90fd14c649bb add edition for content
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
   482
        instance_media = None
42
71c93982463f add content creation dialog
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
   483
    
71c93982463f add content creation dialog
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
   484
    form_status= 'none'        
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   485
    if request.method =="POST":
67
90fd14c649bb add edition for content
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
   486
        content_form = ContentForm(request.POST, prefix="content", instance=instance_content)
90fd14c649bb add edition for content
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
   487
        media_form = MediaForm(request.POST, request.FILES, prefix="media", instance= instance_media)
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   488
        media_valid = media_form.is_valid()
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   489
        content_valid = content_form.is_valid()
71
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   490
        
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   491
        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))
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   492
        
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   493
        if media_valid and content_valid :
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   494
            
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   495
            # see if media must be created
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   496
            cleaned_data = {}
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   497
            cleaned_data.update(media_form.cleaned_data)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   498
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   499
            media_input_type = content_form.cleaned_data["media_input_type"]
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   500
            
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   501
            if media_input_type == "none":
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   502
                media = None
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   503
            elif media_input_type == "link":
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   504
                media = content_form.cleaned_data["media_obj"]
81
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   505
                created = False
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   506
            elif media_input_type == "create":
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   507
                del cleaned_data["media_file"]
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   508
                if not cleaned_data['videopath']:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   509
                    cleaned_data['videopath'] = settings.STREAM_URL
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   510
                media, created = Media.objects.get_or_create(src=cleaned_data['src'], defaults=cleaned_data)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   511
            elif media_input_type == "url" or  media_input_type == "upload" :                
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   512
                # copy file
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   513
                #complet src
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   514
                destination_file = None
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   515
                source_file = None
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   516
                destination_file_path = None
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   517
                try:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   518
                    if media_input_type == "url":
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   519
                        url = cleaned_data["external_src_url"]
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   520
                        source_file = urllib2.urlopen(url)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   521
                        source_filename = source_file.info().get('Content-Disposition', None)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   522
                        if not source_filename:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   523
                            source_filename = urlparse.urlparse(url).path.rstrip("/").split('/')[-1]
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   524
                    elif media_input_type == "upload":
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   525
                        source_file = request.FILES['media-media_file']
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   526
                        source_filename = source_file.name
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   527
                    
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   528
                    source_filename = ldt_utils_path.sanitize_filename(source_filename)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   529
                    destination_filepath = os.path.join(settings.STREAM_PATH, source_filename)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   530
                    base_source_filename = source_filename
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   531
                    extension = base_source_filename.split(".")[-1]
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   532
                    if extension == base_source_filename:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   533
                        extension = ""
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   534
                        base_basename_filename = base_source_filename
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   535
                    else:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   536
                        base_basename_filename  =  base_source_filename[:-1 *(len(extension)+1)]
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   537
                    i = 0
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   538
                    while os.path.exists(destination_filepath):
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   539
                        base_source_filename = "%s.%d.%s" % (base_basename_filename,i,extension)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   540
                        destination_filepath = os.path.join(settings.STREAM_PATH, base_source_filename)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   541
                        i += 1
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   542
                        
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   543
                    destination_file = open(destination_filepath, "w")
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   544
                    src_prefix = settings.STREAM_SRC_PREFIX.rstrip("/")
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   545
                    if len(src_prefix) > 0:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   546
                        cleaned_data["src"] = src_prefix + "/" + base_source_filename
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   547
                    else:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   548
                        cleaned_data["src"] =  base_source_filename
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   549
                    
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   550
                    chunck = source_file.read(2048)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   551
                    while chunck:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   552
                        destination_file.write(chunck)
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   553
                        chunck = source_file.read(2048)
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   554
        
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   555
                except Exception as inst:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   556
                    logging.debug("write_content_base : POST error when processing file:" + str(inst))
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   557
                    form_status = "error"
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   558
                    #set error for form
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   559
                    if media_input_type == "url":
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   560
                        errors = media_form._errors.setdefault("external_src_url", ErrorList())
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   561
                        errors.append(_("Problem when downloading file from url : ")+str(inst))
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   562
                    elif media_input_type == "upload":
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   563
                        errors = media_form._errors.setdefault("media_file", ErrorList())
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   564
                        errors.append(_("Problem when uploading file : ") + str(inst))
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   565
                finally:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   566
                    if destination_file:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   567
                        destination_file.close()
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   568
                    if source_file:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   569
                        source_file.close()
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   570
                        
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   571
                if form_status != "error":
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   572
                    #try:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   573
                    del cleaned_data["media_file"]
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   574
                    if not cleaned_data['videopath']:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   575
                        cleaned_data['videopath'] = settings.STREAM_URL
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   576
                    media, created = Media.objects.get_or_create(src=cleaned_data['src'], defaults=cleaned_data)
81
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   577
                else:
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   578
                    media = None
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   579
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   580
            if media and not created:                        
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   581
                for attribute in ('external_id', 'external_permalink', 'external_publication_url', 'external_src_url', 'media_creation_date', 'videopath', 'duration', 'description', 'title'):
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   582
                    setattr(media, attribute, cleaned_data.get(attribute))
97b12f5f2c7a first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
   583
                media.save()
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   584
                    #except Exception as inst:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   585
#                        logging.debug("write_content_base : POST error when saving media:" + str(inst))
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   586
 #                       form_status = "error"
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   587
                        #TODO: set error message
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   588
                        #media_form.errors = _("Error when saving the media : " + e.message)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   589
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   590
            #if needed preparetemp file and copy temp file to destination
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   591
            
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   592
            
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   593
            if form_status != "error":
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   594
                #try:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   595
                content_defaults = {}
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   596
                content_defaults.update(content_form.cleaned_data)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   597
                content_defaults['media_obj'] = media
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   598
                del content_defaults["media_input_type"]
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   599
                content, created = Content.objects.get_or_create(iri_id = content_form.cleaned_data['iri_id'], defaults = content_defaults)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   600
                if not created:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   601
                    
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   602
                    for attribute in ('iriurl', 'title', 'description', 'duration', 'content_creation_date', 'tags', 'media_obj'):
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   603
                        setattr(content, attribute, content_defaults[attribute])
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   604
                content.save()
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   605
                form_status = 'saved'
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   606
                media_form = MediaForm(instance=media, prefix="media")
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   607
                content_form = ContentForm(instance=content, prefix="content")
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   608
                #except:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   609
                    #logging.debug("write_content_base : POST error when saving content:" + str(inst))
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   610
                    #form_status = "error"
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   611
                    #TODO : set error on content form
42
71c93982463f add content creation dialog
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
   612
        else:
71c93982463f add content creation dialog
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
   613
            form_status = 'error'
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   614
    else:
42
71c93982463f add content creation dialog
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
   615
        form_status = 'empty'
71
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   616
        initial = { 'media_input_type':"link"}
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   617
        
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   618
        content_form = ContentForm(prefix="content", instance=instance_content, initial = initial )
67
90fd14c649bb add edition for content
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
   619
        media_form = MediaForm(prefix="media", instance=instance_media)
71
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   620
        
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   621
        if instance_content is not None:
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   622
            content_form.media_input_type = "link"
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   623
    
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   624
    return content_form, media_form, form_status
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   625
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   626
@login_required
71
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   627
def write_content(request, iri_id=None):
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   628
    
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   629
    submit_action = request.REQUEST.get("submit_button",False) 
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   630
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   631
    if submit_action == "prepare_delete": 
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   632
        errors, titles = prepare_delete_content(request, iri_id)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   633
        if errors and len(errors) > 0:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   634
            message = ungettext("There is %(count)d error when deleting content", "There are %(count)d errors when deleting content", len(errors)) % { 'count': len(errors)}
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   635
            title_msg = _('title error deleting content')
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   636
        else:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   637
            message = _("Confirm delete content %(titles)s") % { 'titles' : ",".join(titles) }
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   638
            title_msg = _("confirm delete content")
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   639
        return render_to_response('ldt/ldt_utils/error_confirm.html', {'errors':errors, 'message':message, 'title': title_msg}, context_instance=RequestContext(request))  
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   640
    elif submit_action == "delete":
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   641
        delete_content(request, iri_id)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   642
        form_status = "deleted"
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   643
        content_form = ContentForm()
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   644
        media_form = MediaForm()
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   645
    else:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   646
        content_form, media_form, form_status = write_content_base(request, iri_id)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   647
71
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   648
    if iri_id:
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   649
        create_content_action =  reverse(write_content, kwargs={'iri_id':iri_id})
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   650
    else:
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   651
        create_content_action =  reverse(write_content)
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   652
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   653
    
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   654
    return render_to_response('ldt/ldt_utils/create_content.html', {'content_form': content_form, 'media_form': media_form,'form_status': form_status,'create_content_action': create_content_action, 'iri_id': iri_id}, context_instance=RequestContext(request))
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   655
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   656
@login_required
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   657
def prepare_delete_content(request, iri_id=None):
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   658
    errors = []
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   659
    titles = []
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   660
    if not iri_id:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   661
        iri_id = request.REQUEST.get("iri_id", None)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   662
        
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   663
    if iri_id:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   664
        for content in Content.objects.filter(iri_id=iri_id):
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   665
            titles.append(unicode(content.title))
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   666
            projects = Content.objects.all()[0].project_set.all()
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   667
            projects_nb = len(projects)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   668
            if projects_nb > 0:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   669
                project_titles = [lambda p: p.title for p in projects]
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   670
                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':content.title,'count':projects_nb, 'project_titles': ",".join(project_titles)}) 
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   671
        
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   672
    return errors, titles
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   673
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   674
82
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   675
@login_required
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   676
def delete_content(request, iri_id=None):
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   677
    if not iri_id:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   678
        iri_id = request.REQUEST.get("iri_id", None)
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   679
        
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   680
    if iri_id:
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   681
        Content.objects.filter(iri_id=iri_id).delete()
9202807b4cec evolutions
ymh <ymh.work@gmail.com>
parents: 81
diff changeset
   682