8deb42818f3653a7052ae76268aa72a57aefdc65
[unfold.git] / rest / update.py
1 from django.views.generic.base      import TemplateView
2 from django.shortcuts               import render_to_response
3
4 from unfold.loginrequired           import LoginRequiredView
5 from django.http                    import HttpResponse
6
7 from manifold.core.query            import Query, AnalyzedQuery
8 from manifoldapi.manifoldapi        import execute_query
9
10 from rest import ObjectRequest, error, success
11
12 from string import join
13
14 import json
15
16
17 def dispatch(request, object_type, object_name):
18     
19     o = ObjectRequest(request, object_type, object_name)    
20     
21     if request.method == 'POST':
22         req_items = request.POST
23     elif request.method == 'GET':
24         #return error('only post request is supported')
25         req_items = request.GET
26     print req_items
27     for el in req_items.items():
28         
29         print "#===============>",el
30         if el[0].startswith('filters'):
31             o.filters[el[0][8:-1]] = el[1]
32         elif el[0].startswith('params'):
33             print "#======> 0 ", el[0]
34             print "#======> 1 ", req_items.getlist(el[0])
35
36             if (el[0][-2:] == '[]') :
37                 # when receiving params[key][] = 'value1' ...
38                 #o.params.append({el[0][7:-3]:",".join(req_items.getlist(el[0]))})
39                 o.params.append({el[0][7:-3]:req_items.getlist(el[0])})
40             else :
41                 # when receiving params[key] = 'value'
42                 o.params.append({el[0][7:-1]:el[1]})
43             
44             print "o.params = ",o.params
45             
46         elif el[0].startswith('fields'):
47             o.fields=req_items.getlist('fields[]')
48         elif el[0].startswith('options'):
49             o.options = req_items.getlist('options[]')
50     
51     try:
52         response = o.update()
53
54         if response :
55             return success('record updated')
56         else :
57             return error('an error has occurred')
58  
59     except Exception, e:
60         return error("exception:"+str(e))
61