add annotatin stats
authorymh <ymh.work@gmail.com>
Mon, 11 Jun 2012 01:17:44 +0200
changeset 49 8eea0d7de41c
parent 48 b888750b6ae5
child 50 a8cca505882d
child 51 b5f84b880a46
add annotatin stats
web/tralalere/__init__.py
web/tralalere/settings.py
web/tralalere/templates/home.html
web/tralalere/views.py
--- a/web/tralalere/__init__.py	Sat Jun 09 01:57:40 2012 +0200
+++ b/web/tralalere/__init__.py	Mon Jun 11 01:17:44 2012 +0200
@@ -15,3 +15,4 @@
 
 
 __version__ = get_version()
+
--- a/web/tralalere/settings.py	Sat Jun 09 01:57:40 2012 +0200
+++ b/web/tralalere/settings.py	Mon Jun 11 01:17:44 2012 +0200
@@ -113,6 +113,7 @@
     'ldt.ldt_utils.middleware.userprofile.LanguageMiddleware',
     'ldt.security.middleware.SecurityMiddleware',    
     'ldt.api.middleware.pistonput.PistonPutMiddleware',
+    #'debug_toolbar.middleware.DebugToolbarMiddleware'
 )
 
 ROOT_URLCONF = 'tralalere.urls'
@@ -152,6 +153,7 @@
     'south',
     'guardian',
     'sorl.thumbnail',
+    #'debug_toolbar',
     # Uncomment the next line to enable admin documentation:
     # 'django.contrib.admindocs',
 )
@@ -304,5 +306,7 @@
 # URL that handles the media served from MEDIA_ROOT.
 if not "MEDIA_URL" in locals():
     MEDIA_URL = BASE_URL + 'static/media/'
+    
+#INTERNAL_IPS = ('127.0.0.1',)
   
 
--- a/web/tralalere/templates/home.html	Sat Jun 09 01:57:40 2012 +0200
+++ b/web/tralalere/templates/home.html	Mon Jun 11 01:17:44 2012 +0200
@@ -17,59 +17,41 @@
                 <h4>
                     <a href="#">{{content.description}}</a>
                 </h4>
+                {% if content.tags_stats %}
+                {% if content.tags_stats.colors %}
                 <ul class="home-dataviz home-dataviz-colors">
-                    <li>
+                {% for tag in content.tags_stats.colors %}
+                    <li style="width: {{tag.width}}px">
+                    {% if tag.scale == 1 %}
                         <div class="tag-container-50">
-                            <div class="tag-shadow"></div>
-                            <div class="tag-color tag-green"></div>
-                        </div>
-                    </li>
-                    <li style="width: 40px">
-                        <div class="tag-container-50" style="-webkit-transform-origin: bottom left; -webkit-transform: scale(.8); -moz-transform-origin: bottom left; -moz-transform: scale(.8);">
+                    {% else %}
+                        <div class="tag-container-50" style="-webkit-transform-origin: bottom left; -webkit-transform: scale({{tag.scale}}); -moz-transform-origin: bottom left; -moz-transform: scale({{tag.scale}});">
+                    {% endif %}
                             <div class="tag-shadow"></div>
-                            <div class="tag-color tag-yellow"></div>
-                        </div>
-                    </li>
-                    <li style="width: 25px">
-                        <div class="tag-container-50" style="-webkit-transform-origin: bottom left; -webkit-transform: scale(.5); -moz-transform-origin: bottom left; -moz-transform: scale(.5);">
-                            <div class="tag-shadow"></div>
-                            <div class="tag-color tag-red"></div>
-                        </div>
-                    </li>
-                    <li style="width: 10px">
-                        <div class="tag-container-50" style="-webkit-transform-origin: bottom left; -webkit-transform: scale(.2); -moz-transform-origin: bottom left; -moz-transform: scale(.2);">
-                            <div class="tag-shadow"></div>
-                            <div class="tag-color tag-blue"></div>
+                            <div class="tag-color tag-{{tag.tag}}"></div>
                         </div>
                     </li>
+                {% endfor %}                      
                 </ul>
+                {% endif %}
+                {% if content.tags_stats.emoticons %}
                 <ul class="home-dataviz home-dataviz-emoticon">
-                    <li>
+                {% for tag in content.tags_stats.emoticons %}
+                    <li style="width: {{tag.width}}px">
+                    {% if tag.scale == 1 %}
                         <div class="tag-container-50">
+                    {% else %}
+                        <div class="tag-container-50" style="-webkit-transform-origin: bottom left; -webkit-transform: scale({{tag.scale}}); -moz-transform-origin: bottom left; -moz-transform: scale({{tag.scale}});">
+                    {% endif %}
                             <div class="tag-shadow"></div>
                             <div class="tag-color"></div>
-                            <div class="tag-emoticon tag-laughing"></div>
+                            <div class="tag-emoticon tag-{{tag.tag}}"></div>
                         </div>
                     </li>
-                    <li style="width: 40px">
-                        <div class="tag-container-50" style="-webkit-transform-origin: bottom left; -webkit-transform: scale(.8); -moz-transform-origin: bottom left; -moz-transform: scale(.8);">
-                            <div class="tag-color"></div>
-                            <div class="tag-emoticon tag-happy"></div>
-                        </div>
-                    </li>
-                    <li style="width: 25px">
-                        <div class="tag-container-50" style="-webkit-transform-origin: bottom left; -webkit-transform: scale(.5); -moz-transform-origin: bottom left; -moz-transform: scale(.5);">
-                            <div class="tag-color"></div>
-                            <div class="tag-emoticon tag-surprised"></div>
-                        </div>
-                    </li>
-                    <li style="width: 10px">
-                        <div class="tag-container-50" style="-webkit-transform-origin: bottom left; -webkit-transform: scale(.2); -moz-transform-origin: bottom left; -moz-transform: scale(.2);">
-                            <div class="tag-color"></div>
-                            <div class="tag-emoticon tag-unhappy"></div>
-                        </div>
-                    </li>
+                {% endfor %}
                 </ul>
+                {% endif %}
+                {% endif %}
             </div>
             {% endfor %}
 {% endblock %}
--- a/web/tralalere/views.py	Sat Jun 09 01:57:40 2012 +0200
+++ b/web/tralalere/views.py	Mon Jun 11 01:17:44 2012 +0200
@@ -1,13 +1,65 @@
 
 from django.shortcuts import render_to_response
 from django.template import RequestContext
-from ldt.ldt_utils.models import Content
+from ldt.ldt_utils.models import Content, Segment
+import operator
+import math
+
+COLORS = ['red', 'yellow', 'green', 'blue']
+EMOTICONS = ['happy','unhappy','laughing','surprised']
+SCALES = [0.2,0.5,0.8,1]
+
 
 def home(request):
     
     contents = Content.objects.filter(front_project__state = 2)
     
-    return render_to_response('home.html', {'contents': contents}, context_instance=RequestContext(request))
+    tags_stats = {}
+    
+    for segment in Segment.objects.filter(content__in = contents):
+        c_tags = tags_stats.setdefault(segment.iri_id,{})
+        for t in segment.tags.split(";"):
+            t = t.strip().lower()
+            if t:
+                c_tags[t] = c_tags.setdefault(t,0) + 1
+            if t in COLORS:
+                c_tags['colors'] = c_tags.setdefault('colors',0) + 1
+            if t in EMOTICONS:
+                c_tags['emoticons'] = c_tags.setdefault('emoticons',0) + 1
+    tags = {}            
+    for contentid, c_tags in tags_stats.iteritems():
+        emoticons_stats = []
+        for t in EMOTICONS:
+            if t in c_tags:
+                score = float(c_tags[t])/float(c_tags['emoticons'])
+                cat = int(len(EMOTICONS)*score)
+                scale = SCALES[cat]
+                width = int(50*scale)
+                emoticons_stats.append({'tag':t, 'score': score, 'cat':cat, 'scale': scale, 'width':width})
+        emoticons_stats.sort(key=operator.itemgetter('score','tag'), reverse=True)
+
+        colors_stats = []
+        for t in COLORS:
+            if t in c_tags:
+                score = float(c_tags[t])/float(c_tags['colors'])
+                cat = int(len(COLORS)*score)
+                scale = SCALES[cat]
+                width = int(50*scale)
+                colors_stats.append({'tag':t, 'score': score, 'cat':cat, 'scale': scale, 'width':width})        
+        colors_stats.sort(key=operator.itemgetter('score','tag'), reverse=True)
+        
+        tags[contentid] = {'emoticons': emoticons_stats, 'colors': colors_stats}
+        
+    contents_list = []
+    for content in contents:
+        if content.iri_id in tags:
+            tags_stats = tags[content.iri_id]
+        else:
+            tags_stats = None
+        content.tags_stats = tags_stats
+        contents_list.append(content)
+    return render_to_response('home.html', {'contents': contents_list}, context_instance=RequestContext(request))
+
 
 def player(request, contentid):