Skip to content

Commit

Permalink
Listen for post_save on EmailAsUsernameProxyUser
Browse files Browse the repository at this point in the history
Before, we were listening to the `allauth.user_signed_up` signed up
signal to create the UserMetadata. This doesn't work if the `User` is
created with the `createsuperuser` management command, as
composed-configuration overrides that to create a user of type
`EmailAsUsernameProxyUser` (which is a subclass of `User`).
  • Loading branch information
mvandenburgh committed Jan 1, 2024
1 parent ec7344f commit 6b5b6e4
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions dandiapi/api/signals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from allauth.account.signals import user_signed_up
from composed_configuration._allauth_support.management.commands import createsuperuser
from corsheaders.signals import check_request_enabled
from django.conf import settings
from django.db.models.signals import post_save
Expand All @@ -14,18 +14,16 @@ def cors_allow_anyone_read_only(*, sender, request, **kwargs):
return request.method in ('GET', 'HEAD', 'OPTIONS')


@receiver(post_save, sender=settings.AUTH_USER_MODEL, dispatch_uid='create_auth_token')
def create_auth_token(*, sender, instance=None, created=False, **kwargs):
"""Create an auth token for every new user."""
# TODO: remove this receiver if https://github.com/girder/django-composed-configuration/issues/187
# is fixed
@receiver(post_save, sender=createsuperuser.user_model, dispatch_uid='initialize_new_user')
@receiver(post_save, sender=settings.AUTH_USER_MODEL, dispatch_uid='initialize_new_user')
def initialize_new_user(sender, instance, created=False, **kwargs):
"""Create an auth token and `UserMetadata` for every new user."""
if created:
Token.objects.create(user=instance)


@receiver(user_signed_up)
def user_signed_up_listener(*, sender, user, **kwargs):
"""Create UserMetadata whenever a user signs up."""
if settings.AUTO_APPROVE_USERS:
status = UserMetadata.Status.APPROVED
else:
status = UserMetadata.Status.INCOMPLETE
UserMetadata.objects.get_or_create(user=user, status=status)
UserMetadata.objects.get_or_create(user=instance, defaults={'status': status})

0 comments on commit 6b5b6e4

Please sign in to comment.