1- import json
21import urlparse
32
43from django .conf import settings
54from django .core .cache import cache
5+ from django .core .exceptions import PermissionDenied
6+ from django .views import debug
67
78import requests
89from aesfield .field import AESField
9- from tastypie import http
10- from tastypie .exceptions import ImmediateHttpResponse
11- from tastypie_services .services import StatusError , StatusObject as Base
10+ from rest_framework .decorators import api_view
11+ from rest_framework .response import Response
1212
1313from lib .sellers .models import Seller
14- from solitude .base import Resource
1514from solitude .logger import getLogger
1615
1716log = getLogger ('s.services' )
1817
1918
20- class StatusObject (Base ):
19+ class StatusObject (object ):
20+
21+ def __init__ (self ):
22+ self .status = {}
23+ self .error = None
2124
2225 @property
2326 def is_proxy (self ):
@@ -26,22 +29,25 @@ def is_proxy(self):
2629 def test_cache (self ):
2730 # caching fails silently so we have to read from it after writing.
2831 cache .set ('status' , 'works' )
29-
3032 if cache .get ('status' ) == 'works' :
31- self .cache = True
33+ return True
34+
35+ return False
3236
3337 def test_db (self ):
3438 try :
3539 # exists is one of the fastest queries one can run.
3640 Seller .objects .exists ()
37- self . db = True
41+ return True
3842 except Exception :
3943 log .error ('Error connection to the db' , exc_info = True )
44+ return False
4045
4146 def test_settings (self ):
4247 # Warn if the settings are confused and the proxy settings are
4348 # mixed with non-proxy settings. At this time we can't tell if you
4449 # are running just the database server or solitude all in one.
50+ self .status ['settings' ] = True
4551 caches = getattr (settings , 'CACHES' , {})
4652 dbs = getattr (settings , 'DATABASES' , {})
4753
@@ -52,7 +58,7 @@ def test_settings(self):
5258 if (db .get ('ENGINE' , '' ) not in ['' ,
5359 'django.db.backends.dummy' ]):
5460 log .error ('Proxy db set to: %s' % engine )
55- self . settings = False
61+ return False
5662
5763 # There could be an issue if you share a proxy with the database
5864 # server, a local cache should be fine.
@@ -62,13 +68,17 @@ def test_settings(self):
6268 'django.core.cache.backends.dummy.DummyCache' ,
6369 'django.core.cache.backends.locmem.LocMemCache' ]):
6470 log .error ('Proxy cache set to: %s' % backend )
65- self . settings = False
71+ return False
6672
6773 # Tuck the encrypt test into settings.
6874 test = AESField (aes_key = 'bango:signature' )
6975 if test ._decrypt (test ._encrypt ('foo' )) != 'foo' :
70- self . settings = False
76+ return False
7177
78+ return True
79+
80+ def test_proxies (self ):
81+ self .status ['proxies' ] = True
7282 if not self .is_proxy and settings .BANGO_PROXY :
7383 # Ensure that we can speak to the proxy.
7484 home = urlparse .urlparse (settings .BANGO_PROXY )
@@ -77,7 +87,7 @@ def test_settings(self):
7787 requests .get (proxy , verify = True , timeout = 5 )
7888 except :
7989 log .error ('Proxy error: %s' % proxy , exc_info = True )
80- self . settings = False
90+ return False
8191
8292 if self .is_proxy :
8393 # Ensure that we can speak to Bango.
@@ -86,35 +96,60 @@ def test_settings(self):
8696 requests .get (url , verify = True , timeout = 30 )
8797 except :
8898 log .error ('Bango error: %s' % proxy , exc_info = True )
89- self . settings = False
99+ return False
90100
91- def test (self ):
92- self .test_cache ()
93- self .test_db ()
94- self .test_settings ()
101+ return True
95102
96- if self .is_proxy :
97- if self .settings and not (self .db and self .cache ):
98- return self
99- raise StatusError (str (self ))
100-
101- if self .db and self .cache :
102- return self
103- raise StatusError (str (self ))
104-
105-
106- class RequestResource (Resource ):
107- """
108- This is a resource that does nothing, just returns some information
109- about the request. Useful for testing that solitude is working for you.
110- """
111-
112- class Meta (Resource .Meta ):
113- list_allowed_methods = ['get' ]
114- resource_name = 'request'
115-
116- def obj_get_list (self , request , ** kwargs ):
117- content = {'authenticated' : request .OAUTH_KEY }
118- response = http .HttpResponse (content = json .dumps (content ),
119- content_type = 'application/json' )
120- raise ImmediateHttpResponse (response = response )
103+
104+ class TestError (Exception ):
105+ pass
106+
107+
108+ @api_view (['GET' ])
109+ def error (request ):
110+ raise TestError ('This is a test.' )
111+
112+
113+ @api_view (['GET' ])
114+ def status (request ):
115+ obj = StatusObject ()
116+ for key , method in (('proxies' , obj .test_proxies ),
117+ ('db' , obj .test_db ),
118+ ('cache' , obj .test_cache ),
119+ ('settings' , obj .test_settings )):
120+ obj .status [key ] = method ()
121+
122+ if obj .is_proxy :
123+ if (obj .status ['settings' ] and not
124+ (obj .status ['db' ] and obj .status ['cache' ])):
125+ code = 200
126+ else :
127+ # The proxy should have good settings but not the db or cache.
128+ code = 500
129+
130+ if obj .status ['db' ] and obj .status ['cache' ]:
131+ code = 200
132+ else :
133+ # The db instance should have a good db and cache.
134+ code = 500
135+ return Response (obj .status , status = code )
136+
137+
138+ @api_view (['GET' ])
139+ def request_resource (request ):
140+ return Response ({'authenticated' : request .OAUTH_KEY })
141+
142+
143+ @api_view (['GET' ])
144+ def settings_list (request ):
145+ if not getattr (settings , 'CLEANSED_SETTINGS_ACCESS' , False ):
146+ raise PermissionDenied
147+ return Response (sorted (debug .get_safe_settings ().keys ()))
148+
149+
150+ @api_view (['GET' ])
151+ def settings_view (request , setting ):
152+ if not getattr (settings , 'CLEANSED_SETTINGS_ACCESS' , False ):
153+ raise PermissionDenied
154+ return Response ({'key' : setting ,
155+ 'value' : debug .get_safe_settings ()[setting ]})
0 commit comments