GithubHelp home page GithubHelp logo

thinkjson / django-maintenance Goto Github PK

View Code? Open in Web Editor NEW
10.0 10.0 4.0 126 KB

Django middleware that allows you to perform maintenance on your application

License: GNU General Public License v3.0

Python 100.00%

django-maintenance's Introduction

Header image

About Me

I love APIs, coffee, and the Oxford comma. I am a full stack engineer working in Go, TypeScript, and Python. I have exceled in a number of roles in the Software Development Lifecycle, but my passion for making things makes me thrive in the role of individual contributer. I have been programming since age eight, and have always enjoyed seeing ideas come to life in my programs. I love to ship elegant code that solves real problems, and iterate based on real feedback from customers. I want to write software that improves people's lives, and which supports our abilities and enriches our relationships outside of the virtual world.

django-maintenance's People

Contributors

jonathan3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

django-maintenance's Issues

Upgrade code to work with the latest versions of Django

Hey,

I've installed your software, and I discovered a bug that I corrected, in middleware.py:

from maintenance.models import MaintenanceMessage
from datetime import datetime
from django.conf import settings
from django.template.loader import render_to_string
from django.template import RequestContext
from django.http import HttpResponse
from django.core.urlresolvers import resolve
from django.core.cache import cache
from django.db.models import Q

class MaintenanceMiddleware(object):
def process_request(self, request, messages=False):

    disable_for_superuser = getattr(settings, 'MAINTENANCE_DISABLE_FOR_SUPERUSER', False)

    if request.user.is_superuser and disable_for_superuser:
        return None

    if getattr(settings, 'MAINTENANCE_CACHE_MESSAGES', False):
        messages = cache.get('maintenance_messages')

    if messages == False: # before it was: if not messages:
        messages = MaintenanceMessage.objects.filter(start_time__lt=datetime.now())\
            .filter(\
            Q(end_time__gte=datetime.now()) | Q(end_time__isnull=True) )
        cache.set('maintenance_messages', messages, getattr(settings, 'MAINTENANCE_CACHE_SECONDS', 3600))

    try:
        view, args, kwargs = resolve(request.path)
    except Exception:
        return None
    if 'django.contrib.admin' not in view.__module__ and messages.count() > 0:
        template = render_to_string('503.html',
        {
            'title': 'Maintenance Mode',
            'messages': messages
        }, context_instance=RequestContext(request))
        return HttpResponse(template, status=503)
    else:
        return None

Now it works like a charm, thanks a lot for your efforts!

Anthony

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.