diff --git a/README.rst b/README.rst index 0d4143a..bf81caf 100644 --- a/README.rst +++ b/README.rst @@ -1,7 +1,7 @@ Django Ping =========== -Django Ping is utility that provides a lightweight endpoint for availability and uptime monitoring services. It +Django Ping is utility that provides a lightweight endpoint for availability and uptime monitoring services. It also provides hooks for testing stack components and reporting them via JSON. Current Version: 0.3.0 @@ -25,12 +25,12 @@ Hitting the endpoint returns a simple status 200 response. You can customize the message by adding to your Django settings:: PING_DEFAULT_RESPONSE = "All systems go!" - PING_DEFAULT_MIMETYPE = 'text/html' + PING_DEFAULT_CONTENT_TYPE = 'text/html' Hitting the url:: /ping - + displays:: All systems go! @@ -47,7 +47,7 @@ a series of status checks and reports the results. For example:: /ping?checks=true - + displays:: Your site is up! @@ -74,7 +74,7 @@ Specifying a ``fmt`` parameter to ``json`` returns more detailed and serialized For example:: /ping?fmt=json - + displays:: { diff --git a/ping/checks.py b/ping/checks.py index 85e0f55..41f6231 100644 --- a/ping/checks.py +++ b/ping/checks.py @@ -2,7 +2,13 @@ from django.utils.importlib import import_module from django.core.exceptions import ImproperlyConfigured -from ping.defaults import * +from django.contrib.auth import get_user_model + +from ping.defaults import PING_DEFAULT_CHECKS, PING_CELERY_TIMEOUT + + +AUTH_USER_MODEL = getattr(settings, "AUTH_USER_MODEL", "auth.User") + def checks(request): """ @@ -11,7 +17,7 @@ def checks(request): for that check. """ response_dict = {} - + #Taken straight from Django #If there is a better way, I don't know it for path in getattr(settings, 'PING_CHECKS', PING_DEFAULT_CHECKS): @@ -25,7 +31,7 @@ def checks(request): func = getattr(mod, attr) except AttributeError: raise ImproperlyConfigured('Module "%s" does not define a "%s" callable' % (module, attr)) - + key, value = func(request) response_dict[key] = value @@ -33,7 +39,7 @@ def checks(request): #DEFAULT SYSTEM CHECKS -#Database +#Database def check_database_sessions(request): from django.contrib.sessions.models import Session try: @@ -55,7 +61,7 @@ def check_database_sites(request): CACHE_KEY = 'django-ping-test' CACHE_VALUE = 'abc123' -def check_cache_set(request): +def check_cache_set(request): from django.core.cache import cache try: cache.set(CACHE_KEY, CACHE_VALUE, 30) @@ -63,7 +69,7 @@ def check_cache_set(request): except: return 'cache_set', False -def check_cache_get(request): +def check_cache_get(request): from django.core.cache import cache try: data = cache.get(CACHE_KEY) @@ -75,12 +81,11 @@ def check_cache_get(request): return 'cache_get', False -#User -def check_user_exists(request): - from django.contrib.auth.models import User +# User +def check_user_exists(request): try: username = request.GET.get('username') - u = User.objects.get(username=username) + get_user_model().objects.get(username=username) return 'user_exists', True except: return 'user_exists', False @@ -95,7 +100,7 @@ def check_celery(request): now = time() datetimenow = datetime.now() expires = datetimenow + timedelta(seconds=getattr(settings, 'PING_CELERY_TIMEOUT', PING_CELERY_TIMEOUT)) - + try: task = sample_task.apply_async(expires=expires) while expires > datetime.now(): diff --git a/ping/defaults.py b/ping/defaults.py index 9f9a23f..c8e9fa0 100644 --- a/ping/defaults.py +++ b/ping/defaults.py @@ -1,5 +1,5 @@ PING_DEFAULT_RESPONSE = "Your site is up!" -PING_DEFAULT_MIMETYPE = 'text/html' +PING_DEFAULT_CONTENT_TYPE = 'text/html' PING_DEFAULT_CHECKS = ( 'ping.checks.check_database_sessions', @@ -8,4 +8,4 @@ PING_BASIC_AUTH = False -PING_CELERY_TIMEOUT = 5 \ No newline at end of file +PING_CELERY_TIMEOUT = 5 diff --git a/ping/urls.py b/ping/urls.py index 78d7c37..6ce8ff8 100644 --- a/ping/urls.py +++ b/ping/urls.py @@ -1,4 +1,4 @@ -from django.conf.urls.defaults import patterns, include, url +from django.conf.urls import patterns, include, url from ping.views import status urlpatterns = patterns('', diff --git a/ping/views.py b/ping/views.py index 82cacd4..d46110d 100644 --- a/ping/views.py +++ b/ping/views.py @@ -1,6 +1,7 @@ +import json + from django.http import HttpResponse from django.conf import settings -from django.utils import simplejson from django.contrib.auth.decorators import login_required from django.views.decorators.csrf import csrf_exempt @@ -14,10 +15,10 @@ def status(request): """ Returns a simple HttpResponse """ - + response = "

%s

" % getattr(settings, 'PING_DEFAULT_RESPONSE', PING_DEFAULT_RESPONSE) - mimetype = getattr(settings, 'PING_DEFAULT_MIMETYPE', PING_DEFAULT_MIMETYPE) - + content_type = getattr(settings, 'PING_DEFAULT_CONTENT_TYPE', PING_DEFAULT_CONTENT_TYPE) + if request.GET.get('checks') == 'true': response_dict = checks(request) response += "
" @@ -28,11 +29,11 @@ def status(request): if request.GET.get('fmt') == 'json': try: - response = simplejson.dumps(response_dict) + response = json.dumps(response_dict) except UnboundLocalError: response_dict = checks(request) response = simplejson.dumps(response_dict) response = simplejson.dumps(response_dict, sort_keys=True) - mimetype = 'application/json' + content_type = 'application/json' - return HttpResponse(response, mimetype=mimetype, status=200) \ No newline at end of file + return HttpResponse(response, content_type=content_type, status=200)