Skip to content

Commit c266c71

Browse files
Include package data in the build (GH-28)
2 parents af4c553 + 3a62b51 commit c266c71

File tree

12 files changed

+76
-27
lines changed

12 files changed

+76
-27
lines changed

.github/workflows/docs.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ name: docs
33
on:
44
push:
55
branches: [ master ]
6-
pull_request:
7-
branches: [ master ]
86

97
jobs:
108
deploy:

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
- name: Install dependencies
5252
run: |
5353
pip install --upgrade pip
54-
python -m pip install -e .
54+
sh build.sh
5555
pip install tox tox-gh-actions
5656
- name: Run tests using tox
5757
run: tox -e ${{ matrix.env }}

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
# caches
12
.idea
23
.tox
34
.pytest_cache
45
*.egg-info
56
__pycache__
67

8+
# docs
79
docs/node_modules
810
docs/package-lock.json
911
docs/.vitepress/cache
1012
docs/.vitepress/dist
13+
14+
# build
15+
build
16+
dist

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include src/django_forbid/templates/*.html

build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
# last version of `build` supporting Python 3.6
4+
pip install build==0.9.0
5+
6+
# build the wheel and install it
7+
WHEEL_NAME=$(python -m build | grep -Po "django_forbid-.*\.whl" | tail -n 1)
8+
pip install dist/$WHEEL_NAME

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ keywords =
2626
detection
2727
django-forbid
2828
license = MIT
29-
license_file = LICENSE
29+
license_files = LICENSE
3030
platforms = unix, linux, osx, win32
3131
classifiers =
3232
Operating System :: OS Independent
@@ -50,6 +50,7 @@ classifiers =
5050
[options]
5151
packages =
5252
django_forbid
53+
django_forbid.skills
5354
install_requires =
5455
Django>=2.1
5556
geoip2

src/django_forbid/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.1.0"
1+
__version__ = "0.1.2"

src/django_forbid/middleware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from .skills.forbid_network import ForbidNetworkMiddleware
44

55
__skills__ = (
6-
ForbidDeviceMiddleware,
7-
ForbidLocationMiddleware,
86
ForbidNetworkMiddleware,
7+
ForbidLocationMiddleware,
8+
ForbidDeviceMiddleware,
99
)
1010

1111

src/django_forbid/skills/forbid_network.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,17 @@ def erase_response_attributes():
2222
for attr in response_attributes:
2323
request.session.pop(attr)
2424

25+
def forbidden_page():
26+
# Redirects to the FORBIDDEN_NET URL if set.
27+
if Settings.has("OPTIONS.URL.FORBIDDEN_NET"):
28+
return redirect(Settings.get("OPTIONS.URL.FORBIDDEN_NET"))
29+
return HttpResponseForbidden()
30+
31+
geoip2_tz = request.session.get("GEOIP2_TZ")
32+
verified_tz = request.session.get("VERIFIED_TZ", "")
33+
2534
if any([
35+
verified_tz == geoip2_tz,
2636
# Checks if VPN is False or not set.
2737
not Settings.get("OPTIONS.VPN", False),
2838
# Checks if the request is an AJAX request.
@@ -33,19 +43,19 @@ def erase_response_attributes():
3343
]):
3444
return self.get_response(request)
3545

46+
# Checks if GEOIP2_TZ and VERIFIED_TZ don't exist.
47+
if all([verified_tz, geoip2_tz != "N/A"]):
48+
return forbidden_page()
49+
3650
if all(map(request.session.has_key, ("GEOIP2_TZ", *response_attributes))):
3751
# Handles if the user's timezone differs from the
3852
# one determined by GeoIP API. If so, VPN is used.
39-
verified_tz = request.session.get("VERIFIED_TZ")
40-
geoip2_tz = request.session.get("GEOIP2_TZ")
4153
client_tz = request.POST.get("CLIENT_TZ", verified_tz)
4254

4355
if geoip2_tz != "N/A" and client_tz != geoip2_tz:
56+
request.session["VERIFIED_TZ"] = ""
4457
erase_response_attributes()
45-
# Redirects to the FORBIDDEN_NET URL if set.
46-
if Settings.has("OPTIONS.URL.FORBIDDEN_NET"):
47-
return redirect(Settings.get("OPTIONS.URL.FORBIDDEN_NET"))
48-
return HttpResponseForbidden()
58+
return forbidden_page()
4959

5060
# Restores the response from the session.
5161
response = HttpResponse(**{

tests/test_network_middleware.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@ def skips(get_response, ip_address, ajax=False):
1212
return response.status_code == 200
1313

1414

15-
def forbids(get_response, ip_address):
16-
detector = Detector(get_response)
15+
def forbids_shared_session(detector, ip_address):
1716
response = detector.request_resource(ip_address)
18-
assert response.status_code == 302
19-
response = detector.request_access()
17+
if response.status_code == 302:
18+
response = detector.request_access(ip_address)
2019
return response.status_code == 403
2120

2221

23-
def forbids_shared_session(detector, ip_address):
24-
response = detector.request_resource(ip_address)
25-
assert response.status_code == 302
26-
response = detector.request_access()
27-
return response.status_code == 403
22+
def forbids(get_response, ip_address):
23+
detector = Detector(get_response)
24+
return forbids_shared_session(detector, ip_address)
2825

2926

3027
class Detector:
@@ -36,13 +33,15 @@ def request_resource(self, ip_address=""):
3633
"""Sends a request to the server to access a resource"""
3734
request = self.request.get()
3835
request.META["HTTP_X_FORWARDED_FOR"] = ip_address
39-
get_response = ForbidLocationMiddleware(self.get_response)
40-
return ForbidNetworkMiddleware(get_response)(request)
36+
get_response = ForbidNetworkMiddleware(self.get_response)
37+
return ForbidLocationMiddleware(get_response)(request)
4138

42-
def request_access(self):
39+
def request_access(self, ip_address=""):
4340
"""Simulates the request sent by the user browser to the server"""
4441
request = self.request.post({"CLIENT_TZ": "Europe/London"})
45-
return ForbidNetworkMiddleware(self.get_response)(request)
42+
request.META["HTTP_X_FORWARDED_FOR"] = ip_address
43+
get_response = ForbidNetworkMiddleware(self.get_response)
44+
return ForbidLocationMiddleware(get_response)(request)
4645

4746

4847
def test_should_allow_all_when_no_config_provided(get_response):

0 commit comments

Comments
 (0)