Skip to content

Commit

Permalink
Expose ports only within Docker and fix reverse url bug
Browse files Browse the repository at this point in the history
  • Loading branch information
okeneo committed Feb 27, 2024
1 parent 45b9b9e commit 854e741
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ celerybeat.pid
*.sage.py

# Environments
.env
.env*
.venv
env/
venv/
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Visit [https://okeneo.github.io/TBD/](https://okeneo.github.io/TBD/) to see the
- PostgreSQL

## Requirements
- Python 3.11
- Django 5.0
- Python 3.11.8
- Docker 24.0.7

## Installation

Expand Down
2 changes: 1 addition & 1 deletion backend/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ celerybeat.pid
*.sage.py

# Environments
.env
.env*
.venv
env/
venv/
Expand Down
14 changes: 5 additions & 9 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ ENV PYTHONUNBUFFERED 1

WORKDIR /backend

COPY requirements.txt /backend/
RUN pip install --upgrade pip

RUN pip install --upgrade pip && \
pip install -r requirements.txt
COPY requirements.txt /backend/
RUN pip install -r requirements.txt

COPY . /backend

EXPOSE 8000

# COPY ./entrypoint.sh .
# ENTRYPOINT ["sh", "./entrypoint.sh"]

CMD ["sh", "-c", "python manage.py makemigrations && python manage.py migrate && python manage.py collectstatic && gunicorn myproject.wsgi:application --bind 0.0.0.0:8000"]
RUN chmod +x ./entrypoint-api.sh
RUN chmod +x ./entrypoint-celery.sh
43 changes: 34 additions & 9 deletions backend/account/controller.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import validate_email

Expand All @@ -9,28 +10,52 @@
)
from .tasks import send_verification_email_task

# from django.urls import reverse
# verification_url = reverse("email_confimation")
EMAIL_TEMPLATES = {
"registration": {
"subject": "Verify your email address",
"message": "http://localhost:8000/api/blog/verify-email/?token_key={token_key}",
"message": "Follow this link to verify your account.",
"url_name": "blog:verify_email",
},
"update_email": {
"subject": "Email Update Verification",
"message": "http://localhost:8000/api/blog/verify-email-update/?token_key={token_key}"
+ "\nIf you didn't change it, you should click this link to recover it.",
"message": "If you didn't change it, you should click this link to recover it.",
"url_name": "blog:verify-email-update",
},
"reset_password": {
"subject": "Reset Password",
"message": "http://localhost:8000/blog/verify-reset-password/?token_key={token_key}"
+ "\nIf you didn't change it, you should click this link to recover it.",
"message": "If you didn't change it, you should click this link to recover it.",
"url_name": "blog:verify-password-reset",
},
}


def send_verification_email(template, email, token_key):
send_verification_email_task.delay(template, email, token_key)
def send_verification_email(template_type, email, token_key):
"""Send a verification email based on the specified template.
Args:
template_type (str): Type of email template.
email (str): Recipient's email address.
token_key (str): Unique token key for email verification.
"""
from django.urls import reverse_lazy

template = EMAIL_TEMPLATES.get(template_type)
if not template:
raise ValueError(f"Unsupported email template: {template}.")

# Get email url.
url_name = template["url_name"]
url = reverse_lazy(url_name)
HOST = settings.HOST
email_url = f"{HOST}{url}?token_key={token_key}"

# Collect email subject and message.
template_message = template["message"]
message = f"{template_message}\n{email_url}"
subject = template["subject"]

# Pass email sending to celery.
send_verification_email_task.delay(subject, message, email)


def validate_email_token_key(token_key):
Expand Down
24 changes: 8 additions & 16 deletions backend/account/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@


@shared_task
def send_verification_email_task(template, email, token_key):
from .controller import EMAIL_TEMPLATES

template = EMAIL_TEMPLATES.get(template)
if template:
subject = template["subject"]
message = template["message"].format(token_key=token_key)
send_mail(
subject=subject,
message=message,
from_email=None,
recipient_list=[email],
fail_silently=False,
)
else:
raise ValueError(f"Unsupported email template: {template}.")
def send_verification_email_task(subject, message, email):
send_mail(
subject=subject,
message=message,
from_email=None,
recipient_list=[email],
fail_silently=False,
)
3 changes: 2 additions & 1 deletion backend/blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
CustomTokenObtainPairView,
EmailUpdateView,
PasswordChangeView,
PasswordResetView,
RegisterView,
ResendVerificationEmailView,
PasswordResetView,
UserProfileView,
VerifyEmailUpdateView,
VerifyEmailView,
Expand All @@ -13,6 +13,7 @@
from django.urls import include, path
from rest_framework_simplejwt.views import TokenBlacklistView, TokenRefreshView

app_name = "blog"
urlpatterns = [
path("register/", RegisterView.as_view(), name="register"),
path("update-email/", EmailUpdateView.as_view(), name="update_email"),
Expand Down
29 changes: 15 additions & 14 deletions backend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ services:
context: .
dockerfile: Dockerfile
image: backend-api:1.0
ports:
- "8000:8000"
expose:
- "8000"
entrypoint: ./entrypoint-api.sh
env_file:
- .env
- .env.dev
volumes:
- staticfiles:/staticfiles
depends_on:
Expand All @@ -20,29 +21,28 @@ services:
context: .
dockerfile: Dockerfile
image: backend-celery:1.0
command: celery -A myproject worker -l INFO
entrypoint: ./entrypoint-celery.sh
environment:
- CELERY_BROKER=redis://redis:6379/0
env_file:
- .env
- .env.dev
depends_on:
- api
- redis

redis:
image: redis:7.2.4-alpine
command: redis-server
ports:
- "6379:6379"
expose:
- "6379"

postgres-db:
image: postgres:15
# volumes:
# - postgres_data:/var/lib/postgresql/data/
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file: .env.prod.db
expose:
- "5432"

nginx:
build:
Expand All @@ -56,4 +56,5 @@ services:
- api

volumes:
staticfiles: # postgres_data:
staticfiles:
postgres_data:
File renamed without changes.
3 changes: 3 additions & 0 deletions backend/entrypoint-celery.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

celery -A myproject worker -l INFO
16 changes: 7 additions & 9 deletions backend/myproject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from dotenv import load_dotenv

# Reload environment variables on startup to avoid caching them.
load_dotenv(verbose=True, override=True)
load_dotenv(dotenv_path="./.env.dev", verbose=True, override=True)

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -30,13 +30,9 @@
SECRET_KEY = config("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = config("DEBUG", default=0, cast=bool)

ALLOWED_HOSTS = [
"127.0.0.1",
"localhost",
"0.0.0.0",
]
ALLOWED_HOSTS = config("DJANGO_ALLOWED_HOSTS", default="").split(" ")


# Application definition
Expand Down Expand Up @@ -102,8 +98,8 @@

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": config("DB_NAME"),
"ENGINE": config("DB_ENGINE", default="django.db.backends.sqlite3"),
"NAME": config("DB_NAME", default=BASE_DIR / "db.sqlite3"),
"USER": config("DB_USER"),
"PASSWORD": config("DB_PASSWORD"),
"HOST": config("DB_HOST"),
Expand Down Expand Up @@ -237,3 +233,5 @@
},
}
}

HOST = config("HOST", default="http://localhost:8000")

0 comments on commit 854e741

Please sign in to comment.