Browse Source

[indexer] Add random ids when the input file does not contain any

Romain Rigaux 10 years ago
parent
commit
f5a9726e59

+ 1 - 1
desktop/libs/indexer/src/indexer/models.py

@@ -154,4 +154,4 @@ DECIMAL_FIELD_TYPES = (
 
 BOOLEAN_FIELD_TYPES = (
   'boolean',
-)
+)

+ 8 - 1
desktop/libs/indexer/src/indexer/static/indexer/js/lib.js

@@ -45,23 +45,30 @@ var Collection = function(name) {
     self.addField('', 'text_general');
   };
 
+  self.newIdField = function() {
+    self.fields.push(new Field(self, 'id', 'string', true, true, true));
+    self.uniqueKeyField('id');
+  };
+
   self.setData = function(data_json) {
     self.data(data_json);
   };
 };
 
-var Field = function(collection, name, type, required, indexed, stored) {
+var Field = function(collection, name, type, required, indexed, stored, mockId) {
   var self = this;
 
   indexed = (indexed == undefined) ? true : !!indexed;
   required = (required == undefined) ? true : !!required;
   stored = (stored == undefined) ? true : !!stored;
+  mockId = (mockId == undefined) ? false: !!mockId;
 
   self.name = ko.observable(name).extend({'errors': null});
   self.type = ko.observable(type).extend({'errors': null});
   self.required = ko.observable(required).extend({'errors': null});
   self.indexed = ko.observable(indexed).extend({'errors': null});
   self.stored = ko.observable(stored).extend({'errors': null});
+  self.mockId = ko.observable(mockId).extend({'errors': null});
 
   // Metadata
   self.saved = ko.observable(false);

+ 23 - 11
desktop/libs/indexer/src/indexer/templates/collections.mako

@@ -26,6 +26,7 @@ ${ commonheader(_('Search Indexes'), "indexer", user, "29px") | n,unicode }
 
 <link rel="stylesheet" href="${ static('desktop/ext/chosen/chosen.min.css') }">
 <link rel="stylesheet" href="${ static('indexer/css/admin.css') }">
+
 <style type="text/css">
 .hueBreadcrumb {
   padding: 12px 14px;
@@ -293,12 +294,12 @@ ${ commonheader(_('Search Indexes'), "indexer", user, "29px") | n,unicode }
       <tr>
         <th width="25%" class="nowrap">${_('Name')}</th>
         <th width="25%" class="nowrap">${_('Type')}</th>
+        <th width="5%" class="nowrap">${_('ID')}</th>
         <th width="0%" class="nowrap">${_('Required')}</th>
         <th width="0%" class="nowrap">${_('Indexed')}</th>
-        <th width="0%" class="nowrap">${_('Stored')}</th>
-        <th width="0%" class="nowrap">${_('Unique Key')}</th>
+        <th width="0%" class="nowrap">${_('Stored')}</th>        
         <th width="0%" class="nowrap">${_('Default Field')}</th>
-        <th width="50%"></th>
+        <th width="45%"></th>
       </tr>
     </thead>
     <tbody data-bind="foreach: collection.fields">
@@ -306,21 +307,27 @@ ${ commonheader(_('Search Indexes'), "indexer", user, "29px") | n,unicode }
         <td data-bind="editableText: name">
           <span class="pull-left fa fa-pencil"></span>
         </td>
-        <td><select data-bind="options: $parent.fieldTypes, value: type, chosen: {}" name="type"></select></td>
-        <td><p class="text-center"><input data-bind="checked: required" type="checkbox"></p></td>
+        <td>
+          <select data-bind="options: $parent.fieldTypes, value: type, chosen: {}" name="type"></select>
+        </td>
         <td>
           <p class="text-center">
-            <input data-bind="checked: indexed, visible: !uniqueKeyField()" type="checkbox">
+            <input data-bind="checked: uniqueKeyField, visible: !uniqueKeyField()" name="unique-key" type="checkbox" />
             <span class="fa" data-bind="css: {'fa-check': uniqueKeyField}">
           </p>
+        </td>        
+        <td>
+          <p class="text-center"><input data-bind="checked: required" type="checkbox"></p>
         </td>
-        <td><p class="text-center"><input data-bind="checked: stored" type="checkbox"></p></td>
         <td>
           <p class="text-center">
-            <input data-bind="checked: uniqueKeyField, visible: !uniqueKeyField()" name="unique-key" type="checkbox" />
+            <input data-bind="checked: indexed, visible: !uniqueKeyField()" type="checkbox">
             <span class="fa" data-bind="css: {'fa-check': uniqueKeyField}">
           </p>
         </td>
+        <td>
+          <p class="text-center"><input data-bind="checked: stored" type="checkbox"></p>
+        </td>
         <td>
           <p class="text-center">
             <input data-bind="checked: df, visible: !df()" name="unique-key" type="checkbox" />
@@ -336,7 +343,12 @@ ${ commonheader(_('Search Indexes'), "indexer", user, "29px") | n,unicode }
 
   <br style="clear: both" />
   <br />
-  <a data-bind="click: collection.newField" href="javascript:void(0)" class="btn btn-info"><i class="fa fa-plus"></i>&nbsp;${_("Add")}</a>
+  <a data-bind="click: collection.newField" href="javascript:void(0)" class="btn btn-info">
+    <i class="fa fa-plus"></i>&nbsp;${_("Add")}
+  </a>
+  <a data-bind="click: collection.newIdField" href="javascript:void(0)" class="btn btn-info" title="${ _('Generate a random ID') }">
+    <i class="fa fa-plus"></i>&nbsp;${_("Add ID")}
+  </a>
 </script>
 <!--/ Create wizard -->
 <!--/ Create by file -->
@@ -363,11 +375,11 @@ ${ commonheader(_('Search Indexes'), "indexer", user, "29px") | n,unicode }
             <tr>
               <th width="25%" class="nowrap">${_('Name')}</th>
               <th width="25%" class="nowrap">${_('Type')}</th>
-              <th width="0%" class="nowrap">${_('Unique key field')}</th>
+              <th width="5%" class="nowrap">${_('ID')}</th>
               <th width="0%" class="nowrap">${_('Required')}</th>
               <th width="0%" class="nowrap">${_('Indexed')}</th>
               <th width="0%" class="nowrap">${_('Stored')}</th>
-              <th width="50%"></th>
+              <th width="45%"></th>
             </tr>
           </thead>
           <tbody data-bind="foreach: ko.utils.arrayFilter(collection().fields(), function(field) { return field.saved() })">

+ 8 - 1
desktop/libs/indexer/src/indexer/utils.py

@@ -24,6 +24,8 @@ import re
 import shutil
 import StringIO
 import tempfile
+import uuid
+
 from dateutil.parser import parse
 
 from django.conf import settings
@@ -218,6 +220,7 @@ def field_values_from_separated_file(fh, delimiter, quote_character, fields=None
 
   content = fh.read()
   headers = None
+
   while content:
     last_newline = content.rfind('\n')
     if last_newline > -1:
@@ -230,7 +233,7 @@ def field_values_from_separated_file(fh, delimiter, quote_character, fields=None
           csvfile = StringIO.StringIO(content[:last_newline])
         else:
           csvfile = StringIO.StringIO('\n' + content[:last_newline])
-        content = content[last_newline+1:] + fh.read()
+        content = content[last_newline + 1:] + fh.read()
     else:
       if headers is None:
         csvfile = StringIO.StringIO(content)
@@ -286,6 +289,10 @@ def field_values_from_separated_file(fh, delimiter, quote_character, fields=None
           if key in row:
             row[key] = str(row[key]).lower() == "true"
 
+      # Add mock id random value
+      if 'id' not in row:
+        row['id'] = str(uuid.uuid4())
+
       yield row