Browse Source

[search] added generic functions to the templates

Enrico Berti 12 năm trước cách đây
mục cha
commit
aa544e1468

+ 1 - 1
apps/search/src/search/conf.py

@@ -24,4 +24,4 @@ SOLR_URL = Config(
   key="solr_url",
   help=_("URL of the Solr Server."),
   private=False,
-  default="http://localhost:1978/solr/")
+  default="http://c1328.halxg.cloudera.com:8983/solr/")

+ 96 - 6
apps/search/src/search/templates/admin_core_template.mako

@@ -75,9 +75,12 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
                 <i class="icon-paste" style="margin-top:2px;"></i>
               </a>
             </div>
-
+          </div>
+        </div>
+        <div class="row-fluid">
+          <div class="span9">
             <div class="well available-fields" style="margin-top: 60px">
-              <h4>${_('Available Fields')}</h4>
+              <h5>${_('Available Fields')}</h5>
               <span data-bind="foreach: availableFields" class="field-button">
                   <a title="${ _('Click on this button to add the field') }"  style="margin-bottom:10px" class="btn btn-small" data-bind="click: $root.addFieldToVisual">
                     <i class="icon-plus"></i>
@@ -88,6 +91,20 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
                 </span>
             </div>
           </div>
+          <div class="span3">
+            <div class="well available-fields" style="margin-top: 60px">
+              <h5>${_('Available Functions')}</h5>
+              <ul class="functions-visual">
+                <li title="${ _('Formats a date in the DD-MM-YYYY format') }" rel="tooltip" data-placement="left">{{#date}} {{/date}}</li>
+                <li title="${ _('Formats a date in the HH:mm:ss format') }" rel="tooltip" data-placement="left">{{#time}} {{/time}}</li>
+                <li title="${ _('Formats a date in the DD-MM-YYYY HH:mm:ss format') }" rel="tooltip" data-placement="left">{{#datetime}} {{/datetime}}</li>
+                <li title="${ _('Formats a date in the full format') }" rel="tooltip" data-placement="left">{{#fulldate}} {{/fulldate}}</li>
+                <li title="${ _('Formats a date as a Unix timestamp') }" rel="tooltip" data-placement="left">{{#timestamp}} {{/timestamp}}</li>
+                <li title="${ _('Downloads the linked file') }" rel="tooltip" data-placement="left">{{#downloadfile}} {{/downloadfile}}</li>
+                <li title="${ _('Links to the file') }" rel="tooltip" data-placement="left">{{#viewfile}} {{/viewfile}}</li>
+              </ul>
+            </div>
+          </div>
         </div>
 
       </div>
@@ -95,8 +112,14 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
         <div class="row-fluid">
           <div class="span12">
             <textarea id="template-source"></textarea>
-            <div class="well available-fields" style="margin-top: 40px">
-              <h4>${_('Available Fields')}</h4>
+
+          </div>
+        </div>
+
+        <div class="row-fluid">
+          <div class="span9">
+            <div class="well available-fields" style="margin-top: 60px">
+              <h5>${_('Available Fields')}</h5>
               <span data-bind="foreach: availableFields" class="field-button">
                   <a title="${ _('Click on this button to add the field') }"  style="margin-bottom:10px" class="btn btn-small" data-bind="click: $root.addFieldToSource">
                     <i class="icon-plus"></i>
@@ -107,6 +130,20 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
                 </span>
             </div>
           </div>
+          <div class="span3">
+            <div class="well available-fields" style="margin-top: 60px">
+              <h5>${_('Available Functions')}</h5>
+              <ul class="functions-source">
+                <li title="${ _('Formats a date in the DD-MM-YYYY format') }" rel="tooltip" data-placement="left">{{#date}} {{/date}}</li>
+                <li title="${ _('Formats a date in the HH:mm:ss format') }" rel="tooltip" data-placement="left">{{#time}} {{/time}}</li>
+                <li title="${ _('Formats a date in the DD-MM-YYYY HH:mm:ss format') }" rel="tooltip" data-placement="left">{{#datetime}} {{/datetime}}</li>
+                <li title="${ _('Formats a date in the full format') }" rel="tooltip" data-placement="left">{{#fulldate}} {{/fulldate}}</li>
+                <li title="${ _('Formats a date as a Unix timestamp') }" rel="tooltip" data-placement="left">{{#timestamp}} {{/timestamp}}</li>
+                <li title="${ _('Downloads the linked file') }" rel="tooltip" data-placement="left">{{#downloadfile}} {{/downloadfile}}</li>
+                <li title="${ _('Links to the file') }" rel="tooltip" data-placement="left">{{#viewfile}} {{/viewfile}}</li>
+              </ul>
+            </div>
+          </div>
         </div>
 
       </div>
@@ -157,15 +194,17 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
 <script src="/static/ext/js/shortcut.js" type="text/javascript" charset="utf-8"></script>
 <script src="/static/ext/js/freshereditor.min.js" type="text/javascript" charset="utf-8"></script>
 <script src="/static/ext/js/codemirror-3.0.js"></script>
+<script src="/static/ext/js/moment.min.js" type="text/javascript" charset="utf-8"></script>
 <link rel="stylesheet" href="/static/ext/css/codemirror.css">
 <script src="/static/ext/js/codemirror-xml.js"></script>
 <script src="/static/ext/js/mustache.js"></script>
 
-
 <script type="text/javascript">
 
   $(document).ready(function () {
 
+    $("li[rel='tooltip']").tooltip();
+
     $("#content-editor").on("mouseup", function () {
       storeSelection();
     });
@@ -246,7 +285,17 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
       };
     };
 
-    ko.applyBindings(new ViewModel());
+    var viewModel = new ViewModel();
+    ko.applyBindings(viewModel);
+
+    $(".functions-visual li").on("click", function(){
+      $("#content-editor").focus();
+      pasteHtmlAtCaret($(this).text());
+    });
+
+    $(".functions-source li").on("click", function(){
+      codeMirror.replaceSelection($(this).text());
+    });
 
     var samples = ${ sample_data | n,unicode };
     var templateEditor = $("#template-source")[0];
@@ -274,6 +323,47 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
       if ($(e.target).attr("href") == "#preview") {
         $("#preview-container").empty();
         $(samples).each(function (cnt, item) {
+          item.viewfile = function () {
+            return function (val) {
+              return '<a href="/filebrowser/view/' + $.trim(Mustache.render(val, item)) + '">' + $.trim(Mustache.render(val, item)) + '</a>';
+            }
+          };
+          item.downloadfile = function () {
+            return function (val) {
+              return '<a href="/filebrowser/download/' + $.trim(Mustache.render(val, item)) + '?disposition=inline">' + $.trim(Mustache.render(val, item)) + '</a>';
+            }
+          };
+          item.date = function () {
+            return function (val) {
+              return genericFormatDate(val, item, "DD-MM-YYYY");
+            }
+          };
+          item.time = function () {
+            return function (val) {
+              return genericFormatDate(val, item, "HH:mm:ss");
+            }
+          };
+          item.datetime = function () {
+            return function (val) {
+              return genericFormatDate(val, item, "DD-MM-YYYY HH:mm:ss");
+            }
+          };
+          item.fulldate = function () {
+            return function (val) {
+              return genericFormatDate(val, item, null);
+            }
+          };
+          item.timestamp = function () {
+            return function (val) {
+              var d = moment(Mustache.render(val, item));
+              if (d.isValid()) {
+                return d.valueOf();
+              }
+              else {
+                return Mustache.render(val, item);
+              }
+            }
+          };
           $("<div>").addClass("preview-row").html(Mustache.render($("#content-editor").html(), item)).appendTo($("#preview-container"));
         });
       }

+ 56 - 5
apps/search/src/search/templates/index.mako

@@ -23,6 +23,9 @@ from django.utils.translation import ugettext as _
 
 ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
 
+<link rel="stylesheet" href="/search/static/css/search.css">
+<script src="/static/ext/js/moment.min.js" type="text/javascript" charset="utf-8"></script>
+
 <div class="search-bar">
   % if user.is_superuser:
     <div class="pull-right" style="margin-top: 4px">
@@ -156,13 +159,66 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
 
       <textarea id="mustacheTmpl" class="hide">${ hue_core.result.get_template(with_highlighting=True) | n,unicode }</textarea>
       <script>
+
       <%
         docs = response['response']['docs']
         for doc in response['response']['docs']:
           if doc['id'] in response.get('highlighting', []):
             doc.update(response['highlighting'][doc['id']])
         %>
+
+        function genericFormatDate(val, item, format){
+          var d = moment(Mustache.render(val, item));
+          if (d.isValid()) {
+            return d.format(format);
+          }
+          else {
+            return Mustache.render(val, item);
+          }
+        }
+
         $.each(${ json.dumps([result for result in docs]) | n,unicode }, function (index, item) {
+          item.viewfile = function () {
+            return function (val) {
+              return '<a href="/filebrowser/view/' + $.trim(Mustache.render(val, item)) + '">' + $.trim(Mustache.render(val, item)) + '</a>';
+            }
+          };
+          item.downloadfile = function () {
+            return function (val) {
+              return '<a href="/filebrowser/download/' + $.trim(Mustache.render(val, item)) + '?disposition=inline">' + $.trim(Mustache.render(val, item)) + '</a>';
+            }
+          };
+          item.date = function () {
+            return function (val) {
+              return genericFormatDate(val, item, "DD-MM-YYYY");
+            }
+          };
+          item.time = function () {
+            return function (val) {
+              return genericFormatDate(val, item, "HH:mm:ss");
+            }
+          };
+          item.datetime = function () {
+            return function (val) {
+              return genericFormatDate(val, item, "DD-MM-YYYY HH:mm:ss");
+            }
+          };
+          item.fulldate = function () {
+            return function (val) {
+              return genericFormatDate(val, item, null);
+            }
+          };
+          item.timestamp = function () {
+            return function (val) {
+              var d = moment(Mustache.render(val, item));
+              if (d.isValid()) {
+                return d.valueOf();
+              }
+              else {
+                return Mustache.render(val, item);
+              }
+            }
+          };
           $("<div>").addClass("result-row").html(
             Mustache.render($("#mustacheTmpl").text(), item)
           ).appendTo($("#result-container"));
@@ -207,11 +263,6 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
   ${ rr | n,unicode }
 </div>
 
-
-<link rel="stylesheet" href="/search/static/css/search.css">
-
-<script src="/static/ext/js/moment.min.js" type="text/javascript" charset="utf-8"></script>
-
 <script>
   $(document).ready(function () {
     $(".current-core").text($("select[name='cores'] option:selected").text());