21d45a1ec7d31825491e2908f3a1666ef838b8e9
[htsworkflow.git] / samplebc / bcmagic / views.py
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
5
6 from samplebc.bcmagic import models
7
8 import json
9 import re
10
11 from samplebc.bcmagic import forms
12
13 def index(request):
14     """
15     Display a barcode magic input box
16     """
17     form = forms.BarcodeMagicForm()
18     
19     return render_to_response('bcmagic.html', {'bcmagic': form},
20                               context_instance=RequestContext(request))
21
22
23 def __magic_process(text):
24     """
25     Based on scanned text, check to see if there is map object to use
26     for a useful redirect.
27     """
28     # Split text on |
29     split_text = text.split('|')
30     
31     # There should always be at least one | in a valid scan.
32     if len(split_text) <= 1:
33         return {'mode': 'Error',
34                 'status': 'Invalid text: %s' % (text)}
35     
36     # Keyword is the first element in the list
37     keyword = split_text[0]
38     
39     # Attempt to find a KeywordMap based on keyword
40     try:
41         keymap = models.KeywordMap.objects.get(keyword=keyword)
42     except ObjectDoesNotExist, e:
43         return {'mode': 'Error',
44                 'status': 'Keyword (%s) is not defined' % (keyword)}
45     
46     # Remove keyword and only scan the content
47     content = '|'.join(split_text[1:])
48     
49     #FIXME: would be faster to cache compiled regex
50     search = re.compile(keymap.regex)
51     
52     mo = search.search(content)
53     
54     # if the search was invalid
55     if not mo:
56         return {'mode': 'Error',
57                 'status': '(%s) failed to match (%s)' % (keymap.regex, content)}
58     
59     t = Template(keymap.url_template)
60     c = Context(mo.groupdict())
61     
62     return {'mode': 'redirect',
63             'url': str(t.render(c)) }
64     
65     
66     
67     
68
69 def magic(request):
70     """
71     Let the magic begin
72     """
73     d = {}
74     
75     if 'text' in request.POST:
76         text = request.POST['text']
77     else:
78         text = None
79     
80     #return HttpResponse(json.write(request.POST.items()), 'text/plain')
81     if text is None or text.strip() == '':
82         d['mode'] = 'Error'
83         d['status'] = 'Did not recieve text'
84         return HttpResponse(json.write(d), 'text/plain')
85     
86     if text.split('|')[0] == 'url':
87         d['mode'] = 'redirect'
88         d['url'] = text.split('|')[1]
89     else:
90         d = __magic_process(text)
91         
92     return HttpResponse(json.write(d), 'text/plain')
93
94
95
96 def json_test(request):
97     d = {}
98     
99     if 'text' in request.POST:
100         text = request.POST['text']
101     else:
102         text = None
103     
104     #return HttpResponse(json.write(request.POST.items()), 'text/plain')
105     if text is None or text.strip() == '':
106         d['mode'] = 'Error'
107         d['status'] = 'Did not recieve text'
108         return HttpResponse(json.write(d), 'text/plain')
109     
110     if text.split('|')[0] == 'url':
111         d['mode'] = 'redirect'
112         d['url'] = text.split('|')[1]
113     else:
114         d['msg'] = 'Recieved text: %s' % (text)
115         d['mode'] = 'clear'
116         
117     return HttpResponse(json.write(d), 'text/plain')