| 1 |
|
| 2 |
""" |
| 3 |
Invitations that use an invitation model |
| 4 |
""" |
| 5 |
import email.utils |
| 6 |
from typing import List |
| 7 |
from typing import Optional |
| 8 |
from typing import Text |
| 9 |
from typing import Tuple |
| 10 |
|
| 11 |
from django.conf import settings |
| 12 |
from django.contrib.auth.models import AbstractUser |
| 13 |
from django.core.mail import EmailMessage |
| 14 |
from django.http import HttpRequest |
| 15 |
from django.http import HttpResponse |
| 16 |
from django.http import HttpResponseForbidden |
| 17 |
from django.shortcuts import get_object_or_404 |
| 18 |
from django.shortcuts import redirect |
| 19 |
from django.shortcuts import render |
| 20 |
from django.template import loader |
| 21 |
from django.urls import path |
| 22 |
from django.utils.translation import gettext_lazy as _ |
| 23 |
|
| 24 |
from organizations.backends.defaults import InvitationBackend |
| 25 |
from organizations.backends.forms import UserRegistrationForm |
| 26 |
from organizations.base import AbstractBaseOrganization |
| 27 |
from organizations.base import OrganizationInvitationBase |
| 28 |
|
| 29 |
|
| 30 |
class ModelInvitation(InvitationBackend): |
| 31 |
"""Invitation backend for model-tracked invitations""" |
| 32 |
|
| 33 |
notification_subject = "organizations/email/notification_subject.txt" |
| 34 |
notification_body = "organizations/email/notification_body.html" |
| 35 |
invitation_subject = "organizations/email/modeled_invitation_subject.txt" |
| 36 |
invitation_body = "organizations/email/modeled_invitation_body.html" |
| 37 |
reminder_subject = "organizations/email/modeled_reminder_subject.txt" |
| 38 |
reminder_body = "organizations/email/modeled_reminder_body.html" |
| 39 |
invitation_join_template = "organizations/invitation_join.html" |
| 40 |
|
| 41 |
form_class = UserRegistrationForm |
| 42 |
|
| 43 |
def __init__(self, org_model=None, namespace=None): |
| 44 |
super().__init__(org_model=org_model, namespace=namespace) |
| 45 |
self.invitation_model = ( |
| 46 |
self.org_model.invitation_model |
| 47 |
) |
| 48 |
|
| 49 |
def get_invitation_queryset(self): |
| 50 |
"""Return this to use a custom queryset that checks for expiration, for example""" |
| 51 |
return self.invitation_model.objects.all() |
| 52 |
|
| 53 |
def get_invitation_accepted_url(self): |
| 54 |
"""Returns the redirect URL after user accepts invitation""" |
| 55 |
return "/" |
| 56 |
|
| 57 |
def get_invitation_accepted_registered_url(self): |
| 58 |
"""Returns the redirect URL after new user accepts invitation""" |
| 59 |
return self.get_invitation_accepted_url() |
| 60 |
|
| 61 |
def activation_router(self, request, guid): |
| 62 |
"""""" |
| 63 |
invitation = get_object_or_404(self.get_invitation_queryset(), guid=guid) |
| 64 |
if invitation.invitee: |
| 65 |
return redirect(self.get_invitation_accepted_url()) |
| 66 |
|
| 67 |
if request.user.is_authenticated: |
| 68 |
return self.activate_existing_user_view(request, invitation) |
| 69 |
else: |
| 70 |
return self.activate_new_user_view(request, invitation) |
| 71 |
|
| 72 |
def activate_existing_user_view(self, request, invitation): |
| 73 |
|
| 74 |
"""""" |
| 75 |
if request.user == invitation.invited_by: |
| 76 |
return HttpResponseForbidden(_("This is not your invitation")) |
| 77 |
if request.method == "POST": |
| 78 |
invitation.activate(request.user) |
| 79 |
return redirect(self.get_invitation_accepted_url()) |
| 80 |
return render( |
| 81 |
request, self.invitation_join_template, {"invitation": invitation} |
| 82 |
) |
| 83 |
|
| 84 |
def activate_new_user_view(self, request, invitation): |
| 85 |
|
| 86 |
"""""" |
| 87 |
form = self.get_form(data=request.POST or None) |
| 88 |
if request.method == "POST" and form.is_valid(): |
| 89 |
new_user = form.save() |
| 90 |
invitation.activate(new_user) |
| 91 |
return redirect(self.get_invitation_accepted_registered_url()) |
| 92 |
return render( |
| 93 |
request, |
| 94 |
self.registration_form_template, |
| 95 |
{"invitation": invitation, "form": form}, |
| 96 |
) |
| 97 |
|
| 98 |
def get_urls(self): |
| 99 |
|
| 100 |
return [ |
| 101 |
path( |
| 102 |
"<uuid:guid>/", view=self.activation_router, name="invitations_register" |
| 103 |
) |
| 104 |
] |
| 105 |
|
| 106 |
@property |
| 107 |
def urls(self): |
| 108 |
|
| 109 |
return self.get_urls(), self.namespace or "registration" |
| 110 |
|
| 111 |
def invite_by_email(self, email, user, organization, **kwargs): |
| 112 |
""" |
| 113 |
Primary interface method by which one user invites another to join |
| 114 |
|
| 115 |
Args: |
| 116 |
email: |
| 117 |
request: |
| 118 |
**kwargs: |
| 119 |
|
| 120 |
Returns: |
| 121 |
an invitation instance |
| 122 |
|
| 123 |
Raises: |
| 124 |
MultipleObjectsReturned if multiple matching users are found |
| 125 |
|
| 126 |
""" |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
user_invitation = self.invitation_model.objects.create( |
| 135 |
invitee_identifier=email.lower(), |
| 136 |
invited_by=user, |
| 137 |
organization=organization, |
| 138 |
) |
| 139 |
self.send_invitation(user_invitation) |
| 140 |
return user_invitation |
| 141 |
|
| 142 |
def send_invitation(self, invitation, **kwargs): |
| 143 |
""" |
| 144 |
Sends an invitation message for a specific invitation. |
| 145 |
|
| 146 |
This could be overridden to do other things, such as sending a confirmation |
| 147 |
email to the sender. |
| 148 |
|
| 149 |
Args: |
| 150 |
invitation: |
| 151 |
|
| 152 |
Returns: |
| 153 |
|
| 154 |
""" |
| 155 |
return self.email_message( |
| 156 |
invitation.invitee_identifier, |
| 157 |
self.invitation_subject, |
| 158 |
self.invitation_body, |
| 159 |
invitation.invited_by, |
| 160 |
**kwargs |
| 161 |
).send() |
| 162 |
|
| 163 |
def email_message( |
| 164 |
self, |
| 165 |
recipient, |
| 166 |
subject_template, |
| 167 |
body_template, |
| 168 |
sender=None, |
| 169 |
message_class=EmailMessage, |
| 170 |
**kwargs |
| 171 |
): |
| 172 |
""" |
| 173 |
Returns an invitation email message. This can be easily overridden. |
| 174 |
For instance, to send an HTML message, use the EmailMultiAlternatives message_class |
| 175 |
and attach the additional conent. |
| 176 |
""" |
| 177 |
from_email = "%s %s <%s>" % ( |
| 178 |
sender.first_name, |
| 179 |
sender.last_name, |
| 180 |
email.utils.parseaddr(settings.DEFAULT_FROM_EMAIL)[1], |
| 181 |
) |
| 182 |
reply_to = "%s %s <%s>" % (sender.first_name, sender.last_name, sender.email) |
| 183 |
|
| 184 |
headers = {"Reply-To": reply_to} |
| 185 |
kwargs.update({"sender": sender, "recipient": recipient}) |
| 186 |
|
| 187 |
subject_template = loader.get_template(subject_template) |
| 188 |
body_template = loader.get_template(body_template) |
| 189 |
|
| 190 |
subject = subject_template.render( |
| 191 |
kwargs |
| 192 |
).strip() |
| 193 |
|
| 194 |
body = body_template.render(kwargs) |
| 195 |
|
| 196 |
return message_class(subject, body, from_email, [recipient], headers=headers) |