How to associate invited users with the inviter's Company/group?

后端 未结 1 1113
無奈伤痛
無奈伤痛 2021-01-22 10:26

I am using Django, django-allauth and django-invitations. I am able to successfully invite users to the platform, but I would like to associate them with the inviter\'s Company.

相关标签:
1条回答
  • 2021-01-22 11:09

    I had to implement a Signal in django. It listens for a user signing up, then looks to see if that user is in the Invitation model. If so, it looks up the inviter's company and associates that with the user signing up.

    init.py

    default_app_config = "users.apps.UsersConfig"
    

    signals.py

    from allauth.account.signals import user_signed_up
    from django.dispatch import receiver
    
    from invitations.utils import get_invitation_model
    
    @receiver(user_signed_up)
    def user_signed_up(request, user, **kwargs):
        try:
            Invitation = get_invitation_model()
            invite = Invitation.objects.get(email=user.email)
        except Invitation.DoesNotExist:
            print("this was probably not an invited user.")
        else:
            user.company = invite.inviter.company
            user.save()
    
    0 讨论(0)
提交回复
热议问题