diff --git a/.env.example.dev b/.env.example.dev index 09a27a17..7bbeff85 100644 --- a/.env.example.dev +++ b/.env.example.dev @@ -1,3 +1,14 @@ +# Type hints based on parameter naming ##### +# +# booleans: [_] VERB_... +# [_] ALLOW_... | ENABLE_... | FAIL_... | HAS_... +# | INCLUDE_... | IS_... | RUN_... | USE_... | ..._DEBUG +# +# numbers: [_] ..._QUANTITY-NOUN +# [_] ..._COUNT | ..._NUMBER | ..._RATE | ..._TOTAL +# +# strings: Everything else + # api deployment ENVIRONMENT=development API_PORT=8030 diff --git a/.env.example.prod b/.env.example.prod index 606677e1..37e14011 100644 --- a/.env.example.prod +++ b/.env.example.prod @@ -1,3 +1,14 @@ +# Type hints based on parameter naming ##### +# +# booleans: [_] VERB_... +# [_] ALLOW_... | ENABLE_... | FAIL_... | HAS_... +# | INCLUDE_... | IS_... | RUN_... | USE_... | ..._DEBUG +# +# numbers: [_] ..._QUANTITY-NOUN +# [_] ..._COUNT | ..._NUMBER | ..._RATE | ..._TOTAL +# +# strings: Everything else + # api deployment ENVIRONMENT=production API_PORT=8030 diff --git a/README.md b/README.md index a98c885b..c2e14834 100644 --- a/README.md +++ b/README.md @@ -292,7 +292,7 @@ docker-compose up You should be able to access the local environment site and admin at the following URLs: - -- +- 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 diff --git a/backend/seismic_site/urls.py b/backend/seismic_site/urls.py index 069f9840..2149fe43 100644 --- a/backend/seismic_site/urls.py +++ b/backend/seismic_site/urls.py @@ -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///", + "reset///", 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 diff --git a/backend/utils/management/commands/_private/__init__.py b/backend/utils/management/commands/_private/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/utils/management/commands/_private/seed_user.py b/backend/utils/management/commands/_private/seed_user.py new file mode 100644 index 00000000..82c7ff96 --- /dev/null +++ b/backend/utils/management/commands/_private/seed_user.py @@ -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 diff --git a/backend/utils/management/commands/seed_superuser.py b/backend/utils/management/commands/seed_superuser.py index 05437c07..5c2c34ad 100644 --- a/backend/utils/management/commands/seed_superuser.py +++ b/backend/utils/management/commands/seed_superuser.py @@ -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 diff --git a/docker/s6-rc.d/init/init.sh b/docker/s6-rc.d/init/init.sh index a88b5417..a8162af7 100755 --- a/docker/s6-rc.d/init/init.sh +++ b/docker/s6-rc.d/init/init.sh @@ -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