From beabba289057141570d9c2b1919ddaeb441e36c7 Mon Sep 17 00:00:00 2001 From: Jens L Date: Sun, 24 May 2020 00:57:25 +0200 Subject: [PATCH] flows: Load Stages without refreshing the whole page (#33) * flows: initial implementation of FlowExecutorShell * flows: load messages dynamically upon card refresh --- .pylintrc | 2 +- passbook/api/v2/urls.py | 5 +- passbook/core/api/messages.py | 36 ++++ passbook/core/api/policies.py | 31 ---- passbook/core/templates/login/base.html | 108 ++--------- .../core/templates/login/form_with_user.html | 23 --- passbook/core/tests/test_views_utils.py | 9 +- passbook/core/views/utils.py | 17 -- passbook/flows/forms.py | 4 - passbook/flows/stage.py | 2 + passbook/flows/templates/flows/shell.html | 169 ++++++++++++++++++ passbook/flows/tests/test_views_helper.py | 4 +- passbook/flows/urls.py | 6 +- passbook/flows/views.py | 23 ++- passbook/policies/api.py | 32 +++- passbook/providers/oauth/urls.py | 7 +- passbook/providers/oauth/views/oauth2.py | 16 +- passbook/sources/oauth/clients.py | 2 +- .../stages/identification/login.html | 24 ++- passbook/stages/identification/tests.py | 20 ++- .../templates/stages/password/backend.html | 44 ++++- passbook/static/static/passbook/pf.css | 19 ++ passbook/static/static/passbook/pf.js | 24 +-- swagger.yaml | 38 ++++ 24 files changed, 428 insertions(+), 237 deletions(-) create mode 100644 passbook/core/api/messages.py delete mode 100644 passbook/core/api/policies.py create mode 100644 passbook/flows/templates/flows/shell.html diff --git a/.pylintrc b/.pylintrc index 894546021..fe0922625 100644 --- a/.pylintrc +++ b/.pylintrc @@ -5,4 +5,4 @@ load-plugins=pylint_django,pylint.extensions.bad_builtin extension-pkg-whitelist=lxml const-rgx=[a-zA-Z0-9_]{1,40}$ ignored-modules=django-otp -jobs=4 +jobs=12 diff --git a/passbook/api/v2/urls.py b/passbook/api/v2/urls.py index f0150b367..4dc29f438 100644 --- a/passbook/api/v2/urls.py +++ b/passbook/api/v2/urls.py @@ -10,14 +10,14 @@ from passbook.api.permissions import CustomObjectPermissions from passbook.audit.api import EventViewSet from passbook.core.api.applications import ApplicationViewSet from passbook.core.api.groups import GroupViewSet -from passbook.core.api.policies import PolicyViewSet +from passbook.core.api.messages import MessagesViewSet from passbook.core.api.propertymappings import PropertyMappingViewSet from passbook.core.api.providers import ProviderViewSet from passbook.core.api.sources import SourceViewSet from passbook.core.api.users import UserViewSet from passbook.flows.api import FlowStageBindingViewSet, FlowViewSet, StageViewSet from passbook.lib.utils.reflection import get_apps -from passbook.policies.api import PolicyBindingViewSet +from passbook.policies.api import PolicyBindingViewSet, PolicyViewSet from passbook.policies.dummy.api import DummyPolicyViewSet from passbook.policies.expiry.api import PasswordExpiryPolicyViewSet from passbook.policies.expression.api import ExpressionPolicyViewSet @@ -55,6 +55,7 @@ for _passbook_app in get_apps(): router.register("core/applications", ApplicationViewSet) router.register("core/groups", GroupViewSet) router.register("core/users", UserViewSet) +router.register("core/messages", MessagesViewSet, basename="messages") router.register("audit/events", EventViewSet) diff --git a/passbook/core/api/messages.py b/passbook/core/api/messages.py new file mode 100644 index 000000000..0dd68c4a4 --- /dev/null +++ b/passbook/core/api/messages.py @@ -0,0 +1,36 @@ +"""core messages API""" +from django.contrib.messages import get_messages +from drf_yasg.utils import swagger_auto_schema +from rest_framework.permissions import AllowAny +from rest_framework.request import Request +from rest_framework.response import Response +from rest_framework.serializers import ReadOnlyField, Serializer +from rest_framework.viewsets import ViewSet + + +class MessageSerializer(Serializer): + """Serialize Django Message into DRF Object""" + + message = ReadOnlyField() + level = ReadOnlyField() + tags = ReadOnlyField() + extra_tags = ReadOnlyField() + level_tag = ReadOnlyField() + + def create(self, request: Request) -> Response: + raise NotImplementedError + + def update(self, request: Request) -> Response: + raise NotImplementedError + + +class MessagesViewSet(ViewSet): + """Read-only view set that returns the current session's messages""" + + permission_classes = [AllowAny] + + @swagger_auto_schema(responses={200: MessageSerializer(many=True)}) + def list(self, request: Request) -> Response: + """List current messages and pass into Serializer""" + all_messages = list(get_messages(request)) + return Response(MessageSerializer(all_messages, many=True).data) diff --git a/passbook/core/api/policies.py b/passbook/core/api/policies.py deleted file mode 100644 index 13d6d7ad9..000000000 --- a/passbook/core/api/policies.py +++ /dev/null @@ -1,31 +0,0 @@ -"""Policy API Views""" -from rest_framework.serializers import ModelSerializer, SerializerMethodField -from rest_framework.viewsets import ReadOnlyModelViewSet - -from passbook.policies.forms import GENERAL_FIELDS -from passbook.policies.models import Policy - - -class PolicySerializer(ModelSerializer): - """Policy Serializer""" - - __type__ = SerializerMethodField(method_name="get_type") - - def get_type(self, obj): - """Get object type so that we know which API Endpoint to use to get the full object""" - return obj._meta.object_name.lower().replace("policy", "") - - class Meta: - - model = Policy - fields = ["pk"] + GENERAL_FIELDS + ["__type__"] - - -class PolicyViewSet(ReadOnlyModelViewSet): - """Policy Viewset""" - - queryset = Policy.objects.all() - serializer_class = PolicySerializer - - def get_queryset(self): - return Policy.objects.select_subclasses() diff --git a/passbook/core/templates/login/base.html b/passbook/core/templates/login/base.html index 6d1070dae..b7234adaa 100644 --- a/passbook/core/templates/login/base.html +++ b/passbook/core/templates/login/base.html @@ -1,96 +1,22 @@ -{% extends 'base/skeleton.html' %} - {% load static %} {% load i18n %} -{% block body %} -
- - - - - - - - - - - -
{% include 'partials/messages.html' %} -
- + + + -{% endblock %} diff --git a/passbook/core/templates/login/form_with_user.html b/passbook/core/templates/login/form_with_user.html index 6eb1412f5..a7c2461fe 100644 --- a/passbook/core/templates/login/form_with_user.html +++ b/passbook/core/templates/login/form_with_user.html @@ -3,29 +3,6 @@ {% load i18n %} {% load passbook_utils %} -{% block head %} -{{ block.super }} - -{% endblock %} - {% block above_form %}