| 1 |
|
| 2 |
from django import forms |
| 3 |
from django.contrib.auth import get_user_model |
| 4 |
from django.contrib.sites.shortcuts import get_current_site |
| 5 |
from django.utils.translation import gettext_lazy as _ |
| 6 |
|
| 7 |
from organizations.backends import invitation_backend |
| 8 |
from organizations.models import Organization |
| 9 |
from organizations.models import OrganizationUser |
| 10 |
from organizations.utils import create_organization |
| 11 |
|
| 12 |
|
| 13 |
class OrganizationForm(forms.ModelForm): |
| 14 |
"""Form class for updating Organizations""" |
| 15 |
|
| 16 |
owner = forms.ModelChoiceField(OrganizationUser.objects.all()) |
| 17 |
|
| 18 |
def __init__(self, request, *args, **kwargs): |
| 19 |
self.request = request |
| 20 |
super().__init__(*args, **kwargs) |
| 21 |
self.fields["owner"].queryset = self.instance.organization_users.filter( |
| 22 |
is_admin=True, user__is_active=True |
| 23 |
) |
| 24 |
self.fields["owner"].initial = self.instance.owner.organization_user |
| 25 |
|
| 26 |
class Meta: |
| 27 |
model = Organization |
| 28 |
exclude = ("users", "is_active") |
| 29 |
|
| 30 |
def save(self, commit=True): |
| 31 |
if self.instance.owner.organization_user != self.cleaned_data["owner"]: |
| 32 |
self.instance.change_owner(self.cleaned_data["owner"]) |
| 33 |
return super().save(commit=commit) |
| 34 |
|
| 35 |
def clean_owner(self): |
| 36 |
owner = self.cleaned_data["owner"] |
| 37 |
if owner != self.instance.owner.organization_user: |
| 38 |
if self.request.user != self.instance.owner.organization_user.user: |
| 39 |
raise forms.ValidationError( |
| 40 |
_("Only the organization owner can change ownerhip") |
| 41 |
) |
| 42 |
return owner |
| 43 |
|
| 44 |
|
| 45 |
class OrganizationUserForm(forms.ModelForm): |
| 46 |
"""Form class for updating OrganizationUsers""" |
| 47 |
|
| 48 |
class Meta: |
| 49 |
model = OrganizationUser |
| 50 |
exclude = ("organization", "user") |
| 51 |
|
| 52 |
def clean_is_admin(self): |
| 53 |
is_admin = self.cleaned_data["is_admin"] |
| 54 |
if ( |
| 55 |
self.instance.organization.owner.organization_user == self.instance |
| 56 |
and not is_admin |
| 57 |
): |
| 58 |
raise forms.ValidationError(_("The organization owner must be an admin")) |
| 59 |
return is_admin |
| 60 |
|
| 61 |
|
| 62 |
class OrganizationUserAddForm(forms.ModelForm): |
| 63 |
"""Form class for adding OrganizationUsers to an existing Organization""" |
| 64 |
|
| 65 |
email = forms.EmailField(max_length=75) |
| 66 |
|
| 67 |
def __init__(self, request, organization, *args, **kwargs): |
| 68 |
self.request = request |
| 69 |
self.organization = organization |
| 70 |
super().__init__(*args, **kwargs) |
| 71 |
|
| 72 |
class Meta: |
| 73 |
model = OrganizationUser |
| 74 |
exclude = ("user", "organization") |
| 75 |
|
| 76 |
def save(self, *args, **kwargs): |
| 77 |
""" |
| 78 |
The save method should create a new OrganizationUser linking the User |
| 79 |
matching the provided email address. If not matching User is found it |
| 80 |
should kick off the registration process. It needs to create a User in |
| 81 |
order to link it to the Organization. |
| 82 |
""" |
| 83 |
try: |
| 84 |
user = get_user_model().objects.get( |
| 85 |
email__iexact=self.cleaned_data["email"] |
| 86 |
) |
| 87 |
except get_user_model().DoesNotExist: |
| 88 |
user = invitation_backend().invite_by_email( |
| 89 |
self.cleaned_data["email"], |
| 90 |
**{ |
| 91 |
"domain": get_current_site(self.request), |
| 92 |
"organization": self.organization, |
| 93 |
"sender": self.request.user, |
| 94 |
} |
| 95 |
) |
| 96 |
|
| 97 |
|
| 98 |
invitation_backend().send_notification( |
| 99 |
user, |
| 100 |
**{ |
| 101 |
"domain": get_current_site(self.request), |
| 102 |
"organization": self.organization, |
| 103 |
"sender": self.request.user, |
| 104 |
} |
| 105 |
) |
| 106 |
return OrganizationUser.objects.create( |
| 107 |
user=user, |
| 108 |
organization=self.organization, |
| 109 |
is_admin=self.cleaned_data["is_admin"], |
| 110 |
) |
| 111 |
|
| 112 |
def clean_email(self): |
| 113 |
email = self.cleaned_data["email"] |
| 114 |
if self.organization.users.filter(email__iexact=email).exists(): |
| 115 |
raise forms.ValidationError( |
| 116 |
_("There is already an organization " "member with this email address!") |
| 117 |
) |
| 118 |
if get_user_model().objects.filter(email__iexact=email).count() > 1: |
| 119 |
raise forms.ValidationError( |
| 120 |
_("This email address has been used multiple times.") |
| 121 |
) |
| 122 |
return email |
| 123 |
|
| 124 |
|
| 125 |
class OrganizationAddForm(forms.ModelForm): |
| 126 |
""" |
| 127 |
Form class for creating a new organization, complete with new owner, including a |
| 128 |
User instance, OrganizationUser instance, and OrganizationOwner instance. |
| 129 |
""" |
| 130 |
|
| 131 |
email = forms.EmailField( |
| 132 |
max_length=75, help_text=_("The email address for the account owner") |
| 133 |
) |
| 134 |
|
| 135 |
def __init__(self, request, *args, **kwargs): |
| 136 |
self.request = request |
| 137 |
super().__init__(*args, **kwargs) |
| 138 |
|
| 139 |
class Meta: |
| 140 |
model = Organization |
| 141 |
exclude = ("users", "is_active") |
| 142 |
|
| 143 |
def save(self, **kwargs): |
| 144 |
""" |
| 145 |
Create the organization, then get the user, then make the owner. |
| 146 |
""" |
| 147 |
is_active = True |
| 148 |
try: |
| 149 |
user = get_user_model().objects.get(email=self.cleaned_data["email"]) |
| 150 |
except get_user_model().DoesNotExist: |
| 151 |
|
| 152 |
user = invitation_backend().invite_by_email( |
| 153 |
self.cleaned_data["email"], |
| 154 |
**{ |
| 155 |
"domain": get_current_site(self.request), |
| 156 |
"organization": self.cleaned_data["name"], |
| 157 |
"sender": self.request.user, |
| 158 |
"created": True, |
| 159 |
} |
| 160 |
) |
| 161 |
is_active = False |
| 162 |
return create_organization( |
| 163 |
user, |
| 164 |
self.cleaned_data["name"], |
| 165 |
self.cleaned_data["slug"], |
| 166 |
is_active=is_active, |
| 167 |
) |
| 168 |
|
| 169 |
|
| 170 |
class SignUpForm(forms.Form): |
| 171 |
""" |
| 172 |
Form class for signing up a new user and new account. |
| 173 |
""" |
| 174 |
|
| 175 |
name = forms.CharField(max_length=50, help_text=_("The name of the organization")) |
| 176 |
slug = forms.SlugField( |
| 177 |
max_length=50, |
| 178 |
help_text=_("The name in all lowercase, suitable for URL identification"), |
| 179 |
) |
| 180 |
email = forms.EmailField() |