Skip to content

Commit

Permalink
Improve db population in development
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Feb 12, 2024
1 parent 40ce2a8 commit 7064dbd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
3 changes: 2 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ RADIS_SITE_NAME=radis.test
ADMIN_EMAIL=[email protected]
ADMIN_FIRST_NAME=Wilhelm
ADMIN_LAST_NAME=Roentgen
ADMIN_PASSWORD=mysecret
ADMIN_USERNAME=admin
ADMIN_PASSWORD=mysecret
ADMIN_AUTH_TOKEN = "f2e7412ca332a85e37f3fce88c6a1904fe35ad63"
BASE_URL=https://localhost
DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1
DJANGO_CSRF_TRUSTED_ORIGINS=
Expand Down
40 changes: 24 additions & 16 deletions radis/core/management/commands/populate_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

USER_COUNT = 20
GROUP_COUNT = 3
ADMIN_AUTH_TOKEN = "f2e7412ca332a85e37f3fce88c6a1904fe35ad63"
PACS_ITEMS = [
{"pacs_aet": "gepacs", "pacs_name": "GE PACS"},
{"pacs_aet": "synapse", "pacs_name": "Synapse"},
Expand Down Expand Up @@ -46,26 +45,35 @@ def feed_reports():
feed_report(report)


def create_users() -> list[User]:
def create_admin() -> User:
if "ADMIN_USERNAME" not in environ or "ADMIN_PASSWORD" not in environ:
print("Cave! No admin credentials found in environment. Using default ones.")

admin_data = {
"username": environ.get("ADMIN_USERNAME", "admin"),
"first_name": environ.get("ADMIN_FIRST_NAME", "Wilhelm"),
"last_name": environ.get("ADMIN_LAST_NAME", "Röntgen"),
"email": environ.get("ADMIN_EMAIL", "[email protected]"),
"password": environ.get("ADMIN_PASSWORD", "mysecret"),
}
admin = AdminUserFactory.create(**admin_data)

TokenFactory.create(
token_hashed=hash_token(ADMIN_AUTH_TOKEN),
fraction=ADMIN_AUTH_TOKEN[:FRACTION_LENGTH],
owner=admin,
expires=None,
admin = AdminUserFactory.create(
username=environ.get("ADMIN_USERNAME", "admin"),
first_name=environ.get("ADMIN_FIRST_NAME", "Wilhelm"),
last_name=environ.get("ADMIN_LAST_NAME", "Röntgen"),
email=environ.get("ADMIN_EMAIL", "[email protected]"),
password=environ.get("ADMIN_PASSWORD", "mysecret"),
)

if "ADMIN_AUTH_TOKEN" not in environ:
print("No admin auth token in environment. Skipping auth token creation.")
else:
auth_token = environ["ADMIN_AUTH_TOKEN"]
TokenFactory.create(
token_hashed=hash_token(auth_token),
fraction=auth_token[:FRACTION_LENGTH],
owner=admin,
expires=None,
)

return admin


def create_users() -> list[User]:
admin = create_admin()

users = [admin]

urgent_permissions = Permission.objects.filter(
Expand Down

0 comments on commit 7064dbd

Please sign in to comment.