web/ldt/ldt_utils/views.py
author ymh <ymh.work@gmail.com>
Tue, 21 Sep 2010 04:27:08 +0200
changeset 62 39b2dab4f939
parent 60 a8ad7ebf5902
child 67 90fd14c649bb
permissions -rw-r--r--
separate contet and media
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import django.core.urlresolvers
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
from django.http import HttpResponse, HttpResponseRedirect
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
from django.template import RequestContext
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
from django.core.urlresolvers import reverse
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
from django.contrib.auth.decorators import login_required
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from django.conf import settings
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
from django.core import serializers
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
from django.utils import simplejson
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from django.utils.html import escape
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from fileimport import *
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    12
from forms import LdtImportForm, LdtAddForm, SearchForm, AddProjectForm, CopyProjectForm, ContentForm, MediaForm
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
from ldt.core.models import Owner
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
from models import *
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
from utils import *
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
from contentindexer import *
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
from projectserializer import *
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
from string import Template
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
from Ft.Xml import MarkupWriter
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
import cgi
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
import uuid
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
import base64
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
import lucene
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
import xml.dom
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
import xml.dom.ext
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
import xml.dom.minidom
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    27
import tempfile
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    28
import urllib2
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    29
from urllib2 import urlparse
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    30
from jogging import logging
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
def searchForm(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    form = SearchForm()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    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
    37
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
def searchIndex(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    sform = SearchForm(request.POST)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    if sform.is_valid():
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        search = sform.cleaned_data["search"]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        queryStr = base64.urlsafe_b64encode(search.encode('utf8'))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        field = request.POST["field"]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        language_code = request.LANGUAGE_CODE[:2]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        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
    50
        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
    51
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        resp = HttpResponse()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        resp.write("<html><head></head><body>Error : No result</body></html>");
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
def searchIndexGet(request, field, query):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
    language_code = request.LANGUAGE_CODE[:2]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    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
    59
    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
    60
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
def searchInit(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
    ldtgen = LdtUtils()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    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
    66
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    resp = HttpResponse(mimetype="text/xml;charset=utf-8")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    xml.dom.ext.PrettyPrint(doc, resp)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    return resp
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
def searchLdt(request, field, query, edition=None):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
    contentList = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    resp = HttpResponse(mimetype="text/xml")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    queryStr = ""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    if query and len(query)>0:        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        queryStr = base64.urlsafe_b64decode(query.encode("ascii")).decode("utf8")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        searcher = LdtSearch()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
        ids = {}
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
        for result in searcher.query(field, queryStr):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
            ids[result["iri_id"]] = ""            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        id_list = ids.keys()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        if edition is not None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
            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
    89
            id_list = filter(lambda id: id in id_list, ids_editions)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
        contentList = Content.objects.filter(iri_id__in=id_list)        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
    ldtgen = LdtUtils()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
    ldtgen.generateLdt(contentList, file=resp, title = u"Recherche : " + queryStr)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
    return resp
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
def searchSegments(request, field, query, edition=None):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
    if query and len(query)>0:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
        searcher = LdtSearch()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        queryStr = base64.urlsafe_b64decode(query.encode("ascii")).decode("utf8")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        res = searcher.query(field, queryStr)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        res = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
    iri_ids = None
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
    if edition is not None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        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
   114
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
    doc = xml.dom.getDOMImplementation().createDocument(None, "iri", None)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
    for resultMap in res:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
        if iri_ids is None or resultMap['iri_id'] in iri_ids:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
            elem = doc.createElement('seg')
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
            elem.setAttribute('idctt', resultMap['iri_id'])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
            elem.setAttribute('idens', resultMap['ensemble_id'])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
            elem.setAttribute('iddec', resultMap['decoupage_id'])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
            elem.setAttribute('idseg', resultMap['element_id'])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
            elem.setAttribute('idvue', "")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
            elem.setAttribute('crit', "")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
            doc.documentElement.appendChild(elem)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
    return HttpResponse(doc.toprettyxml(encoding='utf-8'), mimetype="text/xml;charset=utf-8")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
@login_required         
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
def list_ldt(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
    contents = Content.objects.all()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
    try:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
        owner = Owner.objects.get(user=request.user)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
    except:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
        return HttpResponseRedirect(settings.LOGIN_URL)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
    ldtProjects = Project.objects.filter(owner=owner)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
    context={
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
    'contents': contents,
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   140
    'projects': ldtProjects.reverse(),
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
    }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
    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
   143
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   144
@login_required         
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   145
def list_content(request):
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   146
    contents = Content.objects.all()
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   147
    context={
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   148
        'contents': contents,
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   149
    }
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   150
    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
   151
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   152
@login_required
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
def create_ldt_view(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
    if request.method == "POST" :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
        form = LdtAddForm(request.POST)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
        if form.is_valid():
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
            user = request.user
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
            Project.create_project(title=form.cleaned_data['title'], user=user, contents=form.cleaned_data['contents'])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
            return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt"))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
        form = LdtAddForm()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
        contents = Content.objects.all()    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
        return render_to_response('ldt/ldt_utils/create_ldt.html', {'contents': contents, 'form': form,'create_project_action':reverse(create_ldt_view)}, context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
     
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
def created_ldt(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
    return render_to_response('ldt/ldt_utils/done.html', context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
def indexProject(request, id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
    urlStr = settings.WEB_URL + reverse("space_ldt_init", args=['ldtProject', id])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
    posturl= settings.WEB_URL + reverse("ldt.ldt_utils.views.save_ldtProject")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
    language_code = request.LANGUAGE_CODE[:2]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
    ldt = get_object_or_404(Project, ldt_id=id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
    if ldt.state ==2: #published
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
        readonly = 'true'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
        readonly = 'false'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
    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
   181
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
def init(request, method, url):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
    ldtgen = LdtUtils()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
    doc = ldtgen.generateInit([url], 'ldt.ldt_utils.views.'+method, None)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
    resp = HttpResponse(mimetype="text/xml")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
    resp['Cache-Control']='no-cache, must-revalidate'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
    resp['Pragma']='no-cache'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
    xml.dom.ext.PrettyPrint(doc, resp)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
    return resp
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
       
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
def ldtProject(request, id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
    resp = HttpResponse(mimetype="text/xml")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
    resp['Cache-Control']='no-cache, must-revalidate'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
    resp['Pragma']='no-cache'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
    project = Project.objects.get(ldt_id=id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
    resp.write(project.ldt)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
    return resp
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
def project_json_id(request, id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
    project = get_object_or_404(Project,ldt_id=id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
    return project_json(request, project)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
def project_json_externalid(request, id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
    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
   213
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
    return project_json(request, res_proj[0])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
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(request, project):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
    mimetype = request.REQUEST.get("mimetype")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
    if mimetype is None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
        mimetype = "application/json; charset=utf-8"
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
        mimetype = mimetype.encode("utf-8")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
    if "charset" not in mimetype:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
        mimetype += "; charset=utf-8" 
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
    resp = HttpResponse(mimetype=mimetype)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
    resp['Cache-Control']='no-cache, must-revalidate'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
    resp['Pragma']='no-cache'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
    indent = request.REQUEST.get("indent")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
    if indent is None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
        indent = settings.LDT_JSON_DEFAULT_INDENT
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
        indent = int(indent)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
    callback = request.REQUEST.get("callback")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
    escape_str = request.REQUEST.get("escape")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
    escape_bool = False
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
    if escape_str:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
        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
   242
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
    ps = ProjectSerializer(project)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
    project_dict = ps.serialize_to_cinelab()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
    json_str = simplejson.dumps(project_dict, ensure_ascii=False, indent=indent)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
    if callback is not None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
        json_str = "%s(%s)" % (callback,json_str)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
    if escape_bool:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
        json_str = escape(json_str)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
    resp.write(json_str)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
    return resp
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
def save_ldtProject(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
    if request.method=="POST":
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
        ldt = request.POST['ldt']
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
        id = request.POST['id']
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
        ldtproject=Project.objects.get(ldt_id=id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
        #save xml ldt
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
        ldtproject.ldt=ldt
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
        #get new title
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
        dom = xml.dom.minidom.parseString(ldt.encode( "utf-8" ))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
        con = xml.xpath.Context.Context(dom, 1, 1, None)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
        result = xml.xpath.Evaluate("/iri/project",context=con)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
        for pnode in result:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
            title=pnode.getAttribute("title")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
            break
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
        #set new title
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
        ldtproject.title=title
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
        #get new content list
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
        new_contents=[]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
        result = xml.xpath.Evaluate("/iri/medias/media", context=con)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
        for medianode in result:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
            id = medianode.attributes['id'].value
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
            new_contents.append(id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
        # set new content list
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
        for c in ldtproject.contents.all():
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
            if not c.iri_id in new_contents:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
                ldtproject.contents.remove(c)           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
        ldtproject.save()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
        ldt = ''
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
    return render_to_response('ldt/ldt_utils/save_done.html', {'ldt': ldt, 'id':id, 'title':title, 'contents': new_contents}, context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
@login_required    
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   292
def publish(request, id, redirect=True):
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
    ldt = get_object_or_404(Project, ldt_id=id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
    ldt.state = 2 #published
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
    ldt.save()
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   296
    redirect = boolean_convert(redirect)
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   297
    if redirect:
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   298
        return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt"))
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   299
    else:
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   300
        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
   301
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
@login_required    
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   303
def unpublish(request, id, redirect=True):
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
    ldt = get_object_or_404(Project, ldt_id=id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
    ldt.state = 1 #edition
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
    ldt.save()
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   307
    redirect = boolean_convert(redirect)
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
    if redirect:
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   309
        return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt"))
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   310
    else:
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   311
        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
   312
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
def index(request, url):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
    urlStr = settings.WEB_URL + reverse("ldt_init", args=['ldt',url])
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
    language_code = request.LANGUAGE_CODE[:2]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
    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
   320
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
def ldt(request, url, startSegment = None):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
    import Ft
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
    from Ft.Xml import MarkupWriter
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
    resp = HttpResponse(mimetype="text/xml; charset=utf-8")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
    resp['Cache-Control'] = 'no-cache'
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
    contentList = Content.objects.filter(iri_id=url)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
    ldtgen = LdtUtils()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
    ldtgen.generateLdt(contentList, file=resp, title = contentList[0].title, startSegment=startSegment)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
    return resp
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
def loading(request):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
     return render_to_response('ldt/ldt_utils/loading.html', context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
@login_required
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
def create_project(request, iri_id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
    content = get_object_or_404(Content, iri_id=iri_id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
    contents = [ content, ]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
    if request.method == "POST" :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
        form = AddProjectForm(request.POST)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
        if form.is_valid():
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
            user=request.user
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
            project = Project.create_project(title=form.cleaned_data['title'], user=user, contents=contents)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
            return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id]))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
        form = AddProjectForm()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
    return render_to_response('ldt/ldt_utils/create_ldt.html', {'form':form, 'contents':contents, 'iri_id':iri_id, 'create_project_action':reverse("ldt.ldt_utils.views.create_project",args=[iri_id])}, context_instance=RequestContext(request))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
@login_required
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
def copy_project(request, ldt_id):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
    project = get_object_or_404(Project, ldt_id=ldt_id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
    if request.method == "POST" :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
        form = CopyProjectForm(request.POST)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
        if form.is_valid():
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
            user=request.user
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
            project = project.copy_project(title=request.POST['title'], user=user)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
            return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id]))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
    else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
        form = CopyProjectForm
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
    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
   370
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   371
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   372
def write_content_base(request):
42
71c93982463f add content creation dialog
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
   373
    
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   374
    logging.debug("write_content_base")
42
71c93982463f add content creation dialog
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
   375
    form_status= 'none'        
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   376
    if request.method =="POST":
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   377
        logging.debug("write_content_base : POST")
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   378
        content_form = ContentForm(request.POST, prefix="content")
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   379
        media_form = MediaForm(request.POST, request.FILES, prefix="media")
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   380
        media_valid = media_form.is_valid()
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   381
        content_valid = content_form.is_valid()
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   382
        if media_valid and content_valid :
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   383
            
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   384
            # see if media must be created
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   385
            cleaned_data = {}
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   386
            cleaned_data.update(media_form.cleaned_data)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   387
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   388
            media_input_type = content_form.cleaned_data["media_input_type"]
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   389
            
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   390
            logging.debug("write_content_base : POST media_input_type:" + media_input_type)
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   391
            
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   392
            if media_input_type == "none":
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   393
                media = None
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   394
            elif media_input_type == "link":
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   395
                media = content_form.cleaned_data["media_obj"]
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   396
            elif media_input_type == "url" or  media_input_type == "upload" or  media_input_type == "create":                
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   397
                # copy file
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   398
                #complet src
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   399
                destination_file = None
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   400
                source_file = None
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   401
                destination_file_path = None
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   402
                if media_input_type != "create":
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   403
                    try:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   404
                        if media_input_type == "url":
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   405
                            url = cleaned_data["external_src_url"]
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   406
                            source_file = urllib2.urlopen(url)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   407
                            source_filename = source_file.info().get('Content-Disposition', None)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   408
                            if not source_filename:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   409
                                source_filename = urlparse.urlparse(url).path.rstrip("/").split('/')[-1]
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   410
                        elif media_input_type == "upload":
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   411
                            source_file = request.FILES['media-media_file']
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   412
                            source_filename = source_file.name
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   413
                            
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   414
    
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   415
                        destination_filepath = os.path.join(settings.STREAM_PATH, source_filename)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   416
                        base_source_filename = source_filename
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   417
                        i = 0
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   418
                        while os.path.exists(destination_filepath):
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   419
                            base_source_filename = source_filename+"(%d)" % (i)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   420
                            destination_filepath = os.path.join(settings.STREAM_PATH, base_source_filename)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   421
                            i += 1
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   422
                            
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   423
                        destination_file = open(destination_filepath, "w")
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   424
                        src_prefix = settings.STREAM_SRC_PREFIX.rstrip("/")
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   425
                        if len(src_prefix) > 0:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   426
                            cleaned_data["src"] = src_prefix + "/" + base_source_filename
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   427
                        else:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   428
                            cleaned_data["src"] =  base_source_filename
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   429
                        
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   430
                        chunck = source_file.read(2048)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   431
                        while chunck:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   432
                            destination_file.write(chunck)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   433
                            chunck = source_file.read(2048)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   434
            
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   435
                    except Exception as inst:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   436
                        logging.debug("write_content_base : POST error when processing file:" + str(inst))
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   437
                        form_status = "error"
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   438
                    finally:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   439
                        if destination_file:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   440
                            destination_file.close()
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   441
                        if source_file:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   442
                            source_file.close()
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   443
                        
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   444
                if form_status != "error":
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   445
                    #try:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   446
                    del cleaned_data["media_file"]
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   447
                    if not cleaned_data['videopath']:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   448
                        cleaned_data['videopath'] = settings.STREAM_URL
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   449
                    media, created = Media.objects.get_or_create(src=cleaned_data['src'], defaults=cleaned_data)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   450
                    if not created:                        
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   451
                        for attribute in ('external_id', 'external_permalink', 'external_publication_url', 'external_src_url', 'media_creation_date', 'videopath', 'duration', 'description', 'title'):
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   452
                            setattr(media, attribute, cleaned_data.get(attribute))
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   453
                    media.save()
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   454
                    #except Exception as inst:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   455
#                        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
   456
 #                       form_status = "error"
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   457
                        #TODO: set error message
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   458
                        #media_form.errors = _("Error when saving the media : " + e.message)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   459
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   460
            #if needed preparetemp file and copy temp file to destination
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   461
            
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   462
            
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   463
            if form_status != "error":
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   464
                #try:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   465
                content_defaults = {}
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   466
                content_defaults.update(content_form.cleaned_data)
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   467
                content_defaults['media_obj'] = media
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   468
                del content_defaults["media_input_type"]
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   469
                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
   470
                if not created:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   471
                    
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   472
                    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
   473
                        setattr(content, attribute, content_defaults[attribute])
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   474
                content.save()
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   475
                form_status = 'saved'
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   476
                media_form = MediaForm(instance=media, prefix="media")
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   477
                content_form = ContentForm(instance=content, prefix="content")
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   478
                #except:
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   479
                    #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
   480
                    #form_status = "error"
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   481
                    #TODO : set error on content form
42
71c93982463f add content creation dialog
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
   482
        else:
71c93982463f add content creation dialog
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
   483
            form_status = 'error'
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   484
    else:
42
71c93982463f add content creation dialog
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
   485
        form_status = 'empty'
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   486
        content_form = ContentForm(prefix="content")
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   487
        media_form = MediaForm(prefix="media")
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   488
    
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   489
    return content_form, media_form, form_status
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   490
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   491
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   492
def write_content(request):
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   493
    
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   494
    content_form, media_form, form_status = write_content_base(request)
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   495
    
60
a8ad7ebf5902 various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   496
    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': reverse(write_content)}, context_instance=RequestContext(request))
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   497
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   498
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
   499