修改django-invitations软件包以允许团队功能

时间:2020-05-21 14:17:20

标签: python django

使用django-invitations,用户可以使用以下我从此问题中获得的代码(How to associate invited users with the inviter's Company/group?

)一次向其团队邀请多封电子邮件
@login_required
def invite_multi_premium(request):

    # Get users that are in the company's user database as well as users that have been invited
    taskly_users = TasklyUser.objects.filter(team=request.user.team)
    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':
        form = InviteMultipleEmailsForm(request.POST)
        print(request.POST, "THIS IS THE REQUEST POST")
        invitees2 = request.POST['emails']
        invitees = invitees2.split(', ')
        for invitee in invitees:
            Invitation = get_invitation_model()
            try:
                invite = Invitation.create(invitee, inviter=request.user, team=str(request.user.team))
                invite.send_invitation(request)
            except IntegrityError as e:
                print(type(e))
                print(dir(e))
                return render(request, 'invitations/forms/_invite_multi.html', {
                    "form":form
                    })

    else:
        form = InviteMultipleEmailsForm()
    return render(request, 'invitations/forms/_invite_multi.html', {
        "form":form
    })

当用户点击邀请链接时,他们将被带到注册表单(django allauth),并且电子邮件地址已预先填写。他们注册时,并不与邀请者所在的团队关联。这是我为了将“团队”添加到“邀请”模型中而修改的软件包的一部分,但是我无法让“团队”从“邀请”模型传递到“用户”模型。

class Invitation(AbstractBaseInvitation):
    email = models.EmailField(unique=True, verbose_name=_('e-mail address'),
                              max_length=app_settings.EMAIL_MAX_LENGTH)
    created = models.DateTimeField(verbose_name=_('created'),
                                   default=timezone.now)

    team = models.CharField(max_length=255, null=True, blank=True)

    @classmethod
    def create(cls, email, inviter=None, team=None, **kwargs):
        key = get_random_string(64).lower()
        instance = cls._default_manager.create(
            email=email,
            key=key,
            inviter=inviter,
            team=team,
            **kwargs)
        return instance

    def key_expired(self):
        expiration_date = (
            self.sent + datetime.timedelta(
                days=app_settings.INVITATION_EXPIRY))
        return expiration_date <= timezone.now()

    def send_invitation(self, request, **kwargs):
        current_site = kwargs.pop('site', Site.objects.get_current())
        invite_url = reverse('invitations:accept-invite',
                             args=[self.key])
        invite_url = request.build_absolute_uri(invite_url)
        ctx = kwargs
        ctx.update({
            'invite_url': invite_url,
            'site_name': current_site.name,
            'email': self.email,
            'team': self.team,
            'key': self.key,
            'inviter': self.inviter,
        })

        email_template = 'invitations/email/email_invite'

        get_invitations_adapter().send_mail(
            email_template,
            self.email,
            ctx)
        self.sent = timezone.now()
        self.save()

        signals.invite_url_sent.send(
            sender=self.__class__,
            instance=self,
            invite_url_sent=invite_url,
            inviter=self.inviter)

    def __str__(self):
        return "Invite: {0}".format(self.email)

1 个答案:

答案 0 :(得分:0)

通过将接收信号移到我的app / models.py中而不是在app / signals.py中解决了该问题。我在这篇文章中读过:https://simpleisbetterthancomplex.com/tutorial/2016/07/28/how-to-create-django-signals.html,您应该避免将接收器放在models.py文件中,但是缺少详细的解释。