| 1 |
|
| 2 |
from django.contrib.sites.shortcuts import get_current_site |
| 3 |
from django.core.exceptions import ImproperlyConfigured |
| 4 |
from django.http import HttpResponseGone |
| 5 |
from django.shortcuts import redirect |
| 6 |
from django.urls import reverse |
| 7 |
from django.utils.translation import gettext as _ |
| 8 |
from django.views.generic import CreateView |
| 9 |
from django.views.generic import DeleteView |
| 10 |
from django.views.generic import DetailView |
| 11 |
from django.views.generic import FormView |
| 12 |
from django.views.generic import ListView |
| 13 |
from django.views.generic import UpdateView |
| 14 |
|
| 15 |
from organizations.backends import invitation_backend |
| 16 |
from organizations.backends import registration_backend |
| 17 |
from organizations.forms import OrganizationAddForm |
| 18 |
from organizations.forms import OrganizationForm |
| 19 |
from organizations.forms import OrganizationUserAddForm |
| 20 |
from organizations.forms import OrganizationUserForm |
| 21 |
from organizations.forms import SignUpForm |
| 22 |
from organizations.utils import create_organization |
| 23 |
from organizations.views.mixins import OrganizationMixin |
| 24 |
from organizations.views.mixins import OrganizationUserMixin |
| 25 |
|
| 26 |
|
| 27 |
class BaseOrganizationList(ListView): |
| 28 |
context_object_name = "organizations" |
| 29 |
|
| 30 |
def get_queryset(self): |
| 31 |
return self.org_model.active.filter(users=self.request.user) |
| 32 |
|
| 33 |
|
| 34 |
class BaseOrganizationDetail(OrganizationMixin, DetailView): |
| 35 |
def get_context_data(self, **kwargs): |
| 36 |
context = super().get_context_data(**kwargs) |
| 37 |
context["organization_users"] = self.organization.organization_users.all() |
| 38 |
context["organization"] = self.organization |
| 39 |
return context |
| 40 |
|
| 41 |
|
| 42 |
class BaseOrganizationCreate(CreateView): |
| 43 |
form_class = OrganizationAddForm |
| 44 |
template_name = "organizations/organization_form.html" |
| 45 |
|
| 46 |
def get_success_url(self): |
| 47 |
return reverse("organization_list") |
| 48 |
|
| 49 |
def get_form_kwargs(self): |
| 50 |
kwargs = super().get_form_kwargs() |
| 51 |
kwargs.update({"request": self.request}) |
| 52 |
return kwargs |
| 53 |
|
| 54 |
|
| 55 |
class BaseOrganizationUpdate(OrganizationMixin, UpdateView): |
| 56 |
form_class = OrganizationForm |
| 57 |
|
| 58 |
def get_form_kwargs(self): |
| 59 |
kwargs = super().get_form_kwargs() |
| 60 |
kwargs.update({"request": self.request}) |
| 61 |
return kwargs |
| 62 |
|
| 63 |
|
| 64 |
class BaseOrganizationDelete(OrganizationMixin, DeleteView): |
| 65 |
def get_success_url(self): |
| 66 |
return reverse("organization_list") |
| 67 |
|
| 68 |
|
| 69 |
class BaseOrganizationUserList(OrganizationMixin, ListView): |
| 70 |
def get(self, request, *args, **kwargs): |
| 71 |
self.organization = self.get_organization() |
| 72 |
self.object_list = self.organization.organization_users.all() |
| 73 |
context = self.get_context_data( |
| 74 |
object_list=self.object_list, |
| 75 |
organization_users=self.object_list, |
| 76 |
organization=self.organization, |
| 77 |
) |
| 78 |
return self.render_to_response(context) |
| 79 |
|
| 80 |
|
| 81 |
class BaseOrganizationUserDetail(OrganizationUserMixin, DetailView): |
| 82 |
pass |
| 83 |
|
| 84 |
|
| 85 |
class BaseOrganizationUserCreate(OrganizationMixin, CreateView): |
| 86 |
form_class = OrganizationUserAddForm |
| 87 |
template_name = "organizations/organizationuser_form.html" |
| 88 |
|
| 89 |
def get_success_url(self): |
| 90 |
return reverse( |
| 91 |
"organization_user_list", |
| 92 |
kwargs={"organization_pk": self.object.organization.pk}, |
| 93 |
) |
| 94 |
|
| 95 |
def get_form_kwargs(self): |
| 96 |
kwargs = super().get_form_kwargs() |
| 97 |
kwargs.update({"organization": self.organization, "request": self.request}) |
| 98 |
return kwargs |
| 99 |
|
| 100 |
def get(self, request, *args, **kwargs): |
| 101 |
self.organization = self.get_object() |
| 102 |
return super().get(request, *args, **kwargs) |
| 103 |
|
| 104 |
def post(self, request, *args, **kwargs): |
| 105 |
self.organization = self.get_object() |
| 106 |
return super().post(request, *args, **kwargs) |
| 107 |
|
| 108 |
|
| 109 |
class BaseOrganizationUserRemind(OrganizationUserMixin, DetailView): |
| 110 |
""" |
| 111 |
Reminder view for already-linked org users |
| 112 |
|
| 113 |
This is only applicable for invitation backends using the |
| 114 |
strategy the original "default" backend uses, which is to |
| 115 |
immediately add existing users to the organization after |
| 116 |
invite, but leave new users inactive until confirmation. |
| 117 |
|
| 118 |
""" |
| 119 |
|
| 120 |
template_name = "organizations/organizationuser_remind.html" |
| 121 |
|
| 122 |
|
| 123 |
def get_success_url(self): |
| 124 |
return reverse( |
| 125 |
"organization_user_list", |
| 126 |
kwargs={"organization_pk": self.object.organization.pk}, |
| 127 |
) |
| 128 |
|
| 129 |
def get(self, request, *args, **kwargs): |
| 130 |
self.object = self.get_object() |
| 131 |
if self.object.user.is_active: |
| 132 |
return HttpResponseGone(_("User is already active")) |
| 133 |
return super().get(request, *args, **kwargs) |
| 134 |
|
| 135 |
def post(self, request, *args, **kwargs): |
| 136 |
self.object = self.get_object() |
| 137 |
if self.object.user.is_active: |
| 138 |
return HttpResponseGone(_("User is already active")) |
| 139 |
invitation_backend().send_reminder( |
| 140 |
self.object.user, |
| 141 |
**{ |
| 142 |
"domain": get_current_site(self.request), |
| 143 |
"organization": self.organization, |
| 144 |
"sender": request.user, |
| 145 |
} |
| 146 |
) |
| 147 |
return redirect(self.get_success_url()) |
| 148 |
|
| 149 |
|
| 150 |
class BaseOrganizationUserUpdate(OrganizationUserMixin, UpdateView): |
| 151 |
form_class = OrganizationUserForm |
| 152 |
|
| 153 |
|
| 154 |
class BaseOrganizationUserDelete(OrganizationUserMixin, DeleteView): |
| 155 |
def get_success_url(self): |
| 156 |
return reverse( |
| 157 |
"organization_user_list", |
| 158 |
kwargs={"organization_pk": self.object.organization.pk}, |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
class OrganizationSignup(FormView): |
| 163 |
""" |
| 164 |
View that allows unregistered users to create an organization account. |
| 165 |
|
| 166 |
It simply processes the form and then calls the specified registration |
| 167 |
backend. |
| 168 |
""" |
| 169 |
|
| 170 |
form_class = SignUpForm |
| 171 |
template_name = "organizations/signup_form.html" |
| 172 |
|
| 173 |
|
| 174 |
backend = registration_backend() |
| 175 |
|
| 176 |
def dispatch(self, request, *args, **kwargs): |
| 177 |
if request.user.is_authenticated: |
| 178 |
return redirect("organization_add") |
| 179 |
return super().dispatch(request, *args, **kwargs) |
| 180 |
|
| 181 |
def get_success_url(self): |
| 182 |
if getattr(self, "success_url", None): |
| 183 |
return self.success_url |
| 184 |
raise ImproperlyConfigured( |
| 185 |
"{cls} must either have a `success_url` attribute defined" |
| 186 |
"or override `get_success_url`".format(cls=self.__class__.__name__) |
| 187 |
) |
| 188 |
|
| 189 |
def form_valid(self, form): |
| 190 |
""" |
| 191 |
Register user and create the organization |
| 192 |
""" |
| 193 |
user = self.backend.register_by_email(form.cleaned_data["email"]) |
| 194 |
create_organization( |
| 195 |
user=user, |
| 196 |
name=form.cleaned_data["name"], |
| 197 |
slug=form.cleaned_data["slug"], |
| 198 |
is_active=False, |
| 199 |
) |
| 200 |
return redirect(self.get_success_url()) |
| 201 |
|
| 202 |
|
| 203 |
class ViewFactory: |
| 204 |
""" |
| 205 |
A class that can create a faked 'module' with model specific views |
| 206 |
|
| 207 |
These views have NO access control applied. |
| 208 |
""" |
| 209 |
|
| 210 |
def __init__(self, org_model): |
| 211 |
self.org_model = org_model |
| 212 |
|
| 213 |
@property |
| 214 |
def OrganizationList(self): |
| 215 |
klass = BaseOrganizationList |
| 216 |
klass.org_model = self.org_model |
| 217 |
return klass |
| 218 |
|
| 219 |
@property |
| 220 |
def OrganizationDetail(self): |
| 221 |
klass = BaseOrganizationDetail |
| 222 |
klass.org_model = self.org_model |
| 223 |
return klass |
| 224 |
|
| 225 |
@property |
| 226 |
def OrganizationCreate(self): |
| 227 |
klass = BaseOrganizationCreate |
| 228 |
klass.org_model = self.org_model |
| 229 |
return klass |
| 230 |
|
| 231 |
@property |
| 232 |
def OrganizationUpdate(self): |
| 233 |
klass = BaseOrganizationUpdate |
| 234 |
klass.org_model = self.org_model |
| 235 |
return klass |
| 236 |
|
| 237 |
@property |
| 238 |
def OrganizationDelete(self): |
| 239 |
klass = BaseOrganizationDelete |
| 240 |
klass.org_model = self.org_model |
| 241 |
return klass |
| 242 |
|
| 243 |
@property |
| 244 |
def OrganizationUserList(self): |
| 245 |
klass = BaseOrganizationUserList |
| 246 |
klass.org_model = self.org_model |
| 247 |
return klass |
| 248 |
|
| 249 |
@property |
| 250 |
def OrganizationUserDetail(self): |
| 251 |
klass = BaseOrganizationUserDetail |
| 252 |
klass.org_model = self.org_model |
| 253 |
return klass |
| 254 |
|
| 255 |
@property |
| 256 |
def OrganizationUserUpdate(self): |
| 257 |
klass = BaseOrganizationUserUpdate |
| 258 |
klass.org_model = self.org_model |
| 259 |
return klass |
| 260 |
|
| 261 |
@property |
| 262 |
def OrganizationUserCreate(self): |
| 263 |
klass = BaseOrganizationUserCreate |
| 264 |
klass.org_model = self.org_model |
| 265 |
return klass |
| 266 |
|
| 267 |
@property |
| 268 |
def OrganizationUserDelete(self): |
| 269 |
klass = BaseOrganizationUserDelete |
| 270 |
klass.org_model = self.org_model |
| 271 |
return klass |
| 272 |
|
| 273 |
@property |
| 274 |
def OrganizationUserRemind(self): |
| 275 |
klass = BaseOrganizationUserRemind |
| 276 |
klass.org_model = self.org_model |
| 277 |
return klass |