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 samplebc.bcmagic import models
7 from samplebc.bcmagic.utils import report_error, redirect_to_url
8 from samplebc.bcmagic.plugin import bcm_plugin_processor
13 from samplebc.bcmagic import forms
17 Display a barcode magic input box
19 form = forms.BarcodeMagicForm()
21 return render_to_response('bcmagic.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 return HttpResponse(json.write(d), 'text/plain')
95 # Did not receive bcm_mode error
96 if bcm_mode is None or bcm_mode.strip() == '':
98 d['status'] = 'Missing bcm_mode information'
101 ################################
102 # Figure out which mode to use
103 ################################
104 keyword = text.split('|')[0]
106 # Handle URL mode by default
108 d['mode'] = 'redirect'
109 d['url'] = text.split('|')[1]
111 # Pass off processing to plugins
112 elif bcm_mode != 'default':
113 d = bcm_plugin_processor(keyword, text, bcm_mode)
117 d = __magic_process(text)
119 return HttpResponse(json.write(d), 'text/plain')
123 def json_test(request):
126 if 'text' in request.POST:
127 text = request.POST['text']
131 #return HttpResponse(json.write(request.POST.items()), 'text/plain')
132 if text is None or text.strip() == '':
134 d['status'] = 'Did not recieve text'
135 return HttpResponse(json.write(d), 'text/plain')
137 if text.split('|')[0] == 'url':
138 d['mode'] = 'redirect'
139 d['url'] = text.split('|')[1]
141 d['msg'] = 'Recieved text: %s' % (text)
144 return HttpResponse(json.write(d), 'text/plain')