# HG changeset patch # User cavaliet # Date 1365609676 -7200 # Node ID 923f9a8f2e10e424e10d7625b3abb2c67777ae48 # Parent 4ffa3f546ba02a1a63d82af672a6ced8e4c59b9e first step for keywords and images tabs in senseetive api response. diff -r 4ffa3f546ba0 -r 923f9a8f2e10 src/egonomy/templates/egonomy_create_fragment.html --- a/src/egonomy/templates/egonomy_create_fragment.html Tue Mar 19 18:01:51 2013 +0100 +++ b/src/egonomy/templates/egonomy_create_fragment.html Wed Apr 10 18:01:16 2013 +0200 @@ -33,22 +33,38 @@ data: data_obj, dataType: "json", success: function(data, status, request){ - var n = data.length; + var keywords = data["keywords"]; + var n = keywords.length; if(n>0){ - s = ''; + s = '
' + + '
' + + '
    '; for(var i=0;i'; - if(i<(n-1)) s += ', '; + s += '
  • ' + keywords[i] + '
  • '; } - s += '
    + {% trans 'Add all Senseetive keywords to yours' %}'; + s += '
+ {% trans 'Add all Senseetive keywords to yours' %}' + +'
' + +'
    '; + // Add pictures + var images = data["images"]; + var n = images.length; + for(var i=0;i ' + images[i].title + ''; + } + s += '
' + +'
'; $("#senseetive_holder").html(s); + // Init tabs + $("#tabs").tabs(); // Function to add one tag $("#senseetive_holder .addable_tag").click(function() { addTags([$(this).text()]); }); // Functions to add all tags from senseetive $("#senseetive_tags").click(function() { - addTags($("#add_senseetive_tags").text().split(', ')); + $(".addable_tag").each(function() { + addTags([this.textContent]); + }); }); } }, diff -r 4ffa3f546ba0 -r 923f9a8f2e10 src/egonomy/views.py --- a/src/egonomy/views.py Tue Mar 19 18:01:51 2013 +0100 +++ b/src/egonomy/views.py Wed Apr 10 18:01:16 2013 +0200 @@ -8,12 +8,12 @@ from django.shortcuts import get_object_or_404, render_to_response, redirect from django.template import RequestContext from django.utils.translation import ugettext as _ -from egonomy.models import ImageMetadata, Image, Fragment +from egonomy.models import ImageMetadata, Image, Fragment, ImageInfo from egonomy.search_indexes import QueryParser from egonomy.search_indexes.paginator import SearchPaginator from egonomy.search_indexes.query import ModelRelatedSearchQuerySet from haystack.query import RelatedSearchQuerySet -from sorl.thumbnail import default +from sorl.thumbnail import default, get_thumbnail from sorl.thumbnail.images import ImageFile from unicodedata import normalize import json @@ -309,17 +309,32 @@ # We get the content list contents = resp["contentlist"] keywords = {} + images_data = [] for c in contents: # For each content's keyword, we associate or add its score. score = c["contentimagelist"][0]["score"] - for kw in c["keywordlist"]: + kwl = c["keywordlist"] + for kw in kwl: kw = normalize('NFC',kw) if kw in keywords: keywords[kw] = keywords[kw] + score else: keywords[kw] = score + # We add the image and its metadatas in the images list. We take the id from the filename "id.jpg". + images_data.append({"id":c["contentimagelist"][0]["filename"][:-4], "url":"", "keywords":kwl, "title":c["title"]}) + # We generate the thumbnail and add its url for each picture + images = ImageInfo.objects.filter(id__in=[i["id"] for i in images_data]) + for i in images_data: + # We allow ourselves to loop in a loop because we know images_data has only 10 entries. + for img in images: + if img.id==i["id"]: + # Get the thumbnail + thumb = get_thumbnail(img.image_file, '100', format='PNG', crop='center', quality=99) + i["url"] = thumb.url + break + # We sort the keywords by descending score keywords = sorted(keywords, key=keywords.get, reverse=True) - return HttpResponse(json.dumps(keywords)) + return HttpResponse(json.dumps({"keywords":keywords, "images":images_data}), content_type="application/json")