Skip to content

Commit

Permalink
Merge pull request #1113 from dandi/fix-user-metadata-creation
Browse files Browse the repository at this point in the history
Fix `UserMetadata` not being created if `createsuperuser` script is used
  • Loading branch information
mvandenburgh committed Feb 26, 2024
2 parents 5b6c66a + 749c467 commit 3cc675d
Showing 1 changed file with 33 additions and 0 deletions.
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

0 comments on commit 3cc675d

Please sign in to comment.