Browse Source

[search] Save a new collection button

Romain Rigaux 11 years ago
parent
commit
f5bd061

+ 20 - 2
apps/search/src/search/models.py

@@ -227,6 +227,24 @@ class CollectionManager(models.Manager):
 
       return collection, True      
 
+  def create2(self, name, label):
+    facets = Facet.objects.create()
+    result = Result.objects.create()      
+    sorting = Sorting.objects.create()
+    cores = json.dumps({})
+
+    collection = Collection.objects.create(
+        name=name,
+        label=label,
+        cores=cores,
+        is_core_only=False,
+        facets=facets,
+        result=result,
+        sorting=sorting
+    )
+
+    return collection
+
 
 class Collection(models.Model):
   enabled = models.BooleanField(default=True)
@@ -270,7 +288,6 @@ class Collection(models.Model):
     id_field = [field['name'] for field in fields if field.get('isId')]
     if id_field:
       id_field = id_field[0]
-#    fields = [field.get('name') for field in self.fields_data(user)]
   
     TEMPLATE = {
       "extracode": "<style type=\"text/css\">\nem {\n  font-weight: bold;\n  background-color: yellow;\n}</style>", "highlighting": [""],
@@ -294,7 +311,8 @@ class Collection(models.Model):
     };  
     
     collection_properties = {
-      'id': self.id, 'name': self.name, 'template': TEMPLATE, 'facets': FACETS['fields'], 
+      'id': self.id, 'name': self.name, 'label': self.label,
+      'template': TEMPLATE, 'facets': FACETS['fields'], 
       'fields': fields, 'idField': id_field, 
     };      
     

+ 1 - 4
apps/search/src/search/templates/search2.mako

@@ -544,10 +544,7 @@ $(document).ready(function () {
   viewModel = new SearchViewModel(${ collection.get_c(user) | n,unicode }, ${ query | n,unicode });
   ko.applyBindings(viewModel);
 
-  
-
-  viewModel.isEditing(true);
-  viewModel.search();
+  viewModel.init();
 });
 
   function toggleGridFieldsSelection() {

+ 10 - 6
apps/search/src/search/views.py

@@ -71,7 +71,7 @@ def index(request):
 
 
 def new_search(request):
-  collection = Collection(name='twitter_demo', label='New Twitter Template', enabled=True)
+  collection = Collection(name='twitter_demo', label='New Twitter Template')
   query = {'q': '', 'fq': {}}
 
   return render('search2.mako', request, {
@@ -132,7 +132,10 @@ def save(request):
   layout = json.loads(request.POST.get('layout', '{}')) 
     
   if collection:
-    hue_collection = Collection.objects.get(id=collection['id'])
+    if collection['id']:
+      hue_collection = Collection.objects.get(id=collection['id'])
+    else:
+      hue_collection = Collection.objects.create2(name=collection['name'], label=collection['label'])
     hue_collection.update_properties({'collection': collection})
     hue_collection.update_properties({'layout': layout})
     # Todo update certain atttributes like, label, enabled...
@@ -445,14 +448,15 @@ def query_suggest(request, collection_id, query=""):
 
 
 # TODO security
-def index_fields_dynamic(request, collection_id):
-  hue_collection = Collection.objects.get(id=collection_id)
+def index_fields_dynamic(request, collection_id):  
   result = {'status': -1, 'message': 'Error'}
 
   solr_query = {}
-  solr_query['collection'] = hue_collection.name
-
+  
   try:
+    hue_collection = Collection.objects.get(id=collection_id)
+    solr_query['collection'] = hue_collection.name
+    
     dynamic_fields = SolrApi(SOLR_URL.get(), request.user).luke(hue_collection.name)
     result['message'] = ''
     result['dynamic_fields'] = [name for name, properties in dynamic_fields['fields'].iteritems() if 'dynamicBase' in properties]

+ 10 - 5
apps/search/static/js/search.ko.js

@@ -209,6 +209,7 @@ var Collection = function (vm, collection) {
 
   self.id = collection.id;
   self.name = collection.name;
+  self.label = collection.label;
   self.idField = collection.idField;
   self.template = ko.mapping.fromJS(collection.template);
   self.template.fieldsSelected.subscribe(function () {
@@ -286,8 +287,6 @@ var Collection = function (vm, collection) {
 
 	 vm.search();
   };
-
-  //self.addDynamicFields(); + update fields in case
 };
 
 
@@ -318,8 +317,8 @@ var SearchViewModel = function (collection_json, query_json) {
 
   self.previewColumns = ko.observable("");
   self.columns = ko.observable({});
-  loadLayout(self, collection_json.layout); // move to init + load dynamic fields?  
-  
+  loadLayout(self, collection_json.layout);
+
   self.isEditing = ko.observable(false);
   self.toggleEditing = function () {
     self.isEditing(!self.isEditing());
@@ -333,7 +332,13 @@ var SearchViewModel = function (collection_json, query_json) {
   self.draggableMap = ko.observable(new Widget(12, UUID(), "Map", "map-widget"));
   self.draggableLine = ko.observable(new Widget(12, UUID(), "Line Chart", "line-widget"));
   self.draggablePie = ko.observable(new Widget(12, UUID(), "Pie Chart", "pie-widget"));
-  
+
+  self.init = function () {
+	//self.collection.addDynamicFields();
+
+	self.isEditing(true);
+	self.search();	
+  }
 
   self.search = function () {
     self.isRetrievingResults(true);