Adding barcode magic commands:
[htsworkflow.git] / samplebc / bcmagic / views.py
index 21d45a1ec7d31825491e2908f3a1666ef838b8e9..0b0a2bc19d2324cd99d37fb2e70d41bd0adcd775 100644 (file)
@@ -4,6 +4,8 @@ from django.shortcuts import render_to_response
 from django.core.exceptions import ObjectDoesNotExist
 
 from samplebc.bcmagic import models
+from samplebc.bcmagic.utils import report_error, redirect_to_url
+from samplebc.bcmagic.plugin import bcm_plugin_processor
 
 import json
 import re
@@ -30,8 +32,7 @@ def __magic_process(text):
     
     # There should always be at least one | in a valid scan.
     if len(split_text) <= 1:
-        return {'mode': 'Error',
-                'status': 'Invalid text: %s' % (text)}
+        return report_error('Invalid text: %s' % (text))
     
     # Keyword is the first element in the list
     keyword = split_text[0]
@@ -40,8 +41,7 @@ def __magic_process(text):
     try:
         keymap = models.KeywordMap.objects.get(keyword=keyword)
     except ObjectDoesNotExist, e:
-        return {'mode': 'Error',
-                'status': 'Keyword (%s) is not defined' % (keyword)}
+        return report_error('Keyword (%s) is not defined' % (keyword))
     
     # Remove keyword and only scan the content
     content = '|'.join(split_text[1:])
@@ -53,14 +53,12 @@ def __magic_process(text):
     
     # if the search was invalid
     if not mo:
-        return {'mode': 'Error',
-                'status': '(%s) failed to match (%s)' % (keymap.regex, content)}
+        return report_error('(%s) failed to match (%s)' % (keymap.regex, content))
     
     t = Template(keymap.url_template)
     c = Context(mo.groupdict())
     
-    return {'mode': 'redirect',
-            'url': str(t.render(c)) }
+    return redirect_to_url(str(t.render(c)))
     
     
     
@@ -72,20 +70,49 @@ def magic(request):
     """
     d = {}
     
+    #Retrieve posted text from processing
     if 'text' in request.POST:
         text = request.POST['text']
     else:
         text = None
+        
+    #Retrieve bmc_mode for processing
+    if 'bcm_mode' in request.POST:
+        bcm_mode = request.POST['bcm_mode']
+    else:
+        bcm_mode = None
+        
+    ################################
+    # Handle some errors
+    ################################
     
-    #return HttpResponse(json.write(request.POST.items()), 'text/plain')
+    # Did not receive text error
     if text is None or text.strip() == '':
         d['mode'] = 'Error'
         d['status'] = 'Did not recieve text'
         return HttpResponse(json.write(d), 'text/plain')
     
-    if text.split('|')[0] == 'url':
+    # Did not receive bcm_mode error
+    if bcm_mode is None or bcm_mode.strip() == '':
+        d['mode'] = 'Error'
+        d['status'] = 'Missing bcm_mode information'
+    
+    
+    ################################
+    # Figure out which mode to use
+    ################################
+    keyword = text.split('|')[0]
+    
+    # Handle URL mode by default
+    if keyword == 'url':
         d['mode'] = 'redirect'
         d['url'] = text.split('|')[1]
+        
+    # Pass off processing to plugins
+    elif bcm_mode != 'default':
+        d = bcm_plugin_processor(keyword, text, bcm_mode)
+    
+    # Try keyword mapper
     else:
         d = __magic_process(text)