예제 #1
0
    def create(self, validated_data: Dict[str, Any], *args: Any, **kwargs: Any) -> OrganizationInvite:
        if OrganizationMembership.objects.filter(
            organization_id=self.context["organization_id"], user__email=validated_data["target_email"]
        ).exists():
            raise exceptions.ValidationError("A user with this email address already belongs to the organization.")
        invite: OrganizationInvite = OrganizationInvite.objects.create(
            organization_id=self.context["organization_id"], created_by=self.context["request"].user, **validated_data,
        )

        if is_email_available(with_absolute_urls=True):
            invite.emailing_attempt_made = True
            send_invite.delay(invite_id=invite.id)
            invite.save()

        if not self.context.get("bulk_create"):
            report_team_member_invited(
                self.context["request"].user.distinct_id,
                name_provided=bool(validated_data.get("first_name")),
                current_invite_count=invite.organization.active_invites.count(),
                current_member_count=OrganizationMembership.objects.filter(
                    organization_id=self.context["organization_id"],
                ).count(),
                email_available=is_email_available(),
            )

        return invite
예제 #2
0
 def create(self, validated_data: Dict[str, Any], *args: Any,
            **kwargs: Any) -> OrganizationInvite:
     if OrganizationMembership.objects.filter(
             organization_id=self.context["organization_id"],
             user__email=validated_data["target_email"]).exists():
         raise exceptions.ValidationError(
             "A user with this email address already belongs to the organization."
         )
     invite: OrganizationInvite = OrganizationInvite.objects.create(
         organization_id=self.context["organization_id"],
         created_by=self.context["request"].user,
         target_email=validated_data["target_email"],
     )
     if is_email_available(with_absolute_urls=True):
         invite.emailing_attempt_made = True
         send_invite.delay(invite_id=invite.id)
         invite.save()
     return invite