| 1 |
|
| 2 |
from django.core.exceptions import PermissionDenied |
| 3 |
from django.shortcuts import get_object_or_404 |
| 4 |
from django.utils.functional import cached_property |
| 5 |
from django.utils.translation import gettext_lazy as _ |
| 6 |
|
| 7 |
from organizations.models import Organization |
| 8 |
from organizations.models import OrganizationUser |
| 9 |
|
| 10 |
|
| 11 |
class OrganizationMixin: |
| 12 |
"""Mixin used like a SingleObjectMixin to fetch an organization""" |
| 13 |
|
| 14 |
org_model = Organization |
| 15 |
org_context_name = "organization" |
| 16 |
|
| 17 |
def get_org_model(self): |
| 18 |
return self.org_model |
| 19 |
|
| 20 |
def get_context_data(self, **kwargs): |
| 21 |
kwargs.update({self.org_context_name: self.organization}) |
| 22 |
return super().get_context_data(**kwargs) |
| 23 |
|
| 24 |
@cached_property |
| 25 |
def organization(self): |
| 26 |
organization_pk = self.kwargs.get("organization_pk", None) |
| 27 |
return get_object_or_404(self.get_org_model(), pk=organization_pk) |
| 28 |
|
| 29 |
def get_object(self): |
| 30 |
return self.organization |
| 31 |
|
| 32 |
get_organization = get_object |
| 33 |
|
| 34 |
|
| 35 |
class OrganizationUserMixin(OrganizationMixin): |
| 36 |
"""Mixin used like a SingleObjectMixin to fetch an organization user""" |
| 37 |
|
| 38 |
user_model = OrganizationUser |
| 39 |
org_user_context_name = "organization_user" |
| 40 |
|
| 41 |
def get_user_model(self): |
| 42 |
return self.user_model |
| 43 |
|
| 44 |
def get_context_data(self, **kwargs): |
| 45 |
kwargs = super().get_context_data(**kwargs) |
| 46 |
kwargs.update( |
| 47 |
{ |
| 48 |
self.org_user_context_name: self.object, |
| 49 |
self.org_context_name: self.object.organization, |
| 50 |
} |
| 51 |
) |
| 52 |
return kwargs |
| 53 |
|
| 54 |
@cached_property |
| 55 |
def organization_user(self): |
| 56 |
""" |
| 57 |
Returns the OrganizationUser object |
| 58 |
|
| 59 |
This is fetched based on the primary keys for both |
| 60 |
the organization and the organization user. |
| 61 |
""" |
| 62 |
organization_pk = self.kwargs.get("organization_pk", None) |
| 63 |
user_pk = self.kwargs.get("user_pk", None) |
| 64 |
return get_object_or_404( |
| 65 |
self.get_user_model().objects.select_related(), |
| 66 |
user__pk=user_pk, |
| 67 |
organization__pk=organization_pk, |
| 68 |
) |
| 69 |
|
| 70 |
def get_object(self): |
| 71 |
"""Proxy for the base class interface |
| 72 |
|
| 73 |
This can be called all day long and the object is queried once. |
| 74 |
""" |
| 75 |
return self.organization_user |
| 76 |
|
| 77 |
|
| 78 |
class MembershipRequiredMixin: |
| 79 |
"""This mixin presumes that authentication has already been checked""" |
| 80 |
|
| 81 |
def dispatch(self, request, *args, **kwargs): |
| 82 |
self.request = request |
| 83 |
self.args = args |
| 84 |
self.kwargs = kwargs |
| 85 |
if ( |
| 86 |
not self.organization.is_member(request.user) |
| 87 |
and not request.user.is_superuser |
| 88 |
): |
| 89 |
raise PermissionDenied(_("Wrong organization")) |
| 90 |
return super().dispatch(request, *args, **kwargs) |
| 91 |
|
| 92 |
|
| 93 |
class AdminRequiredMixin: |
| 94 |
"""This mixin presumes that authentication has already been checked""" |
| 95 |
|
| 96 |
def dispatch(self, request, *args, **kwargs): |
| 97 |
self.request = request |
| 98 |
self.args = args |
| 99 |
self.kwargs = kwargs |
| 100 |
if ( |
| 101 |
not self.organization.is_admin(request.user) |
| 102 |
and not request.user.is_superuser |
| 103 |
): |
| 104 |
raise PermissionDenied(_("Sorry, admins only")) |
| 105 |
return super().dispatch(request, *args, **kwargs) |
| 106 |
|
| 107 |
|
| 108 |
class OwnerRequiredMixin: |
| 109 |
"""This mixin presumes that authentication has already been checked""" |
| 110 |
|
| 111 |
def dispatch(self, request, *args, **kwargs): |
| 112 |
self.request = request |
| 113 |
self.args = args |
| 114 |
self.kwargs = kwargs |
| 115 |
if ( |
| 116 |
self.organization.owner.organization_user.user != request.user |
| 117 |
and not request.user.is_superuser |
| 118 |
): |
| 119 |
raise PermissionDenied(_("You are not the organization owner")) |
| 120 |
return super().dispatch(request, *args, **kwargs) |