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.util.jsonutil import encode_json
13 except ImportError, e:
14 import simplejson as json
18 from htsworkflow.frontend.bcmagic import forms
22 Display a barcode magic input box
24 form = forms.BarcodeMagicForm()
26 return render_to_response('bcmagic/magic.html', {'bcmagic': form},
27 context_instance=RequestContext(request))
30 def __magic_process(text):
32 Based on scanned text, check to see if there is map object to use
33 for a useful redirect.
36 split_text = text.split('|')
38 # There should always be at least one | in a valid scan.
39 if len(split_text) <= 1:
40 return report_error('Invalid text: %s' % (text))
42 # Keyword is the first element in the list
43 keyword = split_text[0]
45 # Attempt to find a KeywordMap based on keyword
47 keymap = models.KeywordMap.objects.get(keyword=keyword)
48 except ObjectDoesNotExist, e:
49 return report_error('Keyword (%s) is not defined' % (keyword))
51 # Remove keyword and only scan the content
52 content = '|'.join(split_text[1:])
54 #FIXME: would be faster to cache compiled regex
55 search = re.compile(keymap.regex)
57 mo = search.search(content)
59 # if the search was invalid
61 return report_error('(%s) failed to match (%s)' % (keymap.regex, content))
63 t = Template(keymap.url_template)
64 c = Context(mo.groupdict())
66 return redirect_to_url(str(t.render(c)))
78 #Retrieve posted text from processing
79 if 'text' in request.POST:
80 text = request.POST['text']
84 #Retrieve bmc_mode for processing
85 if 'bcm_mode' in request.POST:
86 bcm_mode = request.POST['bcm_mode']
90 ################################
92 ################################
94 # Did not receive text error
95 if text is None or text.strip() == '':
97 d['status'] = 'Did not recieve text'
99 return HttpResponse(json.dumps(d), 'text/plain')
101 # Did not receive bcm_mode error
102 if bcm_mode is None or bcm_mode.strip() == '':
104 d['status'] = 'Missing bcm_mode information'
107 ################################
108 # Figure out which mode to use
109 ################################
110 keyword = text.split('|')[0]
112 # Handle URL mode by default
114 d['mode'] = 'redirect'
115 d['url'] = text.split('|')[1]
117 # Pass off processing to plugins
118 elif bcm_mode != 'default':
119 d = bcm_plugin_processor(keyword, text, bcm_mode)
123 d = __magic_process(text)
125 return HttpResponse(json.dumps(d), 'text/plain')
129 def json_test(request):
132 if 'text' in request.POST:
133 text = request.POST['text']
137 #return HttpResponse(json.dumps(request.POST.items()), 'text/plain')
138 if text is None or text.strip() == '':
140 d['status'] = 'Did not recieve text'
141 return HttpResponse(json.dumps(d), 'text/plain')
143 if text.split('|')[0] == 'url':
144 d['mode'] = 'redirect'
145 d['url'] = text.split('|')[1]
147 d['msg'] = 'Recieved text: %s' % (text)
150 return HttpResponse(json.dumps(d), 'text/plain')