1 from __future__ import unicode_literals
3 from django.http import HttpResponse
4 from django.template import RequestContext, Template, Context
5 from django.shortcuts import render_to_response
6 from django.core.exceptions import ObjectDoesNotExist
9 from .utils import report_error, redirect_to_url
10 from .plugin import bcm_plugin_processor
12 #from htsworkflow.util.jsonutil import encode_json
16 except ImportError as e:
17 import simplejson as json
21 from htsworkflow.frontend.bcmagic import forms
26 Display a barcode magic input box
28 form = forms.BarcodeMagicForm()
30 return render_to_response('bcmagic/magic.html', {'bcmagic': form},
31 context_instance=RequestContext(request))
34 def __plugin_search(text):
36 Runs registered plugins to search for results
40 for label, search_func in plugin._SEARCH_FUNCTIONS.items():
41 result = search_func(text)
42 if result is not None:
47 msg = 'No hits found for: %s' % (text)
48 return report_error(msg)
50 return redirect_to_url(hits[0][1])
52 msg = "%d hits found for (%s); multi-hit not implemented yet." % \
54 return report_error(msg)
57 #return json.dumps(hits)
60 def __magic_process(text):
62 Based on scanned text, check to see if there is map object to use
63 for a useful redirect.
66 split_text = text.split('|')
68 # There should always be at least one | in a valid scan.
69 if len(split_text) <= 1:
70 #return report_error('Invalid text: %s' % (text))
71 return __plugin_search(text)
73 # Keyword is the first element in the list
74 keyword = split_text[0]
76 # Attempt to find a KeywordMap based on keyword
78 keymap = models.KeywordMap.objects.get(keyword=keyword)
79 except ObjectDoesNotExist(e):
80 return report_error('Keyword (%s) is not defined' % (keyword))
82 # Remove keyword and only scan the content
83 content = '|'.join(split_text[1:])
85 #FIXME: would be faster to cache compiled regex
86 search = re.compile(keymap.regex)
88 mo = search.search(content)
90 # if the search was invalid
93 '(%s) failed to match (%s)' % (keymap.regex, content)
96 t = Template(keymap.url_template)
97 c = Context(mo.groupdict())
99 return redirect_to_url(str(t.render(c)))
108 #Retrieve posted text from processing
109 if 'text' in request.POST:
110 text = request.POST['text']
114 #Retrieve bmc_mode for processing
115 if 'bcm_mode' in request.POST:
116 bcm_mode = request.POST['bcm_mode']
120 ################################
122 ################################
124 # Did not receive text error
125 if text is None or text.strip() == '':
127 d['status'] = 'Did not recieve text'
129 return HttpResponse(json.dumps(d), 'text/plain')
131 # Did not receive bcm_mode error
132 if bcm_mode is None or bcm_mode.strip() == '':
134 d['status'] = 'Missing bcm_mode information'
136 ################################
137 # Figure out which mode to use
138 ################################
139 keyword = text.split('|')[0]
141 # Handle URL mode by default
143 d['mode'] = 'redirect'
144 d['url'] = text.split('|')[1]
146 # Pass off processing to plugins
147 elif bcm_mode != 'default':
148 d = bcm_plugin_processor(keyword, text, bcm_mode)
152 d = __magic_process(text)
154 return HttpResponse(json.dumps(d), 'text/plain')
157 def json_test(request):
160 if 'text' in request.POST:
161 text = request.POST['text']
165 #return HttpResponse(json.dumps(request.POST.items()), 'text/plain')
166 if text is None or text.strip() == '':
168 d['status'] = 'Did not recieve text'
169 return HttpResponse(json.dumps(d), 'text/plain')
171 if text.split('|')[0] == 'url':
172 d['mode'] = 'redirect'
173 d['url'] = text.split('|')[1]
175 d['msg'] = 'Recieved text: %s' % (text)
178 return HttpResponse(json.dumps(d), 'text/plain')