src/ldt/ldt/ldt_utils/views.py
changeset 24 57a2650a7f87
parent 13 97ab7b3191cf
child 38 457d4ab675df
--- a/src/ldt/ldt/ldt_utils/views.py	Fri Feb 04 18:41:55 2011 +0100
+++ b/src/ldt/ldt/ldt_utils/views.py	Fri Feb 11 13:31:26 2011 +0100
@@ -23,7 +23,6 @@
 from ldt.ldt_utils.models import Content, Project, Owner
 from ldt.ldt_utils.projectserializer import ProjectSerializer
 from ldt.ldt_utils.utils import boolean_convert
-from lxml import etree
 from lxml.html import fromstring, fragment_fromstring
 from models import *
 from projectserializer import *
@@ -68,18 +67,32 @@
 
     project = Project.objects.get(ldt_id=ldt_id);
 
+    stream_mode = project.stream_mode
+    if stream_mode != "video":
+        stream_mode = 'radio'
+
+    player_width = 650
+    player_height = 480
+    
+    if stream_mode == 'radio':
+        player_height = 1
+        
     if not ldt_auth.checkAccess(request.user, project):
         return HttpResponseForbidden(_("You can not access this project"))
 
     ps = ProjectSerializer(project, from_contents=False, from_display=True)
     annotations = ps.getAnnotations(first_cutting=True)
+    
+    rend_dict = {'json_url':json_url, 'player_id':player_id, 'annotations':annotations, 'ldt_id': ldt_id, 'stream_mode': stream_mode, 'player_width': player_width, 'player_height': player_height}
 
     embed_rendered = dict((typestr,
-                           (lambda s:escape(lxml.etree.tostring(fragment_fromstring(render_to_string("ldt/ldt_utils/partial/embed_%s.html" % (s), {'json_url':json_url, 'player_id':player_id, 'annotations':annotations, 'ldt_id': ldt_id}, context_instance=RequestContext(request))), pretty_print=True)))(typestr))
+                           (lambda s:escape(lxml.etree.tostring(fragment_fromstring(render_to_string("ldt/ldt_utils/partial/embed_%s.html" % (s), rend_dict, context_instance=RequestContext(request))), pretty_print=True)))(typestr))
                            for typestr in ('player', 'seo_body', 'seo_meta', 'links'))
 
+    rend_dict['embed_rendered'] = embed_rendered
+
     return render_to_response("ldt/ldt_utils/embed_popup.html",
-                              {'json_url':json_url, 'player_id':player_id, 'embed_rendered':embed_rendered, 'annotations':annotations, 'ldt_id' : ldt_id},
+                              rend_dict,
                               context_instance=RequestContext(request))
 
 
@@ -87,7 +100,6 @@
 @login_required
 def projectsfilter(request, filter, is_owner=False, status=0):
 
-    project_list = None
     is_owner = boolean_convert(is_owner)
     status = int(status)
     query = Q()
@@ -219,13 +231,13 @@
 
     for resultMap in res:
         if iri_ids is None or resultMap['iri_id'] in iri_ids:
-	    seg = etree.SubElement(iri, 'seg')
-	    seg.set('idctt', resultMap['iri_id'])
+            seg = lxml.etree.SubElement(iri, 'seg')
+            seg.set('idctt', resultMap['iri_id'])
             seg.set('idens', resultMap['ensemble_id'])
             seg.set('iddec', resultMap['decoupage_id'])
             seg.set('idseg', resultMap['element_id'])
             seg.set('idvue', "")
-	    seg.set('crit', "")
+            seg.set('crit', "")
 
     return doc  
   
@@ -298,7 +310,7 @@
     resp['Cache-Control'] = 'no-cache, must-revalidate'
     resp['Pragma'] = 'no-cache'
 
-    resp.write(etree.tostring(doc, pretty_print=True, xml_declaration=True, encoding="utf-8")) 
+    resp.write(lxml.etree.tostring(doc, pretty_print=True, xml_declaration=True, encoding="utf-8")) 
     return resp
        
 def ldtProject(request, id):
@@ -397,7 +409,7 @@
     dc = u"{%s}" % dc_ns
     nsmap = {u'rdf' : rdf_ns, u'dc':dc_ns}
     
-    rdf_root = etree.Element(rdf + u"RDF", nsmap=nsmap)
+    rdf_root = lxml.etree.Element(rdf + u"RDF", nsmap=nsmap)
     
     logging.debug("RDF annotations : " + repr(annotations))
     
@@ -405,21 +417,21 @@
         uri = u""
         if 'uri' in annotation and annotation['uri']:
             uri = unicode(annotation['uri'])
-        annot_desc = etree.SubElement(rdf_root, rdf + u"Description")
+        annot_desc = lxml.etree.SubElement(rdf_root, rdf + u"Description")
         annot_desc.set(rdf + u'about', uri)
         if annotation['title']:
-            etree.SubElement(annot_desc, dc + 'title').text = etree.CDATA(unicode(annotation['title']))
+            lxml.etree.SubElement(annot_desc, dc + 'title').text = lxml.etree.CDATA(unicode(annotation['title']))
         if annotation['desc']:
-            etree.SubElement(annot_desc, dc + 'description').text = etree.CDATA(unicode(annotation['desc']))
+            lxml.etree.SubElement(annot_desc, dc + 'description').text = lxml.etree.CDATA(unicode(annotation['desc']))
         if annotation['tags']:
             for tag in annotation['tags']:
-                etree.SubElement(annot_desc, dc + 'subject').text = etree.CDATA(unicode(tag))
-        etree.SubElement(annot_desc, dc + 'coverage').text = u"start=%s, duration=%s" % (annotation['begin'], annotation['duration'])
+                lxml.etree.SubElement(annot_desc, dc + 'subject').text = lxml.etree.CDATA(unicode(tag))
+        lxml.etree.SubElement(annot_desc, dc + 'coverage').text = u"start=%s, duration=%s" % (annotation['begin'], annotation['duration'])
         
     resp.write(u"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
     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")
     
-    resp.write(etree.tostring(rdf_root, xml_declaration=False, encoding="utf-8", pretty_print=True))
+    resp.write(lxml.etree.tostring(rdf_root, xml_declaration=False, encoding="utf-8", pretty_print=True))
 
     return resp
 
@@ -429,7 +441,7 @@
         id = request.POST['id']
         ldtproject = Project.objects.get(ldt_id=id)
 
-	    #save xml ldt
+        #save xml ldt
         ldtproject.ldt = ldt
 
 
@@ -505,7 +517,7 @@
 
 
 def loading(request):
-     return render_to_response('ldt/ldt_utils/loading.html', context_instance=RequestContext(request))
+    return render_to_response('ldt/ldt_utils/loading.html', context_instance=RequestContext(request))
 
 
 @login_required
@@ -629,6 +641,7 @@
             # see if media must be created
             cleaned_data = {}
             cleaned_data.update(media_form.cleaned_data)
+                        
 
             media_input_type = content_form.cleaned_data["media_input_type"]
             
@@ -647,7 +660,6 @@
                 #complet src
                 destination_file = None
                 source_file = None
-                destination_file_path = None
                 try:
                     if media_input_type == "url":
                         url = cleaned_data["external_src_url"]
@@ -707,6 +719,10 @@
                     del cleaned_data["media_file"]
                     if not cleaned_data['videopath']:
                         cleaned_data['videopath'] = settings.STREAM_URL
+                    mimetype = cleaned_data.get('mimetype_field',None)
+                    if not mimetype:
+                        mimetype = mimetypes.guess_type(cleaned_data['src'])
+                    cleaned_data['mimetype_field'] = mimetype
                     media, created = Media.objects.get_or_create(src=cleaned_data['src'], defaults=cleaned_data)
                 else:
                     media = None
@@ -714,14 +730,12 @@
             if media and not created:                        
                 for attribute in ('external_id', 'external_permalink', 'external_publication_url', 'external_src_url', 'media_creation_date', 'videopath', 'duration', 'description', 'title'):
                     setattr(media, attribute, cleaned_data.get(attribute))
+                mimetype = cleaned_data.get('mimetype_field',None)
+                if not mimetype:
+                    mimetype = mimetypes.guess_type(media.src) 
+                media.mimetype_field = mimetype
+                    
                 media.save()
-                    #except Exception as inst:
-#                        logging.debug("write_content_base : POST error when saving media:" + str(inst))
- #                       form_status = "error"
-                        #TODO: set error message
-                        #media_form.errors = _("Error when saving the media : " + e.message)
-
-            #if needed preparetemp file and copy temp file to destination
             
             
             if form_status != "error":
@@ -739,10 +753,6 @@
                 form_status = 'saved'
                 media_form = MediaForm(instance=media, prefix="media")
                 content_form = ContentForm(instance=content, prefix="content")
-                #except:
-                    #logging.debug("write_content_base : POST error when saving content:" + str(inst))
-                    #form_status = "error"
-                    #TODO : set error on content form
         else:
             form_status = 'error'
     else: