3
3
import json
4
4
import logging
5
5
6
- from django .utils .decorators import classonlymethod , method_decorator
7
- from django .views .generic import View
8
-
9
6
from no_exceptions .exceptions import Http400
10
7
11
8
from dimagi .utils .parsing import ISO_DATE_FORMAT
12
9
13
10
from corehq .apps .api .models import ESCase , ESXFormInstance
14
11
from corehq .apps .api .util import object_does_not_exist
15
- from corehq .apps .domain .decorators import login_and_domain_required
16
12
from corehq .apps .es import filters
17
13
from corehq .apps .es .cases import CaseES , case_adapter
18
14
from corehq .apps .es .exceptions import ESError
@@ -32,78 +28,18 @@ class DateTimeError(ValueError):
32
28
pass
33
29
34
30
35
- class ESView (View ):
36
- """
37
- Generic CBV for interfacing with the Elasticsearch REST api.
38
- This is necessary because tastypie's built in REST assumptions don't like
39
- ES's POST for querying, which we can set explicitly here.
40
-
41
- For security purposes, queries ought to be domain'ed by the requesting user, so a base_query
42
- is encouraged to be added.
43
-
44
- Access to the APIs can be done via url endpoints which are attached to the corehq.api.urls
31
+ class ESView :
32
+ """Wrapper for an ES adapter that may be used in a web context
45
33
46
- or programmatically via the self.run_query() method.
47
-
48
- This current iteration of the ESView must require a domain for its usage for security purposes.
34
+ Some errors are translated to HTTP errors.
49
35
"""
50
- #note - for security purposes, csrf protection is ENABLED
51
- #search POST queries must take the following format:
52
- #query={query_json}
53
- #csrfmiddlewaretoken=token
54
-
55
- #in curl, this is:
56
- #curl -b "csrftoken=<csrftoken>;sessionid=<session_id>"
57
- # -H "Content-Type: application/json"
58
- # -XPOST http://server/a/domain/api/v0.1/xform_es/
59
- # -d"[email protected] &csrfmiddlewaretoken=<csrftoken>"
60
- #or, call this programmatically to avoid CSRF issues.
61
-
62
- domain = ""
36
+
63
37
doc_type = None
64
38
model = None
65
39
66
- http_method_names = ['get' , 'post' , 'head' , ]
67
-
68
40
def __init__ (self , domain ):
69
- super (ESView , self ).__init__ ()
70
41
self .domain = domain .lower ()
71
42
72
- def head (self , * args , ** kwargs ):
73
- raise NotImplementedError ("Not implemented" )
74
-
75
- @method_decorator (login_and_domain_required )
76
- #@method_decorator(csrf_protect)
77
- # todo: csrf_protect temporarily removed and left to implementor's prerogative
78
- # getting ajax'ed csrf token method needs revisit.
79
- def dispatch (self , * args , ** kwargs ):
80
- req = args [0 ]
81
- self .pretty = req .GET .get ('pretty' , False )
82
- if self .pretty :
83
- self .indent = 4
84
- else :
85
- self .indent = None
86
- ret = super (ESView , self ).dispatch (* args , ** kwargs )
87
- return ret
88
-
89
- @classonlymethod
90
- def as_view (cls , ** initkwargs ):
91
- """
92
- Django as_view cannot be used since the constructor requires information only present in the request.
93
- """
94
- raise Exception ('as_view not supported for domain-specific ESView' )
95
-
96
- @classonlymethod
97
- def as_domain_specific_view (cls , ** initkwargs ):
98
- """
99
- Creates a simple domain-specific class-based view for passing through ES requests.
100
- """
101
- def view (request , domain , * args , ** kwargs ):
102
- self = cls (domain )
103
- return self .dispatch (request , domain , * args , ** kwargs )
104
-
105
- return view
106
-
107
43
def get_document (self , doc_id ):
108
44
try :
109
45
doc = self .adapter .get (doc_id )
0 commit comments