Skip to content

Commit 3c24631

Browse files
committed
update project
1 parent 0613da9 commit 3c24631

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

manage.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
"""Django's command-line utility for administrative tasks."""
33
import os
44
import sys
5+
from dotenv import load_dotenv
6+
from pathlib import Path
57

68

79
def main():
810
"""Run administrative tasks."""
11+
# Load environment variables from .env
12+
BASE_DIR = Path(__file__).resolve().parent.parent
13+
load_dotenv(dotenv_path=BASE_DIR / '.env')
14+
915
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'url_shortener.settings')
1016
try:
1117
from django.core.management import execute_from_command_line

url_shortener/asgi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
"""
99

1010
import os
11+
from dotenv import load_dotenv
12+
from pathlib import Path
1113

1214
from django.core.asgi import get_asgi_application
1315

16+
# Load environment variables from .env
17+
BASE_DIR = Path(__file__).resolve().parent.parent
18+
load_dotenv(dotenv_path=BASE_DIR / '.env')
19+
1420
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'url_shortener.settings')
1521

1622
application = get_asgi_application()

url_shortener/settings.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,26 @@
1010
https://docs.djangoproject.com/en/5.1/ref/settings/
1111
"""
1212

13+
import os
1314
from pathlib import Path
15+
from dotenv import load_dotenv
1416

1517
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1618
BASE_DIR = Path(__file__).resolve().parent.parent
19+
load_dotenv(dotenv_path=BASE_DIR / '.env')
1720

1821

1922
# Quick-start development settings - unsuitable for production
2023
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
2124

2225
# SECURITY WARNING: keep the secret key used in production secret!
23-
SECRET_KEY = 'django-insecure--wx4z5k+&&f%(gr@a4sf$ukenexsr@=ff5uq4(psr9q#$+&)19'
26+
SECRET_KEY = os.getenv('SECRET_KEY')
2427

2528
# SECURITY WARNING: don't run with debug turned on in production!
29+
# DEBUG = os.getenv('DEBUG', 'False').lower() in ['true', '1', 't']
2630
DEBUG = True
27-
28-
ALLOWED_HOSTS = []
31+
# ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '').split(',')
32+
ALLOWED_HOSTS = ['*']
2933

3034

3135
# Application definition
@@ -122,3 +126,12 @@
122126
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
123127

124128
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
129+
130+
# Additional settings
131+
BITLY_API_KEY = os.getenv('BITLY_API_KEY')
132+
CUTLLY_API_KEY = os.getenv('CUTLLY_API_KEY')
133+
134+
135+
print(f"SECRET_KEY: {SECRET_KEY}")
136+
print(f"BITLY_API_KEY: {BITLY_API_KEY}")
137+
print(f"CUTLLY_API_KEY: {CUTLLY_API_KEY}")

url_shortener/views.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import re
23

34
import pyshorteners
@@ -7,6 +8,9 @@
78

89
from .forms import get_url
910

11+
BITLY_API_KEY = os.environ.get("BITLY_API_KEY")
12+
CUTLLY_API_KEY = os.environ.get("CUTLLY_API_KEY")
13+
1014

1115
def generate_url(request):
1216
form = get_url(request.POST or None)
@@ -46,7 +50,7 @@ def generate_url(request):
4650
s = pyshorteners.Shortener()
4751
shortened_url = s.tinyurl.short(long_url)
4852
elif site == "bitly":
49-
s = pyshorteners.Shortener(api_key='de36c105ae76331609a19ff1ce2d391012d5710a')
53+
s = pyshorteners.Shortener(api_key=BITLY_API_KEY)
5054
shortened_url = s.bitly.short(long_url)
5155
elif site == "chilpit":
5256
s = pyshorteners.Shortener()
@@ -55,7 +59,7 @@ def generate_url(request):
5559
s = pyshorteners.Shortener()
5660
shortened_url = s.clckru.short(long_url)
5761
elif site == "cuttly":
58-
s = pyshorteners.Shortener(api_key='3745d515279a2c9396a5144c99c0017ed81b9')
62+
s = pyshorteners.Shortener(api_key=CUTLLY_API_KEY)
5963
shortened_url = s.cuttly.short(long_url)
6064
elif site == "dagd":
6165
s = pyshorteners.Shortener()
@@ -66,4 +70,4 @@ def generate_url(request):
6670
except pyshorteners.exceptions.ShorteningErrorException as e:
6771
error_message = "Failed to shorten the URL. Please try again later."
6872

69-
return render(request, 'home.html', {'form': form, 'shortened_url': shortened_url, 'error_message': error_message})
73+
return render(request, 'home.html', {'form': form, 'shortened_url': shortened_url, 'error_message': error_message})

url_shortener/wsgi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
"""
99

1010
import os
11+
from dotenv import load_dotenv
12+
from pathlib import Path
1113

1214
from django.core.wsgi import get_wsgi_application
1315

16+
# Load environment variables from .env
17+
BASE_DIR = Path(__file__).resolve().parent.parent
18+
load_dotenv(dotenv_path=BASE_DIR / '.env')
19+
1420
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'url_shortener.settings')
1521

1622
application = get_wsgi_application()

0 commit comments

Comments
 (0)