1 from django.http import HttpResponse
2 from django.template import RequestContext, Template, Context
3 from django.shortcuts import render_to_response
4 from django.core.exceptions import ObjectDoesNotExist
6 from htsworkflow.frontend.bcmagic import models
7 from htsworkflow.frontend.bcmagic.utils import report_error, redirect_to_url
8 from htsworkflow.frontend.bcmagic.plugin import bcm_plugin_processor
9 from htsworkflow.frontend.bcmagic import plugin
10 #from htsworkflow.util.jsonutil import encode_json
14 except ImportError, e:
15 import simplejson as json
19 from htsworkflow.frontend.bcmagic import forms
23 Display a barcode magic input box
25 form = forms.BarcodeMagicForm()
27 return render_to_response('bcmagic/magic.html', {'bcmagic': form},
28 context_instance=RequestContext(request))
31 def __plugin_search(text):
33 Runs registered plugins to search for results
37 for label, search_func in plugin._SEARCH_FUNCTIONS.items():
38 result = search_func(text)
39 if result is not None:
44 msg = 'No hits found for: %s' % (text)
45 return report_error(msg)
47 return redirect_to_url(hits[0][1])
49 msg = "%d hits found for (%s); multi-hit not implemented yet." % (n, text)
50 return report_error(msg)
53 #return json.dumps(hits)
56 def __magic_process(text):
58 Based on scanned text, check to see if there is map object to use
59 for a useful redirect.
62 split_text = text.split('|')
64 # There should always be at least one | in a valid scan.
65 if len(split_text) <= 1:
66 #return report_error('Invalid text: %s' % (text))
67 return __plugin_search(text)
69 # Keyword is the first element in the list
70 keyword = split_text[0]
72 # Attempt to find a KeywordMap based on keyword
74 keymap = models.KeywordMap.objects.get(keyword=keyword)
75 except ObjectDoesNotExist, e:
76 return report_error('Keyword (%s) is not defined' % (keyword))
78 # Remove keyword and only scan the content
79 content = '|'.join(split_text[1:])
81 #FIXME: would be faster to cache compiled regex
82 search = re.compile(keymap.regex)
84 mo = search.search(content)
86 # if the search was invalid
88 return report_error('(%s) failed to match (%s)' % (keymap.regex, content))
90 t = Template(keymap.url_template)
91 c = Context(mo.groupdict())
93 return redirect_to_url(str(t.render(c)))
105 #Retrieve posted text from processing
106 if 'text' in request.POST:
107 text = request.POST['text']
111 #Retrieve bmc_mode for processing
112 if 'bcm_mode' in request.POST:
113 bcm_mode = request.POST['bcm_mode']
117 ################################
119 ################################
121 # Did not receive text error
122 if text is None or text.strip() == '':
124 d['status'] = 'Did not recieve text'
126 return HttpResponse(json.dumps(d), 'text/plain')
128 # Did not receive bcm_mode error
129 if bcm_mode is None or bcm_mode.strip() == '':
131 d['status'] = 'Missing bcm_mode information'
134 ################################
135 # Figure out which mode to use
136 ################################
137 keyword = text.split('|')[0]
139 # Handle URL mode by default
141 d['mode'] = 'redirect'
142 d['url'] = text.split('|')[1]
144 # Pass off processing to plugins
145 elif bcm_mode != 'default':
146 d = bcm_plugin_processor(keyword, text, bcm_mode)
150 d = __magic_process(text)
152 return HttpResponse(json.dumps(d), 'text/plain')
156 def json_test(request):
159 if 'text' in request.POST:
160 text = request.POST['text']
164 #return HttpResponse(json.dumps(request.POST.items()), 'text/plain')
165 if text is None or text.strip() == '':
167 d['status'] = 'Did not recieve text'
168 return HttpResponse(json.dumps(d), 'text/plain')
170 if text.split('|')[0] == 'url':
171 d['mode'] = 'redirect'
172 d['url'] = text.split('|')[1]
174 d['msg'] = 'Recieved text: %s' % (text)
177 return HttpResponse(json.dumps(d), 'text/plain')