Adding barcode magic commands:
[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 from samplebc.bcmagic.utils import report_error, redirect_to_url
8 from samplebc.bcmagic.plugin import bcm_plugin_processor
9
10 import json
11 import re
12
13 from samplebc.bcmagic import forms
14
15 def index(request):
16     """
17     Display a barcode magic input box
18     """
19     form = forms.BarcodeMagicForm()
20     
21     return render_to_response('bcmagic.html', {'bcmagic': form},
22                               context_instance=RequestContext(request))
23
24
25 def __magic_process(text):
26     """
27     Based on scanned text, check to see if there is map object to use
28     for a useful redirect.
29     """
30     # Split text on |
31     split_text = text.split('|')
32     
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))
36     
37     # Keyword is the first element in the list
38     keyword = split_text[0]
39     
40     # Attempt to find a KeywordMap based on keyword
41     try:
42         keymap = models.KeywordMap.objects.get(keyword=keyword)
43     except ObjectDoesNotExist, e:
44         return report_error('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 report_error('(%s) failed to match (%s)' % (keymap.regex, content))
57     
58     t = Template(keymap.url_template)
59     c = Context(mo.groupdict())
60     
61     return redirect_to_url(str(t.render(c)))
62     
63     
64     
65     
66
67 def magic(request):
68     """
69     Let the magic begin
70     """
71     d = {}
72     
73     #Retrieve posted text from processing
74     if 'text' in request.POST:
75         text = request.POST['text']
76     else:
77         text = None
78         
79     #Retrieve bmc_mode for processing
80     if 'bcm_mode' in request.POST:
81         bcm_mode = request.POST['bcm_mode']
82     else:
83         bcm_mode = None
84         
85     ################################
86     # Handle some errors
87     ################################
88     
89     # Did not receive text error
90     if text is None or text.strip() == '':
91         d['mode'] = 'Error'
92         d['status'] = 'Did not recieve text'
93         return HttpResponse(json.write(d), 'text/plain')
94     
95     # Did not receive bcm_mode error
96     if bcm_mode is None or bcm_mode.strip() == '':
97         d['mode'] = 'Error'
98         d['status'] = 'Missing bcm_mode information'
99     
100     
101     ################################
102     # Figure out which mode to use
103     ################################
104     keyword = text.split('|')[0]
105     
106     # Handle URL mode by default
107     if keyword == 'url':
108         d['mode'] = 'redirect'
109         d['url'] = text.split('|')[1]
110         
111     # Pass off processing to plugins
112     elif bcm_mode != 'default':
113         d = bcm_plugin_processor(keyword, text, bcm_mode)
114     
115     # Try keyword mapper
116     else:
117         d = __magic_process(text)
118         
119     return HttpResponse(json.write(d), 'text/plain')
120
121
122
123 def json_test(request):
124     d = {}
125     
126     if 'text' in request.POST:
127         text = request.POST['text']
128     else:
129         text = None
130     
131     #return HttpResponse(json.write(request.POST.items()), 'text/plain')
132     if text is None or text.strip() == '':
133         d['mode'] = 'Error'
134         d['status'] = 'Did not recieve text'
135         return HttpResponse(json.write(d), 'text/plain')
136     
137     if text.split('|')[0] == 'url':
138         d['mode'] = 'redirect'
139         d['url'] = text.split('|')[1]
140     else:
141         d['msg'] = 'Recieved text: %s' % (text)
142         d['mode'] = 'clear'
143         
144     return HttpResponse(json.write(d), 'text/plain')