renkan from notice/datasheet and correct renkan from facettes.
authorcavaliet
Tue, 26 Aug 2014 15:22:16 +0200
changeset 316 5cc83cd7d1d1
parent 315 5f8d4897c9f7
child 317 16c475baa0c0
renkan from notice/datasheet and correct renkan from facettes.
src/hdalab/views/profile.py
--- a/src/hdalab/views/profile.py	Fri Aug 22 17:17:00 2014 +0200
+++ b/src/hdalab/views/profile.py	Tue Aug 26 15:22:16 2014 +0200
@@ -24,7 +24,7 @@
 from django.views.decorators.csrf import csrf_exempt, csrf_protect
 from django.views.decorators.debug import sensitive_post_parameters
 from django.views.generic import TemplateView, View
-from hdabo.models import Tag, Datasheet, TaggedSheet
+from hdabo.models import Tag, Datasheet, TaggedSheet, Folder
 from hdalab.models.renkan import HdalabRenkan
 from hdalab.views.ajax import filter_generic
 from renkanmanager.models import Renkan
@@ -187,6 +187,82 @@
             response = json.dumps(content)
             return HttpResponse(response, content_type="application/json")
         
+        
+        # If a folder id is set
+        folder_id = request.GET.get("folder", "")
+        if folder_id!="":
+            #TODO : optimize to avoid tag request on each notice
+            folder = get_object_or_404(Folder.objects.select_related("datasheets", "datasheets__tags"), pk=folder_id)
+            notices = folder.datasheets.all()
+            n_tags = []
+            notice_tag_dict = {}
+            tag_uuid_dict = {}
+            for n in notices:
+                notice_tag_dict[n.pk] = {"uuid":unicode(uuid.uuid1()), "tags":[] }
+                for t in n.tags.all()[:5]:
+                    if t.pk not in tag_uuid_dict:
+                        tag_uuid_dict[t.pk] = unicode(uuid.uuid1())
+                    notice_tag_dict[n.pk]["tags"].append(tag_uuid_dict[t.pk])
+                    n_tags.append(t)
+            n_tags = [t.pk for t in n_tags]
+            all_tags = Tag.objects.filter( pk__in=n_tags ).select_related("dbpedia_fields", "category")
+            
+            # Prepare Node placer :
+            np = LineNodePlacer()
+            np.init({"datasheet": (1, len(notices)), "tags": (2, len(all_tags))})
+            
+            # Place notices
+            for n in notices:
+                content["nodes"].append({
+                  "id": notice_tag_dict[n.pk]["uuid"],
+                  "title": n.title,
+                  "description": n.description,
+                  "uri": n.url,
+                  "position": np.get_place("datasheet"),
+                  "image": static("hdalab/img/page_icon.png"),
+                  "size": 0,
+                  "project_id": project_id,
+                  "color": None,
+                  #"created_by": "roster_user-84fe909f-ba37-48e6-a25f-9d2f129a95b7"
+                })
+            
+            # Place tags
+            for t in all_tags:
+                img_url = t.dbpedia_fields.thumbnail if hasattr(t, 'dbpedia_fields') and t.dbpedia_fields and t.dbpedia_fields.thumbnail else None
+                if img_url is None and t.category is not None:
+                    img_url = cat_dict[t.category.label]
+                content["nodes"].append({
+                  "id": tag_uuid_dict[t.pk],
+                  "title": t.label + ((" (" + t.category.label + ")") if t.category else ""),
+                  "description": t.dbpedia_uri,
+                  "uri": t.dbpedia_uri,
+                  "position": np.get_place("tags"),
+                  "image": img_url,
+                  "size": 0,
+                  "project_id": project_id,
+                  "color": None,
+                  #"created_by": "roster_user-84fe909f-ba37-48e6-a25f-9d2f129a95b7"
+                })
+            
+            # Place edges
+            for n_pk in notice_tag_dict:
+                for tag_id in notice_tag_dict[n_pk]["tags"]:
+                    content["edges"].append({
+                        "id": unicode(uuid.uuid1()),
+                        "title": "",
+                        "description": "",
+                        "uri": "",
+                        "color": None,
+                        "from": notice_tag_dict[n_pk]["uuid"],
+                        "to": tag_id,
+                        "project_id": project_id,
+                        #"created_by": "de68xf75y6hs5rgjhgghxbm217xk"
+                    })
+            
+            response = json.dumps(content)
+            return HttpResponse(response, content_type="application/json")
+            
+        
         # Otherwise we build the datas
         # Get tags and countries
         labels = request.GET.get("label", "").split(",")
@@ -221,7 +297,7 @@
                             thumbnail_url = tt["thumbnail"]
                     related_tags.append({"label": t["label"], "thumbnail":thumbnail_url, "id":t["id"]})
                     all_labels.append(t["label"])
-                    related_tags_dict[c["id"]].append(t["id"])
+                related_tags_dict[c["id"]].append(t["id"])
         
         
         # If possible, we search a dbpedia_fields thumbnail or category thumbnail for related tags