This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/admin/views/invitations.py

68 lines
2.4 KiB
Python
Raw Normal View History

2018-12-10 13:49:15 +00:00
"""passbook Invitation administration"""
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.mixins import \
PermissionRequiredMixin as DjangoPermissionRequiredMixin
2018-12-10 13:13:18 +00:00
from django.contrib.messages.views import SuccessMessageMixin
2018-12-10 13:49:15 +00:00
from django.http import HttpResponseRedirect
2018-12-10 13:13:18 +00:00
from django.urls import reverse_lazy
from django.utils.translation import ugettext as _
from django.views.generic import DeleteView, ListView
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
2018-12-10 13:13:18 +00:00
2018-12-10 13:49:15 +00:00
from passbook.core.forms.invitations import InvitationForm
from passbook.core.models import Invitation
2018-12-10 15:54:28 +00:00
from passbook.core.signals import invitation_created
from passbook.lib.views import CreateAssignPermView
2018-12-10 13:13:18 +00:00
class InvitationListView(LoginRequiredMixin, PermissionListMixin, ListView):
2018-12-10 13:21:42 +00:00
"""Show list of all invitations"""
2018-12-10 13:13:18 +00:00
2018-12-10 13:49:15 +00:00
model = Invitation
permission_required = 'passbook_core.view_invitation'
2018-12-10 13:21:42 +00:00
template_name = 'administration/invitation/list.html'
2018-12-10 13:13:18 +00:00
class InvitationCreateView(SuccessMessageMixin, LoginRequiredMixin,
DjangoPermissionRequiredMixin, CreateAssignPermView):
2018-12-10 13:49:15 +00:00
"""Create new Invitation"""
2018-12-10 13:13:18 +00:00
model = Invitation
form_class = InvitationForm
permission_required = 'passbook_core.add_invitation'
2018-12-10 13:13:18 +00:00
template_name = 'generic/create.html'
2018-12-10 13:21:42 +00:00
success_url = reverse_lazy('passbook_admin:invitations')
2018-12-10 13:49:15 +00:00
success_message = _('Successfully created Invitation')
2018-12-10 13:13:18 +00:00
def get_context_data(self, **kwargs):
kwargs['type'] = 'Invitation'
return super().get_context_data(**kwargs)
2018-12-10 13:49:15 +00:00
def form_valid(self, form):
obj = form.save(commit=False)
obj.created_by = self.request.user
obj.save()
2018-12-10 15:54:28 +00:00
invitation_created.send(
sender=self,
request=self.request,
invitation=obj)
2018-12-10 13:49:15 +00:00
return HttpResponseRedirect(self.success_url)
2018-12-10 13:13:18 +00:00
class InvitationDeleteView(SuccessMessageMixin, LoginRequiredMixin,
PermissionRequiredMixin, DeleteView):
2018-12-10 13:21:42 +00:00
"""Delete invitation"""
2018-12-10 13:13:18 +00:00
2018-12-10 13:49:15 +00:00
model = Invitation
permission_required = 'passbook_core.delete_invitation'
2018-12-10 13:13:18 +00:00
template_name = 'generic/delete.html'
2018-12-10 13:21:42 +00:00
success_url = reverse_lazy('passbook_admin:invitations')
success_message = _('Successfully deleted Invitation')
def delete(self, request, *args, **kwargs):
messages.success(self.request, self.success_message)
return super().delete(request, *args, **kwargs)