如何将受邀用户与邀请人的公司/组相关联? [英] How to associate invited users with the inviter's Company/group?

查看:109
本文介绍了如何将受邀用户与邀请人的公司/组相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django,django-allauth和django-invitations。我能够成功邀请用户使用该平台,但是我想将他们与邀请者的公司相关联。

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.

我已经阅读了养蜂人/ django-invitations,但它没有似乎没有有关如何执行此操作的信息。

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

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.

推荐答案

我必须实现一个Signal在Django。它侦听用户注册,然后查看该用户是否在邀请模型中。如果是这样,它将查找邀请者的公司,并将其与注册用户相关联。

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

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()

这篇关于如何将受邀用户与邀请人的公司/组相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆