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
14 except ImportError, e:
15 import simplejson as json
17 from samplebc.bcmagic import forms
21 Display a barcode magic input box
23 form = forms.BarcodeMagicForm()
25 return render_to_response('bcmagic.html', {'bcmagic': form},
26 context_instance=RequestContext(request))
29 def __magic_process(text):
31 Based on scanned text, check to see if there is map object to use
32 for a useful redirect.
35 split_text = text.split('|')
37 # There should always be at least one | in a valid scan.
38 if len(split_text) <= 1:
39 return report_error('Invalid text: %s' % (text))
41 # Keyword is the first element in the list
42 keyword = split_text[0]
44 # Attempt to find a KeywordMap based on keyword
46 keymap = models.KeywordMap.objects.get(keyword=keyword)
47 except ObjectDoesNotExist, e:
48 return report_error('Keyword (%s) is not defined' % (keyword))
50 # Remove keyword and only scan the content
51 content = '|'.join(split_text[1:])
53 #FIXME: would be faster to cache compiled regex
54 search = re.compile(keymap.regex)
56 mo = search.search(content)
58 # if the search was invalid
60 return report_error('(%s) failed to match (%s)' % (keymap.regex, content))
62 t = Template(keymap.url_template)
63 c = Context(mo.groupdict())
65 return redirect_to_url(str(t.render(c)))
77 #Retrieve posted text from processing
78 if 'text' in request.POST:
79 text = request.POST['text']
83 #Retrieve bmc_mode for processing
84 if 'bcm_mode' in request.POST:
85 bcm_mode = request.POST['bcm_mode']
89 ################################
91 ################################
93 # Did not receive text error
94 if text is None or text.strip() == '':
96 d['status'] = 'Did not recieve text'
97 return HttpResponse(json.dumps(d), 'text/plain')
99 # Did not receive bcm_mode error
100 if bcm_mode is None or bcm_mode.strip() == '':
102 d['status'] = 'Missing bcm_mode information'
105 ################################
106 # Figure out which mode to use
107 ################################
108 keyword = text.split('|')[0]
110 # Handle URL mode by default
112 d['mode'] = 'redirect'
113 d['url'] = text.split('|')[1]
115 # Pass off processing to plugins
116 elif bcm_mode != 'default':
117 d = bcm_plugin_processor(keyword, text, bcm_mode)
121 d = __magic_process(text)
123 return HttpResponse(json.dumps(d), 'text/plain')
127 def json_test(request):
130 if 'text' in request.POST:
131 text = request.POST['text']
135 #return HttpResponse(json.dumps(request.POST.items()), 'text/plain')
136 if text is None or text.strip() == '':
138 d['status'] = 'Did not recieve text'
139 return HttpResponse(json.dumps(d), 'text/plain')
141 if text.split('|')[0] == 'url':
142 d['mode'] = 'redirect'
143 d['url'] = text.split('|')[1]
145 d['msg'] = 'Recieved text: %s' % (text)
148 return HttpResponse(json.dumps(d), 'text/plain')