Skip to content

Commit 8a0e998

Browse files
committed
fix(core): Resolve startup and team management page errors
This commit includes two critical fixes: - Corrects the ALLOWED_HOSTS logic to reliably detect the server's IP at startup, resolving the DisallowedHost error. - Fixes a NameError on the Team Management page by adding the missing 'timedelta' import.
1 parent e3eb610 commit 8a0e998

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

inventory/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django.utils import timezone
1010
from django.db.models import Sum
1111
from django.db.models.signals import post_save
12+
from datetime import timedelta
1213

1314
import barcode
1415
from barcode.writer import SVGWriter

run.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,49 @@
1414

1515
import os
1616
import sys
17+
import socket
1718
from waitress import serve
18-
from sherlock.wsgi import application
19-
from django.core.management import execute_from_command_line
2019

21-
HOST = '0.0.0.0'
22-
PORT = 8000
23-
DB_FILE = 'db.sqlite3'
20+
21+
def discover_and_set_host_ip():
22+
"""Discovers the primary network IP and sets it as an environment variable."""
23+
detected_ip = '127.0.0.1'
24+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
25+
s.settimeout(0)
26+
try:
27+
s.connect(('10.254.254.254', 1))
28+
detected_ip = s.getsockname()[0]
29+
print(f"--- Automatically detected server IP: {detected_ip} ---")
30+
except Exception:
31+
print("--- Warning: Could not auto-detect network IP. Defaulting to localhost. ---")
32+
finally:
33+
s.close()
34+
os.environ['SHERLOCK_ALLOWED_IP'] = detected_ip
35+
return detected_ip
2436

2537
def run_migrations():
2638
"""Checks if the database exists and runs migrations if it doesn't."""
39+
from django.core.management import execute_from_command_line
2740
if not os.path.exists(DB_FILE):
2841
print("--- Database not found. Running initial setup... ---")
29-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sherlock.settings')
30-
3142
args = [sys.argv[0], 'migrate']
32-
3343
execute_from_command_line(args)
3444
print("--- Database created successfully. ---")
3545
else:
3646
print("--- Database found. ---")
3747

3848
if __name__ == "__main__":
49+
server_ip = discover_and_set_host_ip()
50+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sherlock.settings')
51+
52+
DB_FILE = 'db.sqlite3'
3953
run_migrations()
40-
54+
55+
from sherlock.wsgi import application
56+
57+
HOST = '0.0.0.0'
58+
PORT = 8000
4159
print("--- Starting Sherlock Production Server ---")
42-
print(f"Your application will be available at: http://<your_server_ip>:{PORT}")
60+
print(f"Your application should be available at: http://{server_ip}:{PORT}")
4361
print("Press Ctrl+C to stop the server.")
44-
4562
serve(application, host=HOST, port=PORT)

sherlock/settings.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@
5656

5757
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
5858
if not DEBUG:
59+
detected_ip = os.environ.get('SHERLOCK_ALLOWED_IP')
60+
if detected_ip:
61+
ALLOWED_HOSTS.append(detected_ip)
62+
5963
try:
6064
hostname = socket.gethostname()
61-
ALLOWED_HOSTS.append(hostname)
62-
ip_address = socket.gethostbyname(hostname)
63-
ALLOWED_HOSTS.append(ip_address)
64-
except socket.gaierror:
65-
print("Warning: Could not determine hostname or IP for ALLOWED_HOSTS. Network access may be limited.")
65+
if hostname:
66+
ALLOWED_HOSTS.append(hostname)
67+
except Exception:
68+
pass
6669

6770

6871
# Application definition

0 commit comments

Comments
 (0)