Prototype of bcmagic search plugin.
authorBrandon King <kingb@caltech.edu>
Mon, 13 Jul 2009 23:37:10 +0000 (23:37 +0000)
committerBrandon King <kingb@caltech.edu>
Mon, 13 Jul 2009 23:37:10 +0000 (23:37 +0000)
 * Works for inventory item object.

htsworkflow/frontend/bcmagic/plugin.py
htsworkflow/frontend/bcmagic/views.py
htsworkflow/frontend/inventory/bcmagic.py [new file with mode: 0644]
htsworkflow/frontend/inventory/models.py
htsworkflow/frontend/inventory/views.py

index 6653fd9261331b6d43cec48e8810ee4feabd3ebb..d701cf383d3bd91b12d5ade1db3e8beb55629015 100644 (file)
@@ -2,6 +2,7 @@
 
 #BCM_PLUGINS = {'cmd_move_sample': bcm_cmds.cmd_move_sample}
 
+_SEARCH_FUNCTIONS = {}
 
 def bcm_plugin_processor(keyword, text, bcm_mode):
     """
@@ -15,4 +16,19 @@ def bcm_plugin_processor(keyword, text, bcm_mode):
         d['status'] = 'bcm_mode plugin called "%s" was not found' % (bcm_mode)
         return d
     
-    return BCM_PLUGINS[bcm_mode](keyword, text, bcm_mode)
\ No newline at end of file
+    return BCM_PLUGINS[bcm_mode](keyword, text, bcm_mode)
+    
+
+def register_search_plugin(label, search_function):
+    """
+    Registers a group label and search_function
+    
+    search_function(search_string) --> (text_display, obj_url)
+    """
+    
+    if label in _SEARCH_FUNCTIONS:
+        msg = "search function for label (%s) already registered." % (label)
+        raise ValueError, msg
+    
+    _SEARCH_FUNCTIONS[label] = search_function
+    
\ No newline at end of file
index 9d145b6092b82489798271d70e515b05dd334e7c..f9fb7933cdecbe70230cc57f04d50c2ec7569e4f 100644 (file)
@@ -6,6 +6,7 @@ from django.core.exceptions import ObjectDoesNotExist
 from htsworkflow.frontend.bcmagic import models
 from htsworkflow.frontend.bcmagic.utils import report_error, redirect_to_url
 from htsworkflow.frontend.bcmagic.plugin import bcm_plugin_processor
+from htsworkflow.frontend.bcmagic import plugin
 #from htsworkflow.util.jsonutil import encode_json
 
 try:
@@ -27,6 +28,31 @@ def index(request):
                               context_instance=RequestContext(request))
 
 
+def __plugin_search(text):
+    """
+    Runs registered plugins to search for results
+    """
+    
+    hits = []
+    for label, search_func in plugin._SEARCH_FUNCTIONS.items():
+        result = search_func(text)
+        if result is not None:
+            hits.extend(result)
+            
+    n = len(hits)
+    if n == 0:
+        msg = 'No hits found for: %s' % (text)
+        return report_error(msg)
+    elif n == 1:
+        return redirect_to_url(hits[0][1])
+    else:
+        msg = "%d hits found for (%s); multi-hit not implemented yet." % (n, text)
+        return report_error(msg)
+    
+    
+    #return json.dumps(hits)
+    
+
 def __magic_process(text):
     """
     Based on scanned text, check to see if there is map object to use
@@ -37,7 +63,8 @@ def __magic_process(text):
     
     # There should always be at least one | in a valid scan.
     if len(split_text) <= 1:
-        return report_error('Invalid text: %s' % (text))
+        #return report_error('Invalid text: %s' % (text))
+        return __plugin_search(text)
     
     # Keyword is the first element in the list
     keyword = split_text[0]
diff --git a/htsworkflow/frontend/inventory/bcmagic.py b/htsworkflow/frontend/inventory/bcmagic.py
new file mode 100644 (file)
index 0000000..9d275c1
--- /dev/null
@@ -0,0 +1,26 @@
+from htsworkflow.frontend.inventory.models import Item
+
+from django.core.exceptions import ObjectDoesNotExist
+
+def item_search(search):
+    """
+    Searches 
+    """
+    hits = []
+    try:
+        item = Item.objects.get(uuid=search)
+    except ObjectDoesNotExist:
+        item = None
+    
+    if item is not None:
+        hits.append((str(item), item.get_absolute_url()))
+    
+    try:
+        item = Item.objects.get(barcode_id=search)
+    except ObjectDoesNotExist:
+        item = None
+    
+    if item is not None:
+        hits.append((str(item), item.get_absolute_url()))
+
+    return hits
index 456db7ce6dbeb7b1de667e6113e387746b9e9c9b..444d3c5f6c6009f5793e5b2131d6dd1eb0d63cd8 100644 (file)
@@ -123,6 +123,9 @@ class Item(models.Model):
         else:
             return u"invb|%s" % (self.barcode_id)
             
+    def get_absolute_url(self):
+        return '/inventory/%s/' % (self.uuid)
+            
 pre_save.connect(_assign_uuid, sender=Item)
 
 
index 65040a1e10dbabd35dc565b78066c0dac36465d2..027c4aaa4cbdade50a7e9b465e498757ee661d27 100644 (file)
@@ -1,4 +1,6 @@
 from htsworkflow.frontend.inventory.models import Item, LongTermStorage
+from htsworkflow.frontend.inventory.bcmagic import item_search
+from htsworkflow.frontend.bcmagic.plugin import register_search_plugin
 from htsworkflow.frontend.experiments.models import FlowCell
 from htsworkflow.frontend.bcmagic.forms import BarcodeMagicForm
 from htsworkflow.frontend.bcprinter.util import print_zpl_socket
@@ -12,6 +14,8 @@ from django.template import RequestContext
 from django.template.loader import get_template
 from django.contrib.auth.decorators import login_required
 
+register_search_plugin('Inventory Item', item_search)
+
 try:
     import json
 except ImportError, e: