debug and first step of senseetive request
authorcavaliet
Tue, 12 Mar 2013 18:23:44 +0100
changeset 89 da5504ff262e
parent 88 fc3203831322
child 90 9ee9e769ea11
debug and first step of senseetive request
src/egonomy/config.py.tmpl
src/egonomy/models.py
src/egonomy/views.py
--- a/src/egonomy/config.py.tmpl	Tue Mar 12 13:13:29 2013 +0100
+++ b/src/egonomy/config.py.tmpl	Tue Mar 12 18:23:44 2013 +0100
@@ -139,3 +139,8 @@
 
 BATIK_RASTERIZER_PATH = "path/to/batik-rasterizer.jar"
 BATIK_RASTERIZER_TEMP_FOLDER = "path/to/tmp_folder"
+
+SENSEETIVE_API_URL = "http://url/to/senseetive/api"
+SENSEETIVE_API_USER = "user"
+SENSEETIVE_API_PASSWORD = "pwd"
+SENSEETIVE_API_TENANT = "projectX"
--- a/src/egonomy/models.py	Tue Mar 12 13:13:29 2013 +0100
+++ b/src/egonomy/models.py	Tue Mar 12 18:23:44 2013 +0100
@@ -85,7 +85,7 @@
     
     
     def get_viewbox_info(self):
-        if not self.coordinates:
+        if not self.coordinates or self.coordinates=="MZ" or self.coordinates=="":
             return None,None,None,None
         # Now we split the coordinates to get the path points's max and min x and y
         # A typical path is M0.1995 0.1574L0.3718 0.0131L0.7731 0.0597L0.5126 0.2915Z
@@ -113,7 +113,7 @@
     # This property returns a ratio between width and height
     @property
     def ratio(self):
-        if not self.coordinates:
+        if not self.coordinates or self.coordinates=="MZ" or self.coordinates=="":
             return 0
         _, _, vb_w, vb_h = self.get_viewbox_info()
         return vb_w/vb_h
@@ -121,7 +121,7 @@
     # This property returns a viewbox used in the swg xml and enbling to see the fragment only
     @property
     def viewbox(self):
-        if not self.coordinates:
+        if not self.coordinates or self.coordinates=="MZ" or self.coordinates=="":
             return "0 0 1 1"
         vb_x, vb_y, vb_w, vb_h = self.get_viewbox_info()
         vb = str(vb_x) + " " + str(vb_y) + " " + str(vb_w) + " " + str(vb_h)
@@ -130,7 +130,7 @@
     # This property returns a square viewbox used in the swg xml and enbling to see the fragment only
     @property
     def viewbox_square(self):
-        if not self.coordinates:
+        if not self.coordinates or self.coordinates=="MZ" or self.coordinates=="":
             return "0 0 1 1"
         vb_x, vb_y, vb_w, vb_h = self.get_viewbox_info()
         img_info = self.image.info
--- a/src/egonomy/views.py	Tue Mar 12 13:13:29 2013 +0100
+++ b/src/egonomy/views.py	Tue Mar 12 18:23:44 2013 +0100
@@ -1,3 +1,4 @@
+from base64 import b64encode
 from django.conf import settings
 from django.contrib.auth.decorators import login_required
 from django.contrib.auth.models import User
@@ -16,6 +17,7 @@
 import json
 import os
 import uuid
+import requests
 import subprocess
 
 import logging
@@ -251,25 +253,73 @@
     p = subprocess.Popen(args, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
     out, err = p.communicate()
     if p.returncode!=0:
-        HttpResponse("Batik error : " + str(err))
-    # The picture png is now created
+        return HttpResponse("Batik error : " + str(err))
+    # The picture png is now created, we log into the senseetive api
+    params = {
+              "xaction":"sessions.create",
+              "login":settings.SENSEETIVE_API_USER,
+              "pwd":settings.SENSEETIVE_API_PASSWORD,
+              "xtenant":settings.SENSEETIVE_API_TENANT,
+              "role":"user"
+    }
+    req = requests.get(url=settings.SENSEETIVE_API_URL, params=params, verify=False)
+    resp = req.json()
+    if "xsessionid" in resp:
+        session_id = resp["xsessionid"]
+    else:
+        return HttpResponseForbidden("Failed to connect to Senseetive API (1)")
     
-    
+#    #We create the query
+#    params = {
+#              "xaction":"queries.create",
+#              "xsessionid":session_id,
+#              "query":"SELECT img._id FROM contentimages as img, requests as req WHERE img.desc1 cmp1 (file) req.image0",
+#    }
+#    req = requests.get(url=settings.SENSEETIVE_API_URL, params=params, verify=False)
+#    return HttpResponse("session_id = " + session_id + ", req 0 .text = " + str(req.text))
     
-    # This is a TEMPORARY algorithm. In the future, this function build a picture from the fragment and will request the senseetive api
-    frg_list = Fragment.objects.filter(image=img)
-    tag_list = []
-    for frg in frg_list:
-        tags = frg.tags.split(",")
-        for t in tags:
-            t = t.strip()
-            if t != "":
-                #logger.debug(t)
-                tag_list.append(t)
-    from time import sleep
-    sleep(2)
+    # We create the request and load the picture (ex queryid:50d0574a03000043102a5177, requestid:513f474c39b37bfa5b299a31)
+    params = {
+              'image0':b64encode(open(os.path.join(settings.BATIK_RASTERIZER_TEMP_FOLDER, uid + '.png'), 'rb').read()),
+              "xaction":"requests.create",
+              "xsessionid":session_id,
+              "queryid":"",
+              "contentimageid":"",
+              "requestid":"513f474c39b37bfa5b299a31",
+              "requestfieldname":"image0"
+    }
+    req = requests.post(url=settings.SENSEETIVE_API_URL, data=params, verify=False)
+    resp = req.json()
+    if "id" in resp:
+        request_id = resp["id"]
+    else:
+        return HttpResponseForbidden("Failed to connect to Senseetive API (2)")
     
-    return HttpResponse(json.dumps(sorted(set(tag_list))))
+#    # We read the request
+#    params = {
+#              "xaction":"requests.read",
+#              "xsessionid":session_id,
+#              "id":request_id
+#    }
+#    req = requests.post(url=settings.SENSEETIVE_API_URL, data=params, verify=False)
+#    return HttpResponse("session_id = " + session_id + ", req 2 .text = " + str(req.text))
 
+    # We read the contents
+    params = {
+              "xaction":"contents.read",
+              "xsessionid":session_id,
+              "requestid":request_id,
+              #"requestfieldname":"image0",
+    }
+    req = requests.get(url=settings.SENSEETIVE_API_URL, params=params, verify=False)
+    resp = req.json()
+    if "contentlist" not in resp:
+        return HttpResponseForbidden("Failed to connect to Senseetive API (3)")
+    contents = resp["contentlist"]
+    keywords = []
+    for c in contents:
+        keywords += c["keywordlist"]
+    #return HttpResponse("session_id = " + session_id + ", req 3 .text = " + str(len(contents)) + " found")
+    #return HttpResponse("session_id = " + session_id + ", req 3 .text = " + req.text)
+    return HttpResponse(json.dumps(sorted(set(keywords))))
 
-