diff --git a/ping/checks.py b/ping/checks.py index 85e0f55..07de6c9 100644 --- a/ping/checks.py +++ b/ping/checks.py @@ -2,7 +2,8 @@ from django.utils.importlib import import_module from django.core.exceptions import ImproperlyConfigured -from ping.defaults import * +from ping.defaults import PING_DEFAULT_CHECKS, PING_CELERY_TIMEOUT + def checks(request): """ @@ -11,9 +12,9 @@ def checks(request): for that check. """ response_dict = {} - - #Taken straight from Django - #If there is a better way, I don't know it + + # 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): i = path.rfind('.') module, attr = path[:i], path[i+1:] @@ -25,37 +26,40 @@ 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 return response_dict -#DEFAULT SYSTEM CHECKS -#Database +# DEFAULT SYSTEM CHECKS + +# Database def check_database_sessions(request): from django.contrib.sessions.models import Session try: - session = Session.objects.all()[0] + Session.objects.all()[0] return 'db_sessions', True except: return 'db_sessions', False + def check_database_sites(request): from django.contrib.sites.models import Site try: - session = Site.objects.all()[0] + Site.objects.all()[0] return 'db_site', True except: return 'db_site', False -#Caching +# Caching 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 +67,8 @@ 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,18 +80,18 @@ def check_cache_get(request): return 'cache_get', False -#User -def check_user_exists(request): +# User +def check_user_exists(request): from django.contrib.auth.models import User try: username = request.GET.get('username') - u = User.objects.get(username=username) + User.objects.get(username=username) return 'user_exists', True except: return 'user_exists', False -#Celery +# Celery def check_celery(request): from datetime import datetime, timedelta from time import sleep, time @@ -95,14 +100,14 @@ 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(): - if task.ready() and task.result == True: + if task.ready() and task.result is True: finished = str(time() - now) - return 'celery', { 'success': True, 'time':finished } + return 'celery', {'success': True, 'time': finished} sleep(0.25) - return 'celery', { 'success': False } + return 'celery', {'success': False} except Exception: - return 'celery', { 'success': False } + return 'celery', {'success': False} diff --git a/ping/decorators.py b/ping/decorators.py index 0f81073..7fb8bc5 100644 --- a/ping/decorators.py +++ b/ping/decorators.py @@ -1,5 +1,4 @@ from functools import wraps -import base64 from django.http import HttpResponse from django.contrib.auth import authenticate, login @@ -7,12 +6,13 @@ from ping.defaults import PING_BASIC_AUTH + def http_basic_auth(func): """ Attempts to login user with u/p provided in HTTP_AUTHORIZATION header. If successful, returns the view, otherwise returns a 401. - If PING_BASIC_AUTH is False, then just return the view function - + If PING_BASIC_AUTH is False, then just return the view function + Modified code by: http://djangosnippets.org/users/bthomas/ from @@ -21,8 +21,7 @@ def http_basic_auth(func): @wraps(func) def _decorator(request, *args, **kwargs): if getattr(settings, 'PING_BASIC_AUTH', PING_BASIC_AUTH): - from django.contrib.auth import authenticate, login - if request.META.has_key('HTTP_AUTHORIZATION'): + if 'HTTP_AUTHORIZATION' in request.META: authmeth, auth = request.META['HTTP_AUTHORIZATION'].split(' ', 1) if authmeth.lower() == 'basic': auth = auth.strip().decode('base64') @@ -37,4 +36,4 @@ def _decorator(request, *args, **kwargs): return HttpResponse("No Credentials Provided", status=401) else: return func(request, *args, **kwargs) - return _decorator \ No newline at end of file + return _decorator diff --git a/ping/defaults.py b/ping/defaults.py index 9f9a23f..704c984 100644 --- a/ping/defaults.py +++ b/ping/defaults.py @@ -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/models.py b/ping/models.py index 0f02227..1bb8bf6 100644 --- a/ping/models.py +++ b/ping/models.py @@ -1 +1 @@ -from django.db import models \ No newline at end of file +# empty diff --git a/ping/tasks.py b/ping/tasks.py index ac2ce1a..706d7c2 100644 --- a/ping/tasks.py +++ b/ping/tasks.py @@ -1,5 +1,6 @@ from celery.task import task + @task() def sample_task(): - return True \ No newline at end of file + return True diff --git a/ping/urls.py b/ping/urls.py index 78d7c37..a09b814 100644 --- a/ping/urls.py +++ b/ping/urls.py @@ -1,7 +1,9 @@ -from django.conf.urls.defaults import patterns, include, url +from django.conf.urls.defaults import patterns, url from ping.views import status -urlpatterns = patterns('', + +urlpatterns = patterns( + '', + url(r'^$', status, name='status'), ) - diff --git a/ping/views.py b/ping/views.py index 82cacd4..a6bf707 100644 --- a/ping/views.py +++ b/ping/views.py @@ -1,23 +1,23 @@ 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 -from ping.defaults import * +from ping.defaults import PING_DEFAULT_RESPONSE, PING_DEFAULT_MIMETYPE from ping.checks import checks from ping.decorators import http_basic_auth + @csrf_exempt @http_basic_auth def status(request): """ Returns a simple HttpResponse """ - + response = "