2020-05-08 16:45:53 +00:00
|
|
|
"""passbook Flow administration"""
|
2020-08-28 13:06:25 +00:00
|
|
|
from django.contrib import messages
|
2020-05-08 16:45:53 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.contrib.auth.mixins import (
|
|
|
|
PermissionRequiredMixin as DjangoPermissionRequiredMixin,
|
|
|
|
)
|
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2020-08-28 13:06:25 +00:00
|
|
|
from django.http import HttpRequest, HttpResponse, JsonResponse
|
2020-05-08 16:45:53 +00:00
|
|
|
from django.urls import reverse_lazy
|
2020-09-11 21:21:11 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2020-08-28 13:06:25 +00:00
|
|
|
from django.views.generic import DetailView, FormView, ListView, UpdateView
|
2020-05-08 16:45:53 +00:00
|
|
|
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
|
|
|
|
|
2020-09-25 23:56:28 +00:00
|
|
|
from passbook.admin.views.utils import (
|
|
|
|
BackSuccessUrlMixin,
|
|
|
|
DeleteMessageView,
|
|
|
|
UserPaginateListMixin,
|
|
|
|
)
|
2020-08-28 13:06:25 +00:00
|
|
|
from passbook.flows.forms import FlowForm, FlowImportForm
|
2020-05-08 16:45:53 +00:00
|
|
|
from passbook.flows.models import Flow
|
2020-06-30 08:55:38 +00:00
|
|
|
from passbook.flows.planner import PLAN_CONTEXT_PENDING_USER
|
2020-09-08 14:48:25 +00:00
|
|
|
from passbook.flows.transfer.common import DataclassEncoder
|
2020-08-28 13:06:25 +00:00
|
|
|
from passbook.flows.transfer.exporter import FlowExporter
|
|
|
|
from passbook.flows.transfer.importer import FlowImporter
|
2020-06-30 08:55:38 +00:00
|
|
|
from passbook.flows.views import SESSION_KEY_PLAN, FlowPlanner
|
|
|
|
from passbook.lib.utils.urls import redirect_with_qs
|
2020-05-08 16:45:53 +00:00
|
|
|
from passbook.lib.views import CreateAssignPermView
|
|
|
|
|
|
|
|
|
2020-09-25 23:56:28 +00:00
|
|
|
class FlowListView(
|
|
|
|
LoginRequiredMixin, PermissionListMixin, UserPaginateListMixin, ListView
|
|
|
|
):
|
2020-05-08 16:45:53 +00:00
|
|
|
"""Show list of all flows"""
|
|
|
|
|
|
|
|
model = Flow
|
|
|
|
permission_required = "passbook_flows.view_flow"
|
|
|
|
ordering = "name"
|
|
|
|
template_name = "administration/flow/list.html"
|
|
|
|
|
|
|
|
|
|
|
|
class FlowCreateView(
|
|
|
|
SuccessMessageMixin,
|
2020-09-25 23:56:28 +00:00
|
|
|
BackSuccessUrlMixin,
|
2020-05-08 16:45:53 +00:00
|
|
|
LoginRequiredMixin,
|
|
|
|
DjangoPermissionRequiredMixin,
|
|
|
|
CreateAssignPermView,
|
|
|
|
):
|
|
|
|
"""Create new Flow"""
|
|
|
|
|
|
|
|
model = Flow
|
|
|
|
form_class = FlowForm
|
|
|
|
permission_required = "passbook_flows.add_flow"
|
|
|
|
|
|
|
|
template_name = "generic/create.html"
|
|
|
|
success_url = reverse_lazy("passbook_admin:flows")
|
|
|
|
success_message = _("Successfully created Flow")
|
|
|
|
|
2020-06-30 08:55:38 +00:00
|
|
|
|
2020-05-08 16:45:53 +00:00
|
|
|
class FlowUpdateView(
|
2020-09-25 23:56:28 +00:00
|
|
|
SuccessMessageMixin,
|
|
|
|
BackSuccessUrlMixin,
|
|
|
|
LoginRequiredMixin,
|
|
|
|
PermissionRequiredMixin,
|
|
|
|
UpdateView,
|
2020-05-08 16:45:53 +00:00
|
|
|
):
|
|
|
|
"""Update flow"""
|
|
|
|
|
|
|
|
model = Flow
|
|
|
|
form_class = FlowForm
|
|
|
|
permission_required = "passbook_flows.change_flow"
|
|
|
|
|
|
|
|
template_name = "generic/update.html"
|
|
|
|
success_url = reverse_lazy("passbook_admin:flows")
|
|
|
|
success_message = _("Successfully updated Flow")
|
|
|
|
|
|
|
|
|
2020-07-01 22:13:33 +00:00
|
|
|
class FlowDeleteView(LoginRequiredMixin, PermissionRequiredMixin, DeleteMessageView):
|
2020-05-08 16:45:53 +00:00
|
|
|
"""Delete flow"""
|
|
|
|
|
|
|
|
model = Flow
|
|
|
|
permission_required = "passbook_flows.delete_flow"
|
|
|
|
|
|
|
|
template_name = "generic/delete.html"
|
|
|
|
success_url = reverse_lazy("passbook_admin:flows")
|
|
|
|
success_message = _("Successfully deleted Flow")
|
|
|
|
|
2020-07-01 22:13:33 +00:00
|
|
|
|
|
|
|
class FlowDebugExecuteView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
|
|
|
|
"""Debug exectue flow, setting the current user as pending user"""
|
|
|
|
|
|
|
|
model = Flow
|
|
|
|
permission_required = "passbook_flows.view_flow"
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def get(self, request: HttpRequest, pk: str) -> HttpResponse:
|
|
|
|
"""Debug exectue flow, setting the current user as pending user"""
|
|
|
|
flow: Flow = self.get_object()
|
|
|
|
planner = FlowPlanner(flow)
|
|
|
|
planner.use_cache = False
|
|
|
|
plan = planner.plan(self.request, {PLAN_CONTEXT_PENDING_USER: request.user})
|
|
|
|
self.request.session[SESSION_KEY_PLAN] = plan
|
|
|
|
return redirect_with_qs(
|
2020-09-30 17:34:22 +00:00
|
|
|
"passbook_flows:flow-executor-shell",
|
|
|
|
self.request.GET,
|
|
|
|
flow_slug=flow.slug,
|
2020-07-01 22:13:33 +00:00
|
|
|
)
|
2020-08-28 13:06:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FlowImportView(LoginRequiredMixin, FormView):
|
|
|
|
"""Import flow from JSON Export; only allowed for superusers
|
|
|
|
as these flows can contain python code"""
|
|
|
|
|
|
|
|
form_class = FlowImportForm
|
|
|
|
template_name = "administration/flow/import.html"
|
|
|
|
success_url = reverse_lazy("passbook_admin:flows")
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
if not request.user.is_superuser:
|
|
|
|
return self.handle_no_permission()
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def form_valid(self, form: FlowImportForm) -> HttpResponse:
|
|
|
|
importer = FlowImporter(form.cleaned_data["flow"].read().decode())
|
|
|
|
successful = importer.apply()
|
|
|
|
if not successful:
|
|
|
|
messages.error(self.request, _("Failed to import flow."))
|
|
|
|
else:
|
|
|
|
messages.success(self.request, _("Successfully imported flow."))
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
|
|
|
|
class FlowExportView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
|
|
|
|
"""Export Flow"""
|
|
|
|
|
|
|
|
model = Flow
|
|
|
|
permission_required = "passbook_flows.export_flow"
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def get(self, request: HttpRequest, pk: str) -> HttpResponse:
|
|
|
|
"""Debug exectue flow, setting the current user as pending user"""
|
|
|
|
flow: Flow = self.get_object()
|
|
|
|
exporter = FlowExporter(flow)
|
2020-09-08 14:48:25 +00:00
|
|
|
response = JsonResponse(exporter.export(), encoder=DataclassEncoder, safe=False)
|
2020-08-28 13:06:25 +00:00
|
|
|
response["Content-Disposition"] = f'attachment; filename="{flow.slug}.json"'
|
|
|
|
return response
|