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

Fix UserMetadata not being created if createsuperuser script is used #1113

Merged
merged 1 commit into from
Feb 26, 2024
Merged
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
33 changes: 33 additions & 0 deletions dandiapi/api/management/commands/createsuperuser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from composed_configuration._allauth_support.management.commands import createsuperuser
from django.db.models.signals import post_save

from dandiapi.api.models.user import UserMetadata

if TYPE_CHECKING:
from composed_configuration._allauth_support.createsuperuser import EmailAsUsernameProxyUser


def create_usermetadata(instance: EmailAsUsernameProxyUser, *args, **kwargs):
UserMetadata.objects.create(user=instance, status=UserMetadata.Status.APPROVED)


class Command(createsuperuser.Command):
def handle(self, *args, **kwargs) -> str | None:
# Temporarily connect a post_save signal handler so that we can catch the creation of
# this superuser. Note, we do this in the handle() method to ensure this only happens
# when this management command is actually run.
post_save.connect(create_usermetadata, sender=createsuperuser.user_model)

# Save the return value of the parent class function so we can return it later
return_value = super().handle(*args, **kwargs)

# Disconnect the signal handler. This isn't strictly necessary, but this avoids any
# unexpected behavior if, for example, someone extends this command and doesn't
# realize there's a signal handler attached dynamically.
post_save.disconnect(create_usermetadata, sender=createsuperuser.user_model)

return return_value