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
7 from .utils import report_error, redirect_to_url
8 from .plugin import bcm_plugin_processor
10 #from htsworkflow.util.jsonutil import encode_json
14 except ImportError, e:
15 import simplejson as json
19 from htsworkflow.frontend.bcmagic import forms
24 Display a barcode magic input box
26 form = forms.BarcodeMagicForm()
28 return render_to_response('bcmagic/magic.html', {'bcmagic': form},
29 context_instance=RequestContext(request))
32 def __plugin_search(text):
34 Runs registered plugins to search for results
38 for label, search_func in plugin._SEARCH_FUNCTIONS.items():
39 result = search_func(text)
40 if result is not None:
45 msg = 'No hits found for: %s' % (text)
46 return report_error(msg)
48 return redirect_to_url(hits[0][1])
50 msg = "%d hits found for (%s); multi-hit not implemented yet." % \
52 return report_error(msg)
55 #return json.dumps(hits)
58 def __magic_process(text):
60 Based on scanned text, check to see if there is map object to use
61 for a useful redirect.
64 split_text = text.split('|')
66 # There should always be at least one | in a valid scan.
67 if len(split_text) <= 1:
68 #return report_error('Invalid text: %s' % (text))
69 return __plugin_search(text)
71 # Keyword is the first element in the list
72 keyword = split_text[0]
74 # Attempt to find a KeywordMap based on keyword
76 keymap = models.KeywordMap.objects.get(keyword=keyword)
77 except ObjectDoesNotExist(e):
78 return report_error('Keyword (%s) is not defined' % (keyword))
80 # Remove keyword and only scan the content
81 content = '|'.join(split_text[1:])
83 #FIXME: would be faster to cache compiled regex
84 search = re.compile(keymap.regex)
86 mo = search.search(content)
88 # if the search was invalid
91 '(%s) failed to match (%s)' % (keymap.regex, content)
94 t = Template(keymap.url_template)
95 c = Context(mo.groupdict())
97 return redirect_to_url(str(t.render(c)))
106 #Retrieve posted text from processing
107 if 'text' in request.POST:
108 text = request.POST['text']
112 #Retrieve bmc_mode for processing
113 if 'bcm_mode' in request.POST:
114 bcm_mode = request.POST['bcm_mode']
118 ################################
120 ################################
122 # Did not receive text error
123 if text is None or text.strip() == '':
125 d['status'] = 'Did not recieve text'
127 return HttpResponse(json.dumps(d), 'text/plain')
129 # Did not receive bcm_mode error
130 if bcm_mode is None or bcm_mode.strip() == '':
132 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')
155 def json_test(request):
158 if 'text' in request.POST:
159 text = request.POST['text']
163 #return HttpResponse(json.dumps(request.POST.items()), 'text/plain')
164 if text is None or text.strip() == '':
166 d['status'] = 'Did not recieve text'
167 return HttpResponse(json.dumps(d), 'text/plain')
169 if text.split('|')[0] == 'url':
170 d['mode'] = 'redirect'
171 d['url'] = text.split('|')[1]
173 d['msg'] = 'Recieved text: %s' % (text)
176 return HttpResponse(json.dumps(d), 'text/plain')