Skip to content

Commit 9b7b979

Browse files
committed
- Adding in offline
1 parent 51d8e0a commit 9b7b979

File tree

11 files changed

+151
-0
lines changed

11 files changed

+151
-0
lines changed

offline/LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright (c) 2014, Ankit Jaiswal
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
1. Redistributions of source code must retain the above copyright notice,
9+
this list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
3. Neither the name of the copyright holder nor the names of its contributors
16+
may be used to endorse or promote products derived from this software
17+
without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
21+
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23+
THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
24+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30+
THE POSSIBILITY OF SUCH DAMAGE.

offline/__init__.py

Whitespace-only changes.

offline/admin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- coding: utf-8 -*-
2+
from django.contrib import admin
3+
from models import Config
4+
5+
admin.site.register(Config)

offline/middleware.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import re
5+
6+
from django.template import Context
7+
from django.template.loader import get_template
8+
import offline
9+
from views import offline_view
10+
import sys
11+
12+
# Customized for ISB-CGC by spaquett@systemsbiology.org
13+
# Changes:
14+
# - Added exception for admin/ and static/ (so pages would load necessary JS and CSS, and so admin can access the
15+
# admin app to turn it back off).
16+
# - Altered how access is determined for users logged in
17+
# - Added sanity check for presence of content-type (since sometimes it wasn't there) to prevent KeyErrors
18+
19+
20+
class OfflineMiddleware(object):
21+
22+
def process_request(self, request):
23+
# Admin and static always go through
24+
if request.path.startswith('/admin') or request.path.startswith('/static'):
25+
return
26+
# If offline isn't enabled, pass through
27+
if not offline.is_enabled():
28+
return
29+
30+
# Offline is enabled; double-check for staff users
31+
if not (hasattr(request, 'user')) or not request.user.is_authenticated() or not (request.user.is_staff and request.user.is_superuser):
32+
return offline_view(request)
33+
34+
return
35+
36+
def process_response(self, request, response):
37+
return response
38+
39+
def is_html_response(self, response):
40+
return ('Content-Type' in response and response['Content-Type'].startswith('text/html'))

offline/migrations/0001_initial.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.9.6 on 2017-02-16 00:46
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
initial = True
11+
12+
dependencies = [
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='Config',
18+
fields=[
19+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('key', models.CharField(max_length=100, unique=True, verbose_name='Key')),
21+
('value', models.BooleanField(default=False, verbose_name='Value')),
22+
('message', models.TextField(verbose_name='Message')),
23+
],
24+
),
25+
]

offline/migrations/__init__.py

Whitespace-only changes.

offline/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.db import models
2+
from django.utils.translation import ugettext as _
3+
4+
class Config(models.Model):
5+
key = models.CharField(_('Key'), max_length=100, unique=True)
6+
value = models.BooleanField(_('Value'), default=False)
7+
message = models.TextField(_('Message'))
8+
9+
def __unicode__(self):
10+
return self.key

offline/offline.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from models import Config
2+
3+
4+
def is_enabled():
5+
try:
6+
return Config.objects.get(key='offline').value
7+
except Config.DoesNotExist:
8+
set_defaults()
9+
10+
11+
def set_defaults():
12+
option = Config.objects.create(
13+
key= 'offline',
14+
value= False,
15+
message= 'The site is under maintenance. Please come back soon.'
16+
)
17+
return option.value
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% load i18n %}
2+
<div style="font-size:30px; text-align:center; background:#ff0; padding:10px 0; border:1px solid #990;">
3+
{% trans "Site is under maintenance." %}
4+
</div>

offline/templates/offline.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>503 Service Unavailable</title>
6+
</head>
7+
<body>
8+
<h1>{{message}}</h1>
9+
</body>
10+
</html>

offline/views.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.http import HttpResponse
2+
from django.template import loader
3+
from models import Config
4+
5+
def offline_view(request):
6+
offline_message = Config.objects.get(key='offline').message
7+
context = {
8+
'message':offline_message
9+
}
10+
return HttpResponse(loader.render_to_string('offline.html', context), status=503)

0 commit comments

Comments
 (0)