Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

I've read over the bee-keeper/django-invitations and it doesn't seem to have the information on how to do this.

models.py

class Company(models.Model):
    name = models.CharField(max_length=100, default=None)

class CustomUser(AbstractUser):
    company = models.ForeignKey(Company, on_delete=models.CASCADE, blank=True, null=True)
    objects = CustomUserManager()

views.py

@login_required
def company_users(request):
    # Get users that are in the company's user database as well as users that have been invited
    company_users = CustomUser.objects.filter(company=request.user.company.id)
    Invitations = get_invitation_model()
    # I'm afraid this is going to get all invited users, not just those that belong to the company
    invited_users = Invitations.objects.filter()

    if request.method == 'POST':
        print(request.POST)
        invitees = request.POST['invitees']
        invitees = re.split(',', invitees)
        for invitee in invitees:
            Invitation = get_invitation_model()
            try:
                invite = Invitation.create(invitee, inviter=request.user)
                invite.send_invitation(request)
            except IntegrityError as e:
                print(type(e))
                print(dir(e))
                return render(request, "company_users.html", {
                    'message': e.args,
                    'company_users' : company_users,
                    'invited_users' : invited_users,
                    })

    
    return render(request, 'company_users.html', {
        'company_users' : company_users,
        'invited_users' : invited_users,
    })

In the code above, users are successfully invited to the platform, but the user is not associated with the Company of the inviter. I'm also afraid that the list of invited users is not restricted to the user's Company.

like image 943
J. Brett Cunningham Avatar asked Oct 26 '25 18:10

J. Brett Cunningham


1 Answers

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()
like image 64
J. Brett Cunningham Avatar answered Oct 29 '25 13:10

J. Brett Cunningham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!