| 1 |
|
| 2 |
import warnings |
| 3 |
|
| 4 |
from django.conf import settings |
| 5 |
from django.db import models |
| 6 |
from django.urls import reverse |
| 7 |
from django.utils.translation import gettext_lazy as _ |
| 8 |
|
| 9 |
|
| 10 |
from organizations.base import AbstractBaseInvitation |
| 11 |
from organizations.base import AbstractBaseOrganization |
| 12 |
from organizations.base import AbstractBaseOrganizationOwner |
| 13 |
from organizations.base import AbstractBaseOrganizationUser |
| 14 |
from organizations.base import with_metaclass |
| 15 |
from organizations.base import OrgMeta |
| 16 |
from organizations.fields import AutoCreatedField |
| 17 |
from organizations.fields import AutoLastModifiedField |
| 18 |
from organizations.fields import SlugField |
| 19 |
from organizations.signals import owner_changed |
| 20 |
from organizations.signals import user_added |
| 21 |
from organizations.signals import user_removed |
| 22 |
|
| 23 |
USER_MODEL = getattr(settings, "AUTH_USER_MODEL", "auth.User") |
| 24 |
ORGS_TIMESTAMPED_MODEL = getattr(settings, "ORGS_TIMESTAMPED_MODEL", None) |
| 25 |
|
| 26 |
if ORGS_TIMESTAMPED_MODEL: |
| 27 |
warnings.warn( |
| 28 |
"Configured TimestampModel has been replaced and is now ignored.", |
| 29 |
DeprecationWarning, |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
class SharedBaseModel(models.Model): |
| 34 |
""" |
| 35 |
Adds fields ``created`` and ``modified`` and |
| 36 |
two private methods that are used by the rest |
| 37 |
of the abstract models. |
| 38 |
""" |
| 39 |
|
| 40 |
created = AutoCreatedField() |
| 41 |
modified = AutoLastModifiedField() |
| 42 |
|
| 43 |
@property |
| 44 |
def _org_user_model(self): |
| 45 |
model = self.__class__.module_registry[self.__class__.__module__][ |
| 46 |
"OrgUserModel" |
| 47 |
] |
| 48 |
if model is None: |
| 49 |
model = self.__class__.module_registry["organizations.models"][ |
| 50 |
"OrgUserModel" |
| 51 |
] |
| 52 |
return model |
| 53 |
|
| 54 |
@property |
| 55 |
def _org_owner_model(self): |
| 56 |
model = self.__class__.module_registry[self.__class__.__module__][ |
| 57 |
"OrgOwnerModel" |
| 58 |
] |
| 59 |
if model is None: |
| 60 |
model = self.__class__.module_registry["organizations.models"][ |
| 61 |
"OrgOwnerModel" |
| 62 |
] |
| 63 |
return model |
| 64 |
|
| 65 |
class Meta: |
| 66 |
abstract = True |
| 67 |
|
| 68 |
|
| 69 |
class AbstractOrganization( |
| 70 |
with_metaclass(OrgMeta, SharedBaseModel, AbstractBaseOrganization) |
| 71 |
): |
| 72 |
""" |
| 73 |
Abstract Organization model. |
| 74 |
""" |
| 75 |
|
| 76 |
slug = SlugField( |
| 77 |
max_length=200, |
| 78 |
blank=False, |
| 79 |
editable=True, |
| 80 |
populate_from="name", |
| 81 |
unique=True, |
| 82 |
help_text=_("The name in all lowercase, suitable for URL identification"), |
| 83 |
) |
| 84 |
|
| 85 |
class Meta(AbstractBaseOrganization.Meta): |
| 86 |
abstract = True |
| 87 |
verbose_name = _("organization") |
| 88 |
verbose_name_plural = _("organizations") |
| 89 |
|
| 90 |
def __str__(self): |
| 91 |
return self.name |
| 92 |
|
| 93 |
def get_absolute_url(self): |
| 94 |
return reverse("organization_detail", kwargs={"organization_pk": self.pk}) |
| 95 |
|
| 96 |
def add_user(self, user, is_admin=False): |
| 97 |
""" |
| 98 |
Adds a new user and if the first user makes the user an admin and |
| 99 |
the owner. |
| 100 |
""" |
| 101 |
users_count = self.users.all().count() |
| 102 |
if users_count == 0: |
| 103 |
is_admin = True |
| 104 |
|
| 105 |
org_user = self._org_user_model.objects.create( |
| 106 |
user=user, organization=self, is_admin=is_admin |
| 107 |
) |
| 108 |
if users_count == 0: |
| 109 |
|
| 110 |
self._org_owner_model.objects.create( |
| 111 |
organization=self, organization_user=org_user |
| 112 |
) |
| 113 |
|
| 114 |
|
| 115 |
user_added.send(sender=self, user=user) |
| 116 |
return org_user |
| 117 |
|
| 118 |
def remove_user(self, user): |
| 119 |
""" |
| 120 |
Deletes a user from an organization. |
| 121 |
""" |
| 122 |
org_user = self._org_user_model.objects.get(user=user, organization=self) |
| 123 |
org_user.delete() |
| 124 |
|
| 125 |
|
| 126 |
user_removed.send(sender=self, user=user) |
| 127 |
|
| 128 |
def get_or_add_user(self, user, **kwargs): |
| 129 |
""" |
| 130 |
Adds a new user to the organization, and if it's the first user makes |
| 131 |
the user an admin and the owner. Uses the `get_or_create` method to |
| 132 |
create or return the existing user. |
| 133 |
|
| 134 |
`user` should be a user instance, e.g. `auth.User`. |
| 135 |
|
| 136 |
Returns the same tuple as the `get_or_create` method, the |
| 137 |
`OrganizationUser` and a boolean value indicating whether the |
| 138 |
OrganizationUser was created or not. |
| 139 |
""" |
| 140 |
is_admin = kwargs.pop("is_admin", False) |
| 141 |
users_count = self.users.all().count() |
| 142 |
if users_count == 0: |
| 143 |
is_admin = True |
| 144 |
|
| 145 |
org_user, created = self._org_user_model.objects.get_or_create( |
| 146 |
organization=self, user=user, defaults={"is_admin": is_admin} |
| 147 |
) |
| 148 |
if users_count == 0: |
| 149 |
self._org_owner_model.objects.create( |
| 150 |
organization=self, organization_user=org_user |
| 151 |
) |
| 152 |
if created: |
| 153 |
|
| 154 |
user_added.send(sender=self, user=user) |
| 155 |
return org_user, created |
| 156 |
|
| 157 |
def change_owner(self, new_owner): |
| 158 |
""" |
| 159 |
Changes ownership of an organization. |
| 160 |
""" |
| 161 |
old_owner = self.owner.organization_user |
| 162 |
self.owner.organization_user = new_owner |
| 163 |
self.owner.save() |
| 164 |
|
| 165 |
|
| 166 |
owner_changed.send(sender=self, old=old_owner, new=new_owner) |
| 167 |
|
| 168 |
def is_admin(self, user): |
| 169 |
""" |
| 170 |
Returns True is user is an admin in the organization, otherwise false |
| 171 |
""" |
| 172 |
return ( |
| 173 |
True if self.organization_users.filter(user=user, is_admin=True) else False |
| 174 |
) |
| 175 |
|
| 176 |
def is_owner(self, user): |
| 177 |
""" |
| 178 |
Returns True is user is the organization's owner, otherwise false |
| 179 |
""" |
| 180 |
return self.owner.organization_user.user == user |
| 181 |
|
| 182 |
|
| 183 |
class AbstractOrganizationUser( |
| 184 |
with_metaclass(OrgMeta, SharedBaseModel, AbstractBaseOrganizationUser) |
| 185 |
): |
| 186 |
""" |
| 187 |
Abstract OrganizationUser model |
| 188 |
""" |
| 189 |
|
| 190 |
is_admin = models.BooleanField(default=False) |
| 191 |
|
| 192 |
class Meta(AbstractBaseOrganizationUser.Meta): |
| 193 |
abstract = True |
| 194 |
verbose_name = _("organization user") |
| 195 |
verbose_name_plural = _("organization users") |
| 196 |
|
| 197 |
def __str__(self): |
| 198 |
return "{0} ({1})".format( |
| 199 |
self.name if self.user.is_active else self.user.email, |
| 200 |
self.organization.name, |
| 201 |
) |
| 202 |
|
| 203 |
def delete(self, using=None): |
| 204 |
""" |
| 205 |
If the organization user is also the owner, this should not be deleted |
| 206 |
unless it's part of a cascade from the Organization. |
| 207 |
|
| 208 |
If there is no owner then the deletion should proceed. |
| 209 |
""" |
| 210 |
from organizations.exceptions import OwnershipRequired |
| 211 |
|
| 212 |
try: |
| 213 |
if self.organization.owner.organization_user.pk == self.pk: |
| 214 |
raise OwnershipRequired( |
| 215 |
_( |
| 216 |
"Cannot delete organization owner " |
| 217 |
"before organization or transferring ownership." |
| 218 |
) |
| 219 |
) |
| 220 |
|
| 221 |
except self._org_owner_model.DoesNotExist: |
| 222 |
pass |
| 223 |
super().delete(using=using) |
| 224 |
|
| 225 |
def get_absolute_url(self): |
| 226 |
return reverse( |
| 227 |
"organization_user_detail", |
| 228 |
kwargs={"organization_pk": self.organization.pk, "user_pk": self.user.pk}, |
| 229 |
) |
| 230 |
|
| 231 |
|
| 232 |
class AbstractOrganizationOwner( |
| 233 |
with_metaclass(OrgMeta, SharedBaseModel, AbstractBaseOrganizationOwner) |
| 234 |
): |
| 235 |
""" |
| 236 |
Abstract OrganizationOwner model |
| 237 |
""" |
| 238 |
|
| 239 |
class Meta: |
| 240 |
abstract = True |
| 241 |
verbose_name = _("organization owner") |
| 242 |
verbose_name_plural = _("organization owners") |
| 243 |
|
| 244 |
def save(self, *args, **kwargs): |
| 245 |
""" |
| 246 |
Extends the default save method by verifying that the chosen |
| 247 |
organization user is associated with the organization. |
| 248 |
|
| 249 |
Method validates against the primary key of the organization because |
| 250 |
when validating an inherited model it may be checking an instance of |
| 251 |
`Organization` against an instance of `CustomOrganization`. Mutli-table |
| 252 |
inheritance means the database keys will be identical though. |
| 253 |
|
| 254 |
""" |
| 255 |
from organizations.exceptions import OrganizationMismatch |
| 256 |
|
| 257 |
if self.organization_user.organization.pk != self.organization.pk: |
| 258 |
raise OrganizationMismatch |
| 259 |
else: |
| 260 |
super().save(*args, **kwargs) |
| 261 |
|
| 262 |
|
| 263 |
class AbstractOrganizationInvitation( |
| 264 |
with_metaclass(OrgMeta, SharedBaseModel, AbstractBaseInvitation) |
| 265 |
): |
| 266 |
""" |
| 267 |
Abstract OrganizationInvitationBase model |
| 268 |
""" |
| 269 |
|
| 270 |
class Meta: |
| 271 |
abstract = True |