Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/85 manage secure admin panel for django app #86

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .devcontainer/.env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ DEBUG=False
OAUTH_CALLBACK_42API=oauth_callback_42api
CLIENT_ID_42API=client_id_42api
CLIENT_SECRET_42API=client_secret_42api
ADMIN_PANEL_URL=admin_panel_url
DJANGO_SUPERUSER_USERNAME=django_superuser_username
DJANGO_SUPERUSER_PASSWORD=django_superuser_password
DJANGO_SUPERUSER_EMAIL=django_superuser_email
NGINX_ORIGIN=nginx_origin
2 changes: 1 addition & 1 deletion .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
source: ./requirements/Nginx/conf.d/default.conf
target: /etc/nginx/conf.d/default.conf
- type: bind
source: ../pong/pong/static
source: ../pong/static
target: /usr/share/nginx/html/static
- type: bind
source: ../pong/media
Expand Down
1 change: 1 addition & 0 deletions .devcontainer/requirements/Django/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cd code
python manage.py makemigrations --no-input
python manage.py migrate --no-input
python manage.py collectstatic --no-input
python create_superuser.py

if [ $DEBUG = 1 ]; then
exec python manage.py runserver 0.0.0.0:8000
Expand Down
8 changes: 7 additions & 1 deletion pong/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

ALLOWED_HOSTS = ["*"]


# Application definition

INSTALLED_APPS = [
Expand Down Expand Up @@ -169,3 +168,10 @@
},
},
}

NGINX_ORIGIN = os.getenv("NGINX_ORIGIN")
CSRF_TRUSTED_ORIGINS = [
NGINX_ORIGIN,
]

ADMIN_PANEL_URL = os.getenv("ADMIN_PANEL_URL")
3 changes: 2 additions & 1 deletion pong/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@

from django.contrib import admin
from django.urls import path, re_path, include
from django.conf import settings
from pong.views.index import index

urlpatterns = [
path("admin/", admin.site.urls),
path(settings.ADMIN_PANEL_URL, admin.site.urls),
path("pong/", include("pong.urls")),
re_path(r"^.*$", index, name="index"),
]
23 changes: 23 additions & 0 deletions pong/create_superuser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
import django
from django.contrib.auth import get_user_model

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
django.setup()

User = get_user_model()

superuser_name = os.environ.get("DJANGO_SUPERUSER_USERNAME")
superuser_password = os.environ.get("DJANGO_SUPERUSER_PASSWORD")
superuser_email = os.environ.get("DJANGO_SUPERUSER_EMAIL")

if superuser_name and superuser_password:
if not User.objects.filter(name=superuser_name).exists():
print(f"Creating superuser {superuser_name}")
User.objects.create_superuser(
name=superuser_name, email=superuser_email, password=superuser_password
)
else:
print(f"Superuser {superuser_name} already exists")
else:
print("Superuser credentials not provided")
5 changes: 4 additions & 1 deletion pong/pong/admin.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# Register your models here.
from django.contrib import admin
from pong.models import User

admin.site.register(User)