Skip to content

Commit 942bcc9

Browse files
committed
Add files
1 parent b21b967 commit 942bcc9

File tree

19 files changed

+346
-0
lines changed

19 files changed

+346
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,6 @@ ENV/
8787

8888
# Rope project settings
8989
.ropeproject
90+
91+
# BD
92+
*.sqlite3

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
django-dev-protector
2+
====================
3+
The app for freelance developers, that blocks site if needed.
4+
5+
It can be used in situations when a client don't want to pay your work. The app blocks all requests to the site and shows a template.
6+
7+
Installation
8+
------------
9+
settings.py
10+
```
11+
INSTALLED_APPS = [
12+
...
13+
14+
# import django_dev_protector
15+
'django_dev_protector',
16+
]
17+
18+
19+
MIDDLEWARE = [
20+
# set middleware class
21+
'django_dev_protector.middleware.ControlMiddleware',
22+
23+
...
24+
]
25+
```
26+
27+
Settings
28+
--------
29+
```
30+
# render a simple template
31+
PROTECT_TEMPLATE_NAME = 'django_dev_protector/index.html'
32+
33+
# if redirect url is set, then default template would be
34+
# redirects person after 10 sec
35+
PROTECT_REDIRECT_URL = None
36+
```

django_dev_protector/__init__.py

Whitespace-only changes.

django_dev_protector/apps.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from __future__ import unicode_literals
2+
3+
from django.apps import AppConfig
4+
5+
6+
class DjangoDevProtectorConfig(AppConfig):
7+
name = 'django_dev_protector'

django_dev_protector/management/__init__.py

Whitespace-only changes.

django_dev_protector/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from django.core.management.base import BaseCommand, CommandError
2+
from django_dev_protector.settings import PROTECT_HASH_FILE, DIR_NAME
3+
4+
def generate_hash():
5+
import random
6+
return '%032x' % random.getrandbits(128)
7+
8+
9+
class Command(BaseCommand):
10+
help = 'Generates hash for django-dev-protector.'
11+
12+
def handle(self, *args, **options):
13+
self.stdout.write('Generating random hash')
14+
r_hash = generate_hash()
15+
16+
try:
17+
f = open(DIR_NAME + PROTECT_HASH_FILE, mode='w')
18+
f.write(r_hash)
19+
f.close()
20+
self.stdout.write('Saving... OK.')
21+
self.stdout.write('Save your hash code:')
22+
self.stdout.write(r_hash)
23+
except Exception as e:
24+
self.stdout.write('Saving... ERROR.')
25+
self.stdout.write(e)

django_dev_protector/middleware.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from os import environ
2+
from re import match
3+
4+
from django.shortcuts import render, redirect
5+
from django.utils.deprecation import MiddlewareMixin
6+
from .settings import TEMPLATE_NAME, REDIRECT_URL, PROTECT_STATUS_VARIABLE, PROTECT_HASH_VARIABLE
7+
8+
9+
class ControlMiddleware(MiddlewareMixin):
10+
def process_request(self, request):
11+
url = match(r'^/django_dev_protector/(?P<hash>\w+)/(?P<status>(on)|(off))/$', request.path)
12+
if url and url.group('hash') == environ[PROTECT_HASH_VARIABLE]:
13+
if url.group('status') == 'on':
14+
status = 'True'
15+
else:
16+
status = 'False'
17+
from .setup import save_status
18+
environ[PROTECT_STATUS_VARIABLE] = status
19+
save_status(status)
20+
return redirect('/')
21+
22+
if environ.get(PROTECT_STATUS_VARIABLE) == 'True':
23+
from django.shortcuts import render
24+
return render(request, TEMPLATE_NAME, {
25+
'redirect_url': REDIRECT_URL
26+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6e271254f78907e1ffe558184fb58018
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
False

0 commit comments

Comments
 (0)