Skip to content

Commit

Permalink
Fixes and tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Mar 28, 2024
1 parent 2547543 commit 2eb8d3f
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 58 deletions.
11 changes: 11 additions & 0 deletions .env.example.dev
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# Type hints based on parameter naming #####
#
# booleans: [<app_name>_] VERB_...
# [<app_name>_] ALLOW_... | ENABLE_... | FAIL_... | HAS_...
# | INCLUDE_... | IS_... | RUN_... | USE_... | ..._DEBUG
#
# numbers: [<app_name>_] ..._QUANTITY-NOUN
# [<app_name>_] ..._COUNT | ..._NUMBER | ..._RATE | ..._TOTAL
#
# strings: Everything else

# api deployment
ENVIRONMENT=development
API_PORT=8030
Expand Down
11 changes: 11 additions & 0 deletions .env.example.prod
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# Type hints based on parameter naming #####
#
# booleans: [<app_name>_] VERB_...
# [<app_name>_] ALLOW_... | ENABLE_... | FAIL_... | HAS_...
# | INCLUDE_... | IS_... | RUN_... | USE_... | ..._DEBUG
#
# numbers: [<app_name>_] ..._QUANTITY-NOUN
# [<app_name>_] ..._COUNT | ..._NUMBER | ..._RATE | ..._TOTAL
#
# strings: Everything else

# api deployment
ENVIRONMENT=production
API_PORT=8030
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ docker-compose up
You should be able to access the local environment site and admin at the following URLs:
- <http://localhost:8030/api/v1/>
- <http://localhost:8030/admin/>
- <http://localhost:8030/>
If you have problems starting the project, first check out
the [FAQ](https://github.com/code4romania/seismic-risc/wiki/FAQ) and if that doesn't work, ask someone from the
Expand Down
10 changes: 5 additions & 5 deletions backend/seismic_site/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@
i18n_patterns(
# URL patterns which accept a language prefix
path(
"admin/password_reset/",
"password_reset/",
auth_views.PasswordResetView.as_view(),
name="admin_password_reset",
),
path(
"admin/password_reset/done/",
"password_reset/done/",
auth_views.PasswordResetDoneView.as_view(),
name="password_reset_done",
),
path(
"admin/reset/<uidb64>/<token>/",
"reset/<uidb64>/<token>/",
auth_views.PasswordResetConfirmView.as_view(),
name="password_reset_confirm",
),
path(
"admin/reset/done/",
"reset/done/",
auth_views.PasswordResetCompleteView.as_view(),
name="password_reset_complete",
),
path("admin/", admin.site.urls),
path("", admin.site.urls),
)
+ [
# URL patterns which do not use a language prefix
Expand Down
Empty file.
65 changes: 65 additions & 0 deletions backend/utils/management/commands/_private/seed_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import logging

from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand

logger = logging.getLogger(__name__)


class CommonCreateUserCommand(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
"--username",
type=str,
help="Username of the superuser (default: email)",
required=False,
)
parser.add_argument(
"--first_name",
type=str,
help="First name of the superuser",
required=False,
)
parser.add_argument(
"--last_name",
type=str,
help="Last name of the superuser",
required=False,
)

@classmethod
def _create_user(
cls,
admin_email: str,
password: str,
is_superuser: bool,
is_staff: bool,
username: str = None,
first_name: str = "Admin",
last_name: str = "Admin",
):
if not password:
raise ValueError("Password is required. Please set the proper variables.")

user_model = get_user_model()

if user_model.objects.filter(email=admin_email).exists():
logger.warning("Super admin already exists")
return None

user = user_model(
email=admin_email,
username=username or admin_email,
first_name=first_name,
last_name=last_name,
is_active=True,
is_superuser=is_superuser,
is_staff=is_staff,
)
user.set_password(password)

user.save()

logger.info("Super admin created successfully")

return user
62 changes: 11 additions & 51 deletions backend/utils/management/commands/seed_superuser.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,27 @@
import logging

import environ
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.conf import settings

from ._private.seed_user import CommonCreateUserCommand

logger = logging.getLogger(__name__)


class Command(BaseCommand):
class Command(CommonCreateUserCommand):
help = "Command to create a superuser"

def add_arguments(self, parser):
parser.add_argument(
"--email",
type=str,
help="Email of the superuser",
required=True,
)
parser.add_argument(
"--username",
type=str,
help="Username of the superuser (default: email)",
required=False,
)
parser.add_argument(
"--first_name",
type=str,
help="First name of the superuser",
required=False,
)
parser.add_argument(
"--last_name",
type=str,
help="Last name of the superuser",
required=False,
)

def handle(self, *args, **kwargs):
user_model = get_user_model()
env = environ.Env()

admin_email = kwargs.get("email")
admin_username = kwargs.get("username") or admin_email
admin_first_name = kwargs.get("first_name") or ""
admin_last_name = kwargs.get("last_name") or ""

if user_model.objects.filter(email=admin_email).exists():
logger.info("Super admin already exists")
return 0
kwargs["last_name"] = "Super"
kwargs["first_name"] = "User"

superuser = user_model(
email=admin_email,
username=admin_username,
first_name=admin_first_name,
last_name=admin_last_name,
is_active=True,
self._create_user(
admin_email=settings.DJANGO_ADMIN_EMAIL,
password=settings.DJANGO_ADMIN_PASSWORD,
is_superuser=True,
is_staff=True,
first_name=kwargs.get("first_name", ""),
last_name=kwargs.get("last_name", ""),
)
superuser.set_password(env.str("DJANGO_ADMIN_PASSWORD"))

superuser.save()

logger.info("Super admin created successfully")

return 0
1 change: 0 additions & 1 deletion docker/s6-rc.d/init/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ if is_enabled "${RUN_CREATE_SUPER_USER:-False}"; then
echo "Running the superuser seed script"

python3 manage.py seed_superuser \
--email "${DJANGO_ADMIN_EMAIL}" \
--first_name "${DJANGO_ADMIN_FIRST_NAME}" \
--last_name "${DJANGO_ADMIN_LAST_NAME}"
fi
Expand Down

0 comments on commit 2eb8d3f

Please sign in to comment.