835ccf42ddb63c0f290788d63d995f5004dcb3f3
[htsworkflow.git] / htsworkflow / frontend / 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 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
9 from htsworkflow.util.jsonutil import encode_json
10
11 import re
12
13 from htsworkflow.frontend.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/magic.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         j = json.JSONEncoder()
94         return HttpResponse(j.encode(d), 'text/plain')
95     
96     # Did not receive bcm_mode error
97     if bcm_mode is None or bcm_mode.strip() == '':
98         d['mode'] = 'Error'
99         d['status'] = 'Missing bcm_mode information'
100     
101     
102     ################################
103     # Figure out which mode to use
104     ################################
105     keyword = text.split('|')[0]
106     
107     # Handle URL mode by default
108     if keyword == 'url':
109         d['mode'] = 'redirect'
110         d['url'] = text.split('|')[1]
111         
112     # Pass off processing to plugins
113     elif bcm_mode != 'default':
114         d = bcm_plugin_processor(keyword, text, bcm_mode)
115     
116     # Try keyword mapper
117     else:
118         d = __magic_process(text)
119     
120     return HttpResponse(encode_json(d), 'text/plain')
121
122
123
124 def json_test(request):
125     d = {}
126     
127     if 'text' in request.POST:
128         text = request.POST['text']
129     else:
130         text = None
131     
132     #return HttpResponse(encode_json(request.POST.items()), 'text/plain')
133     if text is None or text.strip() == '':
134         d['mode'] = 'Error'
135         d['status'] = 'Did not recieve text'
136         return HttpResponse(encode_json(d), 'text/plain')
137     
138     if text.split('|')[0] == 'url':
139         d['mode'] = 'redirect'
140         d['url'] = text.split('|')[1]
141     else:
142         d['msg'] = 'Recieved text: %s' % (text)
143         d['mode'] = 'clear'
144     
145     return HttpResponse(json_encode(d), 'text/plain')