Skip to content

Commit

Permalink
add create superuser function
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Karamanis committed Dec 3, 2024
1 parent 7ffa529 commit 91f80bf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion neu_users/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.4"
__version__ = "0.1.5"
3 changes: 3 additions & 0 deletions neu_users/config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__all__ = ["settings"]

from neu_users.config.settings import settings
23 changes: 23 additions & 0 deletions neu_users/config/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from dotenv import find_dotenv
from neu_sdk.config.settings import Settings as DefaultSettings
from pydantic import BaseModel, EmailStr, Field
from pydantic_settings import SettingsConfigDict


class Superuser(BaseModel):
password: str = Field("Neu123@!")
email: EmailStr = Field("info@neu.com")


class Settings(DefaultSettings):
model_config = SettingsConfigDict(
env_nested_delimiter="_",
env_file=find_dotenv(),
env_file_encoding="utf-8",
extra="ignore",
)

superuser: Superuser = Superuser()


settings = Settings()
25 changes: 23 additions & 2 deletions neu_users/main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
from neu_sdk.config import settings
from aredis_om import NotFoundError
from neu_sdk.config import LOGGER
from neu_sdk.interface import create_app
from uvicorn import run

from neu_users import __version__ as app_version
from neu_users.config import settings
from neu_users.routes import router
from neu_users.schemas import *
from neu_users.schemas import User, UserCreate
from neu_users.schemas import __version__ as schema_version


async def create_superuser():
try:
await User.find(User.superuser == True).first()
LOGGER.info("Superuser found")
except NotFoundError as e:
LOGGER.info("Superuser not found and it will be created.")
user = UserCreate(
first_name="Superuser",
username="superuser",
password=settings.superuser.password,
email=settings.superuser.email,
)
user = User.model_validate(user)
user.superuser = True
await user.save()


app = create_app(
service_name="neu-users",
tags=["neu", "microservice", "users"],
app_version=app_version,
schema_version=schema_version,
lifespan_before=[create_superuser()],
)
app.include_router(router)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ allow-direct-references = true

[tool.hatch.envs.lint]
detached = true
dependencies = ["mypy>=1.13.0", "ruff>=0.7.2"]
dependencies = ["mypy", "ruff"]

[tool.hatch.envs.lint.scripts]
typing = "mypy --install-types --non-interactive {args:neu_users tests}"
Expand Down

0 comments on commit 91f80bf

Please sign in to comment.