Made Diane's suggest json problem fix for python 2.5/2.6 support... now using simplej...
[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 try:
12     import json
13 except ImportError, e:
14     import simplejson as json
15
16 import re
17
18 from htsworkflow.frontend.bcmagic import forms
19
20 def index(request):
21     """
22     Display a barcode magic input box
23     """
24     form = forms.BarcodeMagicForm()
25     
26     return render_to_response('bcmagic/magic.html', {'bcmagic': form},
27                               context_instance=RequestContext(request))
28
29
30 def __magic_process(text):
31     """
32     Based on scanned text, check to see if there is map object to use
33     for a useful redirect.
34     """
35     # Split text on |
36     split_text = text.split('|')
37     
38     # There should always be at least one | in a valid scan.
39     if len(split_text) <= 1:
40         return report_error('Invalid text: %s' % (text))
41     
42     # Keyword is the first element in the list
43     keyword = split_text[0]
44     
45     # Attempt to find a KeywordMap based on keyword
46     try:
47         keymap = models.KeywordMap.objects.get(keyword=keyword)
48     except ObjectDoesNotExist, e:
49         return report_error('Keyword (%s) is not defined' % (keyword))
50     
51     # Remove keyword and only scan the content
52     content = '|'.join(split_text[1:])
53     
54     #FIXME: would be faster to cache compiled regex
55     search = re.compile(keymap.regex)
56     
57     mo = search.search(content)
58     
59     # if the search was invalid
60     if not mo:
61         return report_error('(%s) failed to match (%s)' % (keymap.regex, content))
62     
63     t = Template(keymap.url_template)
64     c = Context(mo.groupdict())
65     
66     return redirect_to_url(str(t.render(c)))
67     
68     
69     
70     
71
72 def magic(request):
73     """
74     Let the magic begin
75     """
76     d = {}
77     
78     #Retrieve posted text from processing
79     if 'text' in request.POST:
80         text = request.POST['text']
81     else:
82         text = None
83         
84     #Retrieve bmc_mode for processing
85     if 'bcm_mode' in request.POST:
86         bcm_mode = request.POST['bcm_mode']
87     else:
88         bcm_mode = None
89         
90     ################################
91     # Handle some errors
92     ################################
93     
94     # Did not receive text error
95     if text is None or text.strip() == '':
96         d['mode'] = 'Error'
97         d['status'] = 'Did not recieve text'
98         
99         return HttpResponse(json.dumps(d), 'text/plain')
100     
101     # Did not receive bcm_mode error
102     if bcm_mode is None or bcm_mode.strip() == '':
103         d['mode'] = 'Error'
104         d['status'] = 'Missing bcm_mode information'
105     
106     
107     ################################
108     # Figure out which mode to use
109     ################################
110     keyword = text.split('|')[0]
111     
112     # Handle URL mode by default
113     if keyword == 'url':
114         d['mode'] = 'redirect'
115         d['url'] = text.split('|')[1]
116         
117     # Pass off processing to plugins
118     elif bcm_mode != 'default':
119         d = bcm_plugin_processor(keyword, text, bcm_mode)
120     
121     # Try keyword mapper
122     else:
123         d = __magic_process(text)
124     
125     return HttpResponse(json.dumps(d), 'text/plain')
126
127
128
129 def json_test(request):
130     d = {}
131     
132     if 'text' in request.POST:
133         text = request.POST['text']
134     else:
135         text = None
136     
137     #return HttpResponse(json.dumps(request.POST.items()), 'text/plain')
138     if text is None or text.strip() == '':
139         d['mode'] = 'Error'
140         d['status'] = 'Did not recieve text'
141         return HttpResponse(json.dumps(d), 'text/plain')
142     
143     if text.split('|')[0] == 'url':
144         d['mode'] = 'redirect'
145         d['url'] = text.split('|')[1]
146     else:
147         d['msg'] = 'Recieved text: %s' % (text)
148         d['mode'] = 'clear'
149     
150     return HttpResponse(json.dumps(d), 'text/plain')