Enhance search and front template : add begin and duration to searched segments.
authorcavaliet
Wed, 11 Jan 2012 16:19:38 +0100
changeset 349 63f729155d81
parent 348 d211a78da395
child 352 6fa2e1f8ddaa
Enhance search and front template : add begin and duration to searched segments.
src/ldt/ldt/indexation/__init__.py
src/ldt/ldt/ldt_utils/contentindexer.py
src/ldt/ldt/ldt_utils/models.py
src/ldt/ldt/ldt_utils/templates/front/front_group.html
src/ldt/ldt/ldt_utils/templates/front/front_home.html
src/ldt/ldt/ldt_utils/templates/front/front_search_results.html
src/ldt/ldt/ldt_utils/views/workspace.py
src/ldt/ldt/templatetags/str_duration.py
--- a/src/ldt/ldt/indexation/__init__.py	Wed Jan 11 12:38:06 2012 +0100
+++ b/src/ldt/ldt/indexation/__init__.py	Wed Jan 11 16:19:38 2012 +0100
@@ -23,12 +23,16 @@
         title = doc.getField('title').stringValue()
         desc = doc.getField('abstract').stringValue()
         tags = doc.getField('tags').stringValue()
+        begin = doc.getField('begin').stringValue()
+        duration = doc.getField('duration').stringValue()
 
         ids['context'] = desc
         ids['title'] = title
         ids['tags'] = tags
         ids['score'] = score
         ids['lucene_id'] = i.doc
+        ids['begin'] = begin
+        ids['duration'] = duration
         contexts.append(ids)     
     
     searcher.close()
--- a/src/ldt/ldt/ldt_utils/contentindexer.py	Wed Jan 11 12:38:06 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/contentindexer.py	Wed Jan 11 16:19:38 2012 +0100
@@ -101,7 +101,9 @@
                 doc.add(lucene.Field("tags", tags, lucene.Field.Store.YES, lucene.Field.Index.ANALYZED))
                 doc.add(lucene.Field("title", title, lucene.Field.Store.YES, lucene.Field.Index.ANALYZED))
                 doc.add(lucene.Field("abstract", abstract, lucene.Field.Store.YES, lucene.Field.Index.ANALYZED))
-                doc.add(lucene.Field("all", " ".join([tags, title, abstract]), lucene.Field.Store.YES, lucene.Field.Index.ANALYZED))
+                doc.add(lucene.Field("all", " ".join([tags, title, abstract]), lucene.Field.Store.YES, lucene.Field.Index.ANALYZED))        
+                doc.add(lucene.Field("begin", str(start_ts), lucene.Field.Store.YES, lucene.Field.Index.NOT_ANALYZED))
+                doc.add(lucene.Field("duration", str(duration), lucene.Field.Store.YES, lucene.Field.Index.NOT_ANALYZED))
 
                 seg = Segment(content=content,
                               iri_id=content.iri_id,
--- a/src/ldt/ldt/ldt_utils/models.py	Wed Jan 11 12:38:06 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/models.py	Wed Jan 11 16:19:38 2012 +0100
@@ -158,20 +158,6 @@
             self.save()
         return self.duration    
     
-    def duration_str(self):
-        if self.duration is None:
-            return "?"
-        # We take off the milliseconds
-        sec = self.duration//1000
-        if sec < 60 :
-            return str(dur) + "s"
-        hours = sec//3600
-        min = (sec - (hours * 3600))//60
-        if min<10:
-            min_str = "0" + str(min)
-        else:
-            min_str = str(min)
-        return str(hours) + "h" + min_str
     
     def mimetype(): #@NoSelf
         def fget(self):
@@ -460,7 +446,7 @@
         self.save()
         
     def unpublish(self):
-        if not self.pk():
+        if not self.pk:
             self.save()
         self.state = 1
         everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
--- a/src/ldt/ldt/ldt_utils/templates/front/front_group.html	Wed Jan 11 12:38:06 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/templates/front/front_group.html	Wed Jan 11 16:19:38 2012 +0100
@@ -1,6 +1,7 @@
 {% extends "front/front_base.html" %}
 {% load i18n %}
 {% load thumbnail %}
+{% load str_duration %}
 
 {% block title %}Lignes de temps : groupe "{{group.name}}"{% endblock %}
 
@@ -21,7 +22,7 @@
         <a href="{% url ldt.ldt_utils.views.front.annot_content content.iri_id %}">{% thumbnail content.image "134x75" format="PNG" crop="center" as im %}<img src="{{ im.url }}" class="img_media" width="{{ im.width }}" height="{{ im.height }}">{% endthumbnail %}</a>
         <div class="bulle_annot">120</div>
         <p><a href="{% url ldt.ldt_utils.views.front.annot_content content.iri_id %}"><b>{% if content.title|length > 69 %}{{content.title|slice:":69"}}...{% else %}{{content.title}}{% endif %}</b></a></p>
-        <p class="font_11">{% trans 'by' %} IRI | {{content.duration_str}}</p>
+        <p class="font_11">{% trans 'by' %} IRI | {{content.duration|str_duration:"h"}}</p>
     </li>
     {% endfor %}
 </ul>
--- a/src/ldt/ldt/ldt_utils/templates/front/front_home.html	Wed Jan 11 12:38:06 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/templates/front/front_home.html	Wed Jan 11 16:19:38 2012 +0100
@@ -1,6 +1,8 @@
 {% extends "front/front_base.html" %}
 {% load i18n %}
 {% load thumbnail %}
+{% load str_duration %}
+
 
 {% block title %}Lignes de temps : Home{% endblock %}
 
@@ -21,7 +23,7 @@
         <a href="{% url ldt.ldt_utils.views.front.annot_content content.iri_id %}">{% thumbnail content.image "294x165" format="PNG" crop="center" as im %}<img src="{{ im.url }}" class="img_media" width="{{ im.width }}" height="{{ im.height }}">{% endthumbnail %}</a>
         <div class="bulle_annot">{{ content.nb_annotation }}</div>
         <p><a href="{% url ldt.ldt_utils.views.front.annot_content content.iri_id %}"><b>{{content.title}}</b></a></p>
-        <p>{% trans 'by' %} IRI | {{content.duration_str}}</p>
+        <p>{% trans 'by' %} IRI | {{content.duration|str_duration:"h"}}</p>
     </li>
     {% endfor %}
 </ul>
@@ -35,7 +37,7 @@
         <a href="{% url ldt.ldt_utils.views.front.annot_content content.iri_id %}">{% thumbnail content.image "134x75" format="PNG" crop="center" as im %}<img src="{{ im.url }}" class="img_media" width="{{ im.width }}" height="{{ im.height }}">{% endthumbnail %}</a>
         <div class="bulle_annot">{{ content.nb_annotation }}</div>
         <p><a href="{% url ldt.ldt_utils.views.front.annot_content content.iri_id %}"><b>{% if content.title|length > 69 %}{{content.title|slice:":69"}}...{% else %}{{content.title}}{% endif %}</b></a></p>
-        <p class="font_11">{% trans 'by' %} IRI | {{content.duration_str}}</p>
+        <p class="font_11">{% trans 'by' %} IRI | {{content.duration|str_duration:"h"}}</p>
     </li>
     {% endfor %}
 </ul>
--- a/src/ldt/ldt/ldt_utils/templates/front/front_search_results.html	Wed Jan 11 12:38:06 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/templates/front/front_search_results.html	Wed Jan 11 16:19:38 2012 +0100
@@ -1,6 +1,7 @@
 {% extends "front/front_base.html" %}
 {% load i18n %}
 {% load thumbnail %}
+{% load str_duration %}
 
 {% block title %}Lignes de temps : {% trans 'search' %} ""{% endblock %}
 
@@ -65,7 +66,7 @@
                         </div>
                         <h4 class="title_segment"><a class="blue under" href="{% url ldt.ldt_utils.views.lignesdetemps.index_segment segment.project_id segment.iri_id segment.ensemble_id segment.decoupage_id segment.element_id %}">
                           {% if segment.title %}{{ segment.title }}{% else %}{% trans "No title" %}{% endif %}</a></h4>
-                        <p class="text_segment">{% if segment.context %}{{ segment.context }}{% endif %}</h4>
+                        <p class="text_segment">{% if segment.context %}{{ segment.context }}{% endif %}<br/>begin : {{ segment.begin|str_duration:"::" }} - dur : {{ segment.duration|str_duration:"::" }}</h4>
                     </li>
 	                <!--li class="segmentinfos" ><span  class="" title="{% trans 'open ldt' %}"><a class="ldt_link_open_ldt" href="{% url ldt.ldt_utils.views.lignesdetemps.index_segment segment.project_id segment.iri_id segment.ensemble_id segment.decoupage_id segment.element_id %}"-->
 	                </li>
--- a/src/ldt/ldt/ldt_utils/views/workspace.py	Wed Jan 11 12:38:06 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/views/workspace.py	Wed Jan 11 16:19:38 2012 +0100
@@ -167,7 +167,8 @@
         else:
             results = get_results_with_context(field, search)     
             complete_results = []
-            proj_list = Project.safe_objects.all()
+            # the search is made only on the published project
+            proj_list = Project.safe_objects.filter(state=2)
             results.sort(key=lambda k: k['iri_id'])
             for iri_id, item in groupby(results, itemgetter('iri_id')):                
                 try: 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ldt/ldt/templatetags/str_duration.py	Wed Jan 11 16:19:38 2012 +0100
@@ -0,0 +1,46 @@
+from django.template import Library, TemplateSyntaxError
+
+register = Library()
+
+@register.filter
+def str_duration(value, arg=None):
+    """Takes an integer value of milliseconds and write a human readable duration like 1h23, 01:23 (hours:minutes), 01:23:45 (hours:minutes:seconds), or number of seconds """
+    # Error management
+    if value is None :
+        return ""
+    if not isinstance(value, (int,long,float,str,unicode)) :
+        raise TemplateSyntaxError('str_duration value error : value must be integer or long or float or string. type = ' + str(type(value)))
+    if isinstance(value, (str,unicode)) :
+        try:
+            value = int(value)
+        except :
+            raise TemplateSyntaxError('str_duration value error : can not convert value "' + value + '" into integer')
+    # We take off the milliseconds
+    ms = abs(value)
+    sec = ms//1000
+    if arg is None :
+        arg = "::"
+    if arg=="::" or arg=="h" or arg==":" :
+        hours = sec//3600
+        min = (sec - (hours * 3600))//60
+        if min<10:
+            min_str = "0" + str(min)
+        else:
+            min_str = str(min)
+        if (arg=="::" or arg==":") and hours<10 :
+            hours_str = "0" + str(hours)
+        else :
+            hours_str = str(hours)
+        if arg=="h" or arg==":" :
+            return hours_str + arg + min_str
+        sec = (sec - (hours * 3600) - (min*60))
+        if sec<10:
+            sec_str = "0" + str(sec)
+        else:
+            sec_str = str(sec)
+        return hours_str + ":" + min_str + ":" + sec_str
+    elif arg=="s" :
+        return sec
+    else :
+        raise TemplateSyntaxError('str_duration filter error : filters argument must be None, "::", ":", "h" or "s"')
+str_duration.is_safe = True