-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1113 from dandi/fix-user-metadata-creation
Fix `UserMetadata` not being created if `createsuperuser` script is used
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |