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