gpt4 book ai didi

django - 如何将受邀用户关联到邀请人的公司/群组?

转载 作者:行者123 更新时间:2023-12-02 03:12:34 32 4
gpt4 key购买 nike

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

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

模型.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()

View .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,
})

在上面的代码中,用户被成功邀请到平台,但用户与邀请人的公司没有关联。我也担心受邀用户的列表不限于用户的公司。

最佳答案

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

初始化.py

default_app_config = "users.apps.UsersConfig"

信号.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()

关于django - 如何将受邀用户关联到邀请人的公司/群组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57207796/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com