search in notice json, and renkan search bin for notices.
authorcavaliet
Fri, 29 Aug 2014 16:19:31 +0200
changeset 323 67cff4e39ad8
parent 322 4c72bc520e19
child 324 573043a98b44
search in notice json, and renkan search bin for notices.
src/hdabo/urls.py
src/hdabo/views.py
src/hdalab/static/hdalab/css/additionnal_renkan.css
src/hdalab/static/hdalab/js/hdalab-renkan-bins.js
src/hdalab/templates/renkan_edit.html
--- a/src/hdabo/urls.py	Thu Aug 28 18:16:02 2014 +0200
+++ b/src/hdabo/urls.py	Fri Aug 29 16:19:31 2014 +0200
@@ -52,6 +52,6 @@
     url(r'^folders/$', login_required(Folders.as_view()), name='folders'),
     url(r'^folder/$', login_required(AddOrUpdateFolder.as_view()), name='add_or_update_folder'),
     url(r'^folder/(?P<folder_pk>[\w-]+)$', login_required(AddOrUpdateFolder.as_view()), name='add_or_update_folder'),
-    url(r'^searchajax/$', login_required(SearchDatasheet()), name='searchajax'),
+    url(r'^searchajax/$', SearchDatasheet(), name='searchajax'),
     url(r'^deletefolder/(?P<folder_pk>[\w-]+)$', login_required(DeleteFolder.as_view()), name='delete_folder'),
 )
--- a/src/hdabo/views.py	Thu Aug 28 18:16:02 2014 +0200
+++ b/src/hdabo/views.py	Fri Aug 29 16:19:31 2014 +0200
@@ -19,6 +19,7 @@
 import re
 from django.views.generic.base import TemplateView, View
 from haystack.views import SearchView
+from django.http.response import HttpResponse
 
 
 @login_required
@@ -630,6 +631,42 @@
     
     template = "partial/search_datasheet_for_folders.html"
 
+    def create_response(self):
+        """
+        Generates the actual HttpResponse to send back to the user.
+        """
+        limit = self.request.GET.get("limit", -1)
+        try:
+            limit = int(limit)
+        except:
+            pass
+        if limit>0:
+            self.results_per_page = limit
+        
+        (paginator, page) = self.build_page()
+
+        context = {
+            'query': self.query,
+            'form': self.form,
+            'page': page,
+            'paginator': paginator,
+            'suggestion': None,
+        }
+
+        if self.results and hasattr(self.results, 'query') and self.results.query.backend.include_spelling:
+            context['suggestion'] = self.form.get_suggestion()
+
+        context.update(self.extra_context())
+        
+        if self.request.GET.get("format", "")=="json":
+            output = { "results": [] }
+            for result in page.object_list:
+                o = result.object
+                output["results"].append({"title":o.title, "description":o.description, "url":o.url, "hda_id":o.hda_id})
+            return HttpResponse(json.dumps(output, indent=2), content_type="application/json")
+        
+        return render_to_response(self.template, context, context_instance=self.context_class(self.request))
+
 
 
 class DeleteFolder(View):
--- a/src/hdalab/static/hdalab/css/additionnal_renkan.css	Thu Aug 28 18:16:02 2014 +0200
+++ b/src/hdalab/static/hdalab/css/additionnal_renkan.css	Fri Aug 29 16:19:31 2014 +0200
@@ -16,4 +16,11 @@
     -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
     box-sizing: border-box;
+}
+.Rk-HdaNotices-Title-Icon {
+    background: url("../img/page_icon.png") no-repeat 0 -5px;
+    height: 20px;
+}
+.Rk-HdaNotices-Search-Icon {
+    background: url("../img/page_icon.png") no-repeat 0 -5px;
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/hdalab/static/hdalab/js/hdalab-renkan-bins.js	Fri Aug 29 16:19:31 2014 +0200
@@ -0,0 +1,212 @@
+/* Rkns.HdaNotices bin definition : search form and result rendering */
+
+Rkns.HdaNotices = {
+};
+
+Rkns.HdaNotices.Search = function(_renkan, _opts) {
+    this.renkan = _renkan;
+    this.lang = _opts.lang || "fr";
+    this.image = _opts.image;
+    this.url = _opts.url;
+};
+
+Rkns.HdaNotices.Search.prototype.getBgClass = function() {
+    return "Rk-HdaNotices-Search-Icon";
+};
+
+Rkns.HdaNotices.Search.prototype.getSearchTitle = function() {
+    return this.renkan.translate("Notices Hda");
+};
+
+Rkns.HdaNotices.Search.prototype.search = function(_q) {
+    this.renkan.tabs.push(
+        new Rkns.HdaNotices.Bin(this.renkan, {
+            lang: this.lang,
+            url: this.url,
+            image: this.image,
+            search: _q
+        })
+    );
+};
+
+Rkns.HdaNotices.Bin = Rkns.Utils.inherit(Rkns._BaseBin);
+
+Rkns.HdaNotices.Bin.prototype.resultTemplate = Rkns._.template(
+    '<li class="Rk-Wikipedia-Result Rk-Bin-Item" draggable="true" data-uri="<%-url%>" ' +
+    'data-title="<%-title%>" data-description="<%-full_description%>" data-image="<%-image%>">' +
+    '<img class="Rk-Wikipedia-Icon" src="<%-image%>"></div><h4 class="Rk-Wikipedia-Title"><a href="<%-url%>" target="_blank"><%=htitle%></a></h4>' +
+    '<p class="Rk-Wikipedia-Snippet"><%=hdescription%></p></li>'
+);
+
+Rkns.HdaNotices.Bin.prototype._init = function(_renkan, _opts) {
+    this.renkan = _renkan;
+    this.search = _opts.search;
+    this.lang = _opts.lang || "fr";
+    this.url = _opts.url;
+    this.image = _opts.image;
+    this.title_icon_$.addClass('Rk-HdaNotices-Title-Icon');
+    this.title_$.html(this.search).addClass("Rk-HdaNotices-Title");
+    this.refresh();
+};
+
+Rkns.HdaNotices.Bin.prototype.render = function(searchbase) {
+    var search = searchbase || Rkns.Utils.regexpFromTextOrArray();
+    var highlightrx = (search.isempty ? Rkns.Utils.regexpFromTextOrArray(this.search) : search);
+    function highlight(_text) {
+        return highlightrx.replace(_(_text).escape(), "<span class='searchmatch'>$1</span>");
+    }
+    var _html = "",
+        _this = this,
+        count = 0;
+    Rkns._(this.data.results).each(function(_result) {
+        console.log(_result);
+        var title = _result.title,
+            url = _result.url,
+            full_description = _result.description,
+            description = _result.description.substr(0, _result.description.indexOf(" ", 170)) + "...",
+            image = "http://www.histoiredesarts.culture.fr/images/pf/" + _result.hda_id + ".jpg";
+        if (!search.isempty && !search.test(title) && !search.test(description)) {
+            return;
+        }
+        count++;
+        _html += _this.resultTemplate({
+            url: url,
+            title: title,
+            htitle: highlight(title),
+            full_description: full_description,
+            description: description,
+            hdescription: highlight(description),
+            image: image
+        });
+    });
+    _this.main_$.html(_html);
+    if (!search.isempty && count) {
+        this.count_$.text(count).show();
+    } else {
+        this.count_$.hide();
+    }
+    if (!search.isempty && !count) {
+        this.$.hide();
+    } else {
+        this.$.show();
+    }
+    this.renkan.resizeBins();
+};
+
+Rkns.HdaNotices.Bin.prototype.refresh = function() {
+    var _this = this;
+    console.log("refresh");
+    Rkns.$.ajax({
+        url: this.url + "?q=" + encodeURIComponent(this.search) + "&format=json&limit=50",
+        success: function(_data) {
+            console.log("ntm");
+            _this.data = _data;
+            _this.render();
+        }
+    });
+};
+
+
+/* Rkns.HdaTags bin definition : search form and result rendering */
+/*
+Rkns.HdaTags = {
+};
+
+Rkns.HdaTags.Search = function(_renkan, _opts) {
+    this.renkan = _renkan;
+    this.lang = _opts.lang || "fr";
+    this.image = _opts.image;
+    console.log("2",this.image);
+};
+
+Rkns.HdaTags.Search.prototype.getBgClass = function() {
+    return "Rk-HdaTags-Search-Icon";
+};
+
+Rkns.HdaTags.Search.prototype.getSearchTitle = function() {
+    return this.renkan.translate("Tags Hda");
+};
+
+Rkns.HdaTags.Search.prototype.search = function(_q) {
+    this.renkan.tabs.push(
+        new Rkns.HdaTags.Bin(this.renkan, {
+            lang: this.lang,
+            image: this.image,
+            search: _q
+        })
+    );
+};
+
+Rkns.HdaTags.Bin = Rkns.Utils.inherit(Rkns._BaseBin);
+
+Rkns.HdaTags.Bin.prototype.resultTemplate = Rkns._.template(
+    '<li class="Rk-Wikipedia-Result Rk-Bin-Item" draggable="true" data-uri="<%-url%>" ' +
+    'data-title="Wikipedia: <%-title%>" data-description="<%-description%>" data-image="<%- Rkns.Utils.getFullURL( static_url + \'img/wikipedia.png\' ) %>">' +
+    '<img class="Rk-Wikipedia-Icon" src="<%-image%>"></div><h4 class="Rk-Wikipedia-Title">HDA TAGS : <a href="<%-url%>" target="_blank"><%=htitle%></a></h4>' +
+    '<p class="Rk-Wikipedia-Snippet"><%=hdescription%></p></li>'
+);
+
+Rkns.HdaTags.Bin.prototype._init = function(_renkan, _opts) {
+    this.renkan = _renkan;
+    this.search = _opts.search;
+    this.lang = _opts.lang || "fr";
+    this.image = _opts.image;
+    console.log("1",this.image,_opts);
+    this.title_icon_$.addClass('Rk-Wikipedia-Title-Icon');
+    this.title_$.html(this.search).addClass("Rk-Wikipedia-Title");
+    this.refresh();
+};
+
+Rkns.HdaTags.Bin.prototype.render = function(searchbase) {
+    var search = searchbase || Rkns.Utils.regexpFromTextOrArray();
+    var highlightrx = (search.isempty ? Rkns.Utils.regexpFromTextOrArray(this.search) : search);
+    function highlight(_text) {
+        return highlightrx.replace(_(_text).escape(), "<span class='searchmatch'>$1</span>");
+    }
+    var _html = "",
+        _this = this,
+        count = 0;
+    Rkns._(this.data.query.search).each(function(_result) {
+        var title = _result.title,
+            url = "http://" + _this.lang + ".wikipedia.org/wiki/" + encodeURI(title.replace(/ /g,"_")),
+            description = Rkns.$('<div>').html(_result.snippet).text();
+        if (!search.isempty && !search.test(title) && !search.test(description)) {
+            return;
+        }
+        count++;
+        _html += _this.resultTemplate({
+            url: url,
+            title: title,
+            htitle: highlight(title),
+            description: description,
+            hdescription: highlight(description),
+            static_url: _this.renkan.options.static_url,
+            image: _this.image
+        });
+    });
+    _this.main_$.html(_html);
+    if (!search.isempty && count) {
+        this.count_$.text(count).show();
+    } else {
+        this.count_$.hide();
+    }
+    if (!search.isempty && !count) {
+        this.$.hide();
+    } else {
+        this.$.show();
+    }
+    this.renkan.resizeBins();
+};
+
+Rkns.HdaTags.Bin.prototype.refresh = function() {
+    var _this = this;
+    Rkns.$.ajax({
+        url: "http://" + _this.lang + ".wikipedia.org/w/api.php?action=query&list=search&srsearch=" + encodeURIComponent(this.search) + "&format=json",
+        dataType: "jsonp",
+        success: function(_data) {
+            _this.data = _data;
+            _this.render();
+        }
+    });
+};
+*/
\ No newline at end of file
--- a/src/hdalab/templates/renkan_edit.html	Thu Aug 28 18:16:02 2014 +0200
+++ b/src/hdalab/templates/renkan_edit.html	Fri Aug 29 16:19:31 2014 +0200
@@ -25,7 +25,8 @@
         </script>
         <script src="{% static 'renkanmanager/lib/require.js' %}"></script>
         <script src="{% static 'renkanmanager/lib/renkan/renkan.js' %}"></script>
-        <script src="{% static 'hdalab/js/renkan-manual-save.js ' %}"></script>
+        <script src="{% static 'hdalab/js/renkan-manual-save.js' %}"></script>
+        <script src="{% static 'hdalab/js/hdalab-renkan-bins.js' %}"></script>
         <script type="text/javascript">
             var io_options = {}, dialog=false;
             function startRenkan(){
@@ -34,15 +35,14 @@
                     static_url : "{% static 'renkanmanager/lib/renkan/' %}",
                     search: [
                         {
-                            type: "Ldt"
+                            type: "HdaNotices",
+                            lang: "{{LANGUAGE_CODE}}",
+                            image: "http://digital-impulse.com/wp-content/plugins/smilies-themer/digicons/IronMan.png",
+                            url: "{% url 'searchajax' %}"
                         },
                         {
                             type: "Wikipedia",
-                            lang: "fr"
-                        },
-                        {
-                            type: "Wikipedia",
-                            lang: "ja"
+                            lang: "{{LANGUAGE_CODE}}"
                         }
                     ],
                     bins: [
@@ -55,21 +55,13 @@
 								    title: "Histoire des arts",
 								    description: "Portail Histoire des arts sur culture.fr",
 								    image: "http://www.culture.fr/extension/culture_fr/design/culture/images/favicon.ico"
-								},
-                                {
-                                    url: "http://www.google.com/",
-                                    title: "Google",
-                                    description: "Search engine",
-                                    image: "http://www.google.fr/images/srpr/logo4w.png"
-                                },
-                                "Polemic Tweet http://www.polemictweet.com",
-                                "Twitter http://www.twitter.com/"
+								}
                             ]
                         }
                     ],
                     /*property_files: [ "data/properties.json" ],
                        node_fill_color: false*/
-                    language: "fr"
+                    language: "{{LANGUAGE_CODE}}"
                 });
                 
                 io_options = { url: "{% url 'renkan_get_put' %}" + window.location.search, user_authenticated: {% if user.is_authenticated %}true{% else %}false{% endif %} };