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
13 from htsworkflow.frontend.bcmagic import forms
17 Display a barcode magic input box
19 form = forms.BarcodeMagicForm()
21 return render_to_response('bcmagic/magic.html', {'bcmagic': form},
22 context_instance=RequestContext(request))
25 def __magic_process(text):
27 Based on scanned text, check to see if there is map object to use
28 for a useful redirect.
31 split_text = text.split('|')
33 # There should always be at least one | in a valid scan.
34 if len(split_text) <= 1:
35 return report_error('Invalid text: %s' % (text))
37 # Keyword is the first element in the list
38 keyword = split_text[0]
40 # Attempt to find a KeywordMap based on keyword
42 keymap = models.KeywordMap.objects.get(keyword=keyword)
43 except ObjectDoesNotExist, e:
44 return report_error('Keyword (%s) is not defined' % (keyword))
46 # Remove keyword and only scan the content
47 content = '|'.join(split_text[1:])
49 #FIXME: would be faster to cache compiled regex
50 search = re.compile(keymap.regex)
52 mo = search.search(content)
54 # if the search was invalid
56 return report_error('(%s) failed to match (%s)' % (keymap.regex, content))
58 t = Template(keymap.url_template)
59 c = Context(mo.groupdict())
61 return redirect_to_url(str(t.render(c)))
73 #Retrieve posted text from processing
74 if 'text' in request.POST:
75 text = request.POST['text']
79 #Retrieve bmc_mode for processing
80 if 'bcm_mode' in request.POST:
81 bcm_mode = request.POST['bcm_mode']
85 ################################
87 ################################
89 # Did not receive text error
90 if text is None or text.strip() == '':
92 d['status'] = 'Did not recieve text'
93 j = json.JSONEncoder()
94 return HttpResponse(j.encode(d), 'text/plain')
96 # Did not receive bcm_mode error
97 if bcm_mode is None or bcm_mode.strip() == '':
99 d['status'] = 'Missing bcm_mode information'
102 ################################
103 # Figure out which mode to use
104 ################################
105 keyword = text.split('|')[0]
107 # Handle URL mode by default
109 d['mode'] = 'redirect'
110 d['url'] = text.split('|')[1]
112 # Pass off processing to plugins
113 elif bcm_mode != 'default':
114 d = bcm_plugin_processor(keyword, text, bcm_mode)
118 d = __magic_process(text)
119 j = json.JSONEncoder()
120 return HttpResponse(j.encode(d), 'text/plain')
124 def json_test(request):
127 if 'text' in request.POST:
128 text = request.POST['text']
132 #return HttpResponse(json.write(request.POST.items()), 'text/plain')
133 if text is None or text.strip() == '':
135 d['status'] = 'Did not recieve text'
136 return HttpResponse(json.write(d), 'text/plain')
138 if text.split('|')[0] == 'url':
139 d['mode'] = 'redirect'
140 d['url'] = text.split('|')[1]
142 d['msg'] = 'Recieved text: %s' % (text)
145 j = json.JSONEncoder()
146 return HttpResponse(j.encode(d), 'text/plain')