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